1 // Copyright 2014 Simon Lydell
2 // X11 (“MIT”) Licensed. (See LICENSE.)
4 var expect = require("expect.js")
6 var sourceMappingURL = require("../")
11 "/*# sourceMappingURL=foo.js.map */"
15 "//# sourceMappingURL=foo.js.map"
20 "# sourceMappingURL=foo.js.map",
26 "//# sourceMappingURL=foo.js.map",
32 var nonTrailingComments = {
36 "//# sourceMappingURL=foo.js.map",
46 "/*! Library Name v1.0.0",
47 "//# sourceMappingURL=foo.js.map",
52 "/*! Library Name v1.0.0",
60 function forEachComment(fn) {
61 forOf(comments, function(name, comment) {
62 var description = "the '" + name + "' syntax with "
63 fn(comment.join("\n"), description + "regular newlines")
64 fn(comment.join("\r\n"), description + "Windows newlines")
68 function forEachNonTrailingComment(fn) {
69 forOf(nonTrailingComments, function(name, comment) {
71 var description = "the '" + name + "' syntax with "
74 contents: comment.contents.join("\n"),
75 solution: comment.solution.join("\n")
76 }, description + "regular newlines")
79 contents: comment.contents.join("\r\n"),
80 solution: comment.solution.join("\r\n")
81 }, description + "Windows newlines")
85 function forOf(obj, fn) {
86 for (var key in obj) {
87 if (Object.prototype.hasOwnProperty.call(obj, key)) {
94 describe("sourceMappingURL", function() {
96 describe(".getFrom", function() {
98 forEachComment(function(comment, description) {
100 it("gets the url from " + description, function() {
101 expect(sourceMappingURL.getFrom("code\n" + comment))
102 .to.equal("foo.js.map")
104 expect(sourceMappingURL.getFrom("code" + comment))
105 .to.equal("foo.js.map")
107 expect(sourceMappingURL.getFrom(comment))
108 .to.equal("foo.js.map")
113 forEachNonTrailingComment(function(comment, description) {
115 it("gets the url from " + description, function() {
116 expect(sourceMappingURL.getFrom("code\n" + comment.contents))
117 .to.equal("foo.js.map")
119 expect(sourceMappingURL.getFrom("code" + comment.contents))
120 .to.equal("foo.js.map")
122 expect(sourceMappingURL.getFrom(comment.contents))
123 .to.equal("foo.js.map")
129 it("returns null if no comment", function() {
130 expect(sourceMappingURL.getFrom("code"))
135 it("can return an empty string as url", function() {
136 expect(sourceMappingURL.getFrom("/*# sourceMappingURL= */"))
141 it("is detachable", function() {
142 var get = sourceMappingURL.getFrom
143 expect(get("/*# sourceMappingURL=foo */"))
150 describe(".existsIn", function() {
152 forEachComment(function(comment, description) {
154 it("returns true for " + description, function() {
155 expect(sourceMappingURL.existsIn("code\n" + comment))
158 expect(sourceMappingURL.existsIn("code" + comment))
161 expect(sourceMappingURL.existsIn(comment))
167 forEachNonTrailingComment(function(comment, description) {
169 it("returns true for " + description, function() {
170 expect(sourceMappingURL.existsIn("code\n" + comment.contents))
173 expect(sourceMappingURL.existsIn("code" + comment.contents))
176 expect(sourceMappingURL.existsIn(comment.contents))
183 it("returns false if no comment", function() {
184 expect(sourceMappingURL.existsIn("code"))
189 it("is detachable", function() {
190 var has = sourceMappingURL.existsIn
191 expect(has("/*# sourceMappingURL=foo */"))
198 describe(".removeFrom", function() {
200 forEachComment(function(comment, description) {
202 it("removes the comment for " + description, function() {
203 expect(sourceMappingURL.removeFrom("code\n" + comment))
206 expect(sourceMappingURL.removeFrom("code" + comment))
209 expect(sourceMappingURL.removeFrom(comment))
215 forEachNonTrailingComment(function(comment, description) {
217 it("removes the comment for " + description, function() {
218 expect(sourceMappingURL.removeFrom("code\n" + comment.contents))
219 .to.equal("code\n" + comment.solution)
221 expect(sourceMappingURL.removeFrom("code" + comment.contents))
222 .to.equal("code" + comment.solution)
224 expect(sourceMappingURL.removeFrom(comment.contents))
225 .to.equal(comment.solution)
231 it("does nothing if no comment", function() {
232 expect(sourceMappingURL.removeFrom("code\n"))
237 it("is detachable", function() {
238 var remove = sourceMappingURL.removeFrom
239 expect(remove("/*# sourceMappingURL=foo */"))
246 describe(".insertBefore", function() {
248 forEachComment(function(comment, description) {
250 it("inserts a string before the comment for " + description, function() {
251 expect(sourceMappingURL.insertBefore("code\n" + comment, "more code\n"))
252 .to.equal("code\nmore code\n" + comment)
254 expect(sourceMappingURL.insertBefore("code" + comment, "\nmore code"))
255 .to.equal("code\nmore code" + comment)
257 expect(sourceMappingURL.insertBefore(comment, "some code"))
258 .to.equal("some code" + comment)
264 it("inserts a string before an embedded comment", function() {
265 expect(sourceMappingURL.insertBefore("/*! Library Name v1.0.0\n" +
266 "//# sourceMappingURL=foo.js.map\n*/\n(function(){})", "code\n"))
267 .to.equal("/*! Library Name v1.0.0\ncode\n" +
268 "//# sourceMappingURL=foo.js.map\n*/\n(function(){})")
272 it("inserts a string before a leading comment", function() {
273 expect(sourceMappingURL.insertBefore("//# sourceMappingURL=foo.js.map\n" +
274 "(function(){})", "code\n"))
275 .to.equal("code\n//# sourceMappingURL=foo.js.map\n" +
280 it("appends if no comment", function() {
281 expect(sourceMappingURL.insertBefore("code", "\nmore code"))
282 .to.equal("code\nmore code")
286 it("is detachable", function() {
287 var insertBefore = sourceMappingURL.insertBefore
288 expect(insertBefore("/*# sourceMappingURL=foo */", "bar"))
289 .to.equal("bar/*# sourceMappingURL=foo */")
295 describe(".regex", function() {
297 it("includes ._innerRegex", function() {
298 expect(sourceMappingURL.regex.source)
299 .to.contain(sourceMappingURL._innerRegex.source)
303 var match = function(code) {
305 .to.match(sourceMappingURL.regex)
308 var noMatch = function(code) {
310 .not.to.match(sourceMappingURL.regex)
314 forEachComment(function(comment, description) {
316 it("matches " + description, function() {
317 match("code\n" + comment)
318 match("code" + comment)
323 it("matches " + description + ", with trailing whitespace", function() {
325 match(comment + "\n")
326 match(comment + "\n\n\t\n \t ")
332 it("does not match some cases that are easy to mess up", function() {
334 "/* # sourceMappingURL=foo */"
338 "// # sourceMappingURL=foo"
343 it("is liberal regarding inner whitespace", function() {
345 "/*# sourceMappingURL=foo*/"
349 "/*# sourceMappingURL=foo */"
353 "/*# sourceMappingURL=foo \t\n" +
359 "# sourceMappingURL=foo\n" +
365 "# sourceMappingURL=foo\n" +
371 "# sourceMappingURL=foo\n" +
381 describe("._innerRegex", function() {
383 it("matches the contents of sourceMappingURL comments", function() {
384 expect("# sourceMappingURL=http://www.example.com/foo/bar.js.map")
385 .to.match(sourceMappingURL._innerRegex)
389 it("captures the url in the first capture group", function() {
390 expect(sourceMappingURL._innerRegex.exec("# sourceMappingURL=foo")[1])
395 it("supports the legacy syntax", function() {
396 expect("@ sourceMappingURL=http://www.example.com/foo/bar.js.map")
397 .to.match(sourceMappingURL._innerRegex)