Skip to content

Commit 3dc7124

Browse files
committed
Merge pull request #2 from rayd/fix/parse-attribute-quotes
Update the attr regex to nicely parse out attr values with quotation marks in them.
2 parents fdf537c + 49b9adb commit 3dc7124

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/parse-tag.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var attrRE = /([\w-]+)|['"]{1}([^'"]*)['"]{1}/g;
1+
var attrRE = /([\w-]+)|(['"])(.*?)\2/g;
22

33
// create optimized lookup object for
4-
// void elements as listed here:
4+
// void elements as listed here:
55
// http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
66
var lookup = (Object.create) ? Object.create(null) : {};
77
lookup.area = true;
@@ -42,7 +42,7 @@ module.exports = function (tag) {
4242
}
4343
res.name = match;
4444
} else {
45-
res.attrs[key] = match.replace(/['"]/g, '');
45+
res.attrs[key] = match.replace(/^['"]|['"]$/g, '');
4646
}
4747
}
4848
i++;

test/parse-tag.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,19 @@ test('parseTag', function (t) {
5353
children: []
5454
});
5555

56+
tag = '<div class="button another-button" onclick="do(\'something\');" onhover=\'do("something else")\'>';
57+
58+
t.deepEqual(parseTag(tag), {
59+
type: 'tag',
60+
attrs: {
61+
class: 'button another-button',
62+
onclick: 'do(\'something\');',
63+
onhover: 'do("something else")'
64+
},
65+
name: 'div',
66+
voidElement: false,
67+
children: []
68+
});
69+
5670
t.end();
5771
});

0 commit comments

Comments
 (0)