Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Allow the comment to be non-trailing
Browse files Browse the repository at this point in the history
  • Loading branch information
Synzvato committed Nov 12, 2015
1 parent ffa8f4c commit 0ec3e32
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "source-map-url",
"version": "0.3.0",
"version": "0.4.0",
"author": "Simon Lydell",
"license": "MIT",
"description": "Tools for working with sourceMappingURL comments.",
Expand Down
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
### Version 0.4.0 (2015-11-12) ###

- Changed: sourceMappingURL comments used to be matched only when placed at
the end of the script. However, since several commonly used JavaScript
libraries do not follow this convention and all popular web browsers accept
non-trailing comments, this has been revised.

So now non-trailing SourceMappingURL comments are matched as well.


### Version 0.3.0 (2014-08-16) ###

- Changed: sourceMappingURL comments used to be matched only if they appeared
Expand Down
4 changes: 2 additions & 2 deletions component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "source-map-url",
"version": "0.3.0",
"version": "0.4.0",
"author": "Simon Lydell",
"license": "MIT",
"description": "Tools for working with sourceMappingURL comments.",
Expand All @@ -15,4 +15,4 @@
"scripts": [
"source-map-url.js"
]
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "source-map-url",
"version": "0.3.0",
"version": "0.4.0",
"author": "Simon Lydell",
"license": "MIT",
"description": "Tools for working with sourceMappingURL comments.",
Expand Down Expand Up @@ -36,4 +36,4 @@
"android-browser/4"
]
}
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ such comment. Returns the updated `code`.
Inserts `string` before the sourceMappingURL comment in `code`. Appends
`string` to `code` if there is no such comment.

Lets you append something to a file without worrying about breaking the
sourceMappingURL comment (which needs to be at the end of the file).
Lets you append something to a file without worrying about burying the
sourceMappingURL comment (by keeping it at the end of the file).

### `sourceMappingURL.regex` ###

Expand Down
2 changes: 1 addition & 1 deletion source-map-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void (function(root, factory) {
"|" +
"//(?:" + innerRegex.source + ")" +
")" +
"\\s*$"
"\\s*"
)

return {
Expand Down
131 changes: 106 additions & 25 deletions test/source-map-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ var comments = {

}

var nonTrailingComments = {

jsLeading: {
contents: [
"//# sourceMappingURL=foo.js.map",
"(function(){})"
],
solution: [
"(function(){})"
]
},

mixEmbedded: {
contents: [
"/*! Library Name v1.0.0",
"//# sourceMappingURL=foo.js.map",
"*/",
"(function(){})"
],
solution: [
"/*! Library Name v1.0.0",
"*/",
"(function(){})"
]
}

}

function forEachComment(fn) {
forOf(comments, function(name, comment) {
var description = "the '" + name + "' syntax with "
Expand All @@ -37,6 +65,23 @@ function forEachComment(fn) {
})
}

function forEachNonTrailingComment(fn) {
forOf(nonTrailingComments, function(name, comment) {

var description = "the '" + name + "' syntax with "

fn({
contents: comment.contents.join("\n"),
solution: comment.solution.join("\n")
}, description + "regular newlines")

fn({
contents: comment.contents.join("\r\n"),
solution: comment.solution.join("\r\n")
}, description + "Windows newlines")
})
}

function forOf(obj, fn) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
Expand Down Expand Up @@ -65,6 +110,21 @@ describe("sourceMappingURL", function() {

})

forEachNonTrailingComment(function(comment, description) {

it("gets the url from " + description, function() {
expect(sourceMappingURL.getFrom("code\n" + comment.contents))
.to.equal("foo.js.map")

expect(sourceMappingURL.getFrom("code" + comment.contents))
.to.equal("foo.js.map")

expect(sourceMappingURL.getFrom(comment.contents))
.to.equal("foo.js.map")
})

})


it("returns null if no comment", function() {
expect(sourceMappingURL.getFrom("code"))
Expand Down Expand Up @@ -104,6 +164,21 @@ describe("sourceMappingURL", function() {

})

forEachNonTrailingComment(function(comment, description) {

it("returns true for " + description, function() {
expect(sourceMappingURL.existsIn("code\n" + comment.contents))
.to.equal(true)

expect(sourceMappingURL.existsIn("code" + comment.contents))
.to.equal(true)

expect(sourceMappingURL.existsIn(comment.contents))
.to.equal(true)
})

})


it("returns false if no comment", function() {
expect(sourceMappingURL.existsIn("code"))
Expand Down Expand Up @@ -137,6 +212,21 @@ describe("sourceMappingURL", function() {

})

forEachNonTrailingComment(function(comment, description) {

it("removes the comment for " + description, function() {
expect(sourceMappingURL.removeFrom("code\n" + comment.contents))
.to.equal("code\n" + comment.solution)

expect(sourceMappingURL.removeFrom("code" + comment.contents))
.to.equal("code" + comment.solution)

expect(sourceMappingURL.removeFrom(comment.contents))
.to.equal(comment.solution)
})

})


it("does nothing if no comment", function() {
expect(sourceMappingURL.removeFrom("code\n"))
Expand Down Expand Up @@ -171,6 +261,22 @@ describe("sourceMappingURL", function() {
})


it("inserts a string before an embedded comment", function() {
expect(sourceMappingURL.insertBefore("/*! Library Name v1.0.0\n" +
"//# sourceMappingURL=foo.js.map\n*/\n(function(){})", "code\n"))
.to.equal("/*! Library Name v1.0.0\ncode\n" +
"//# sourceMappingURL=foo.js.map\n*/\n(function(){})")
})


it("inserts a string before a leading comment", function() {
expect(sourceMappingURL.insertBefore("//# sourceMappingURL=foo.js.map\n" +
"(function(){})", "code\n"))
.to.equal("code\n//# sourceMappingURL=foo.js.map\n" +
"(function(){})")
})


it("appends if no comment", function() {
expect(sourceMappingURL.insertBefore("code", "\nmore code"))
.to.equal("code\nmore code")
Expand Down Expand Up @@ -220,20 +326,6 @@ describe("sourceMappingURL", function() {
match(comment + "\n\n\t\n \t ")
})


it("only matches " + description + " at the end of files", function() {
noMatch("code\n" + comment + " code")
noMatch("code" + comment + " code")
noMatch("code\n" + comment + "\ncode")
noMatch("code" + comment + "\ncode")
noMatch("code\n" + comment + "\n// Generated by foobar")
noMatch("code" + comment + "\n// Generated by foobar")
noMatch("alert\n('\\" + comment + "')")
noMatch("alert('\\" + comment + "')")
noMatch('alert\n("\\' + comment + '")')
noMatch('alert("\\' + comment + '")')
})

})


Expand All @@ -242,17 +334,6 @@ describe("sourceMappingURL", function() {
"/* # sourceMappingURL=foo */"
)

noMatch(
"/*\n" +
" //# sourceMappingURL=foo\n" +
"*/"
)

noMatch(
"/*//# sourceMappingURL=foo\n" +
"*/"
)

noMatch(
"// # sourceMappingURL=foo"
)
Expand Down
2 changes: 1 addition & 1 deletion x-package.json5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
name: "source-map-url",
version: "0.3.0",
version: "0.4.0",
author: "Simon Lydell",
license: "MIT",
description: "Tools for working with sourceMappingURL comments.",
Expand Down

0 comments on commit 0ec3e32

Please sign in to comment.