Skip to content

Commit 46576f7

Browse files
committed
Prevents hash fragment in URLs from being parsed
1 parent 90e6ddd commit 46576f7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var htmlMinifier = require("html-minifier");
66
var attrParse = require("./lib/attributesParser");
77
var SourceNode = require("source-map").SourceNode;
88
var loaderUtils = require("loader-utils");
9+
var url = require("url");
910

1011
function randomIdent() {
1112
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
@@ -35,6 +36,15 @@ module.exports = function(content) {
3536
content = [content];
3637
links.forEach(function(link) {
3738
if(!loaderUtils.isUrlRequest(link.value, root)) return;
39+
40+
var uri = url.parse(link.value);
41+
if (uri.hash !== null && uri.hash !== undefined) {
42+
uri.hash = null;
43+
link.value = uri.format();
44+
link.length = link.value.length;
45+
}
46+
47+
3848
do {
3949
var ident = randomIdent();
4050
} while(data[ident]);

test/loaderTest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ describe("loader", function() {
5353
'module.exports = "Text <img src=\\"" + require("/test/image.png") + "\\">";'
5454
);
5555
});
56+
it("should ignore hash fragments in URLs", function() {
57+
loader.call({}, '<img src="icons.svg#hash">').should.be.eql(
58+
'module.exports = "<img src=\\"" + require("./icons.svg") + "#hash\\">";'
59+
);
60+
});
5661
});

0 commit comments

Comments
 (0)