Skip to content

Commit 4a75c3e

Browse files
committed
Merge pull request #19 from latentflip/master
Support single quotes for attributes as well as double quotes. fixes #18
2 parents 53b9cd1 + b546129 commit 4a75c3e

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

lib/attributesParser.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
*/
55
var Parser = require("fastparse");
66

7+
var processMatch = function(match, strUntilValue, name, value, index) {
8+
if(!this.isRelevantTagAttr(this.currentTag, name)) return;
9+
this.results.push({
10+
start: index + strUntilValue.length,
11+
length: value.length,
12+
value: value
13+
});
14+
};
15+
716
var parser = new Parser({
817
outside: {
918
"<!--.*?-->": true,
@@ -18,22 +27,9 @@ var parser = new Parser({
1827
inside: {
1928
"\\s+": true, // eat up whitespace
2029
">": "outside", // end of attributes
21-
"(([a-zA-Z\\-]+)\\s*=\\s*\")([^\"]*)\"": function(match, strUntilValue, name, value, index) {
22-
if(!this.isRelevantTagAttr(this.currentTag, name)) return;
23-
this.results.push({
24-
start: index + strUntilValue.length,
25-
length: value.length,
26-
value: value
27-
});
28-
},
29-
"(([a-zA-Z\\-]+)\\s*=\\s*)([^\\s>]+)": function(match, strUntilValue, name, value, index) {
30-
if(!this.isRelevantTagAttr(this.currentTag, name)) return;
31-
this.results.push({
32-
start: index + strUntilValue.length,
33-
length: value.length,
34-
value: value
35-
});
36-
}
30+
"(([a-zA-Z\\-]+)\\s*=\\s*\")([^\"]*)\"": processMatch,
31+
"(([a-zA-Z\\-]+)\\s*=\\s*\')([^\']*)\'": processMatch,
32+
"(([a-zA-Z\\-]+)\\s*=\\s*)([^\\s>]+)": processMatch
3733
}
3834
});
3935

test/parserTest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function test(name, html, result) {
1414

1515
describe("parser", function() {
1616
test("normal", 'Text <img src="image.png"><img src="image2.png">', ["image.png", "image2.png"]);
17+
test("single-quotes", "Text <img src='image.png'><img src='image2.png'>", ["image.png", "image2.png"]);
1718
test("whitespace", 'T ex t <img \t src = "image.png" > <img\t\nsrc\n=\n"image2.png"\n>', ["image.png", "image2.png"]);
1819
test("whitespace2", 'Text < img src="image.png" >', []);
1920
test("wrong <", 'Text <<img src="image.png">', ["image.png"]);

0 commit comments

Comments
 (0)