Skip to content

Commit 83ce38d

Browse files
committed
Merge branch 'des-des-master'
2 parents 0460814 + 358f8b3 commit 83ce38d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/parse-tag.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var attrRE = /([\w-]+)|(['"])([.\s\S]*?)\2/g;
1+
var attrRE = /([\w-]+)|=|(['"])([.\s\S]*?)\2/g;
22
var voidElements = require('void-elements');
33

44
module.exports = function (tag) {
55
var i = 0;
66
var key;
7+
var expectingValueAfterEquals = true;
78
var res = {
89
type: 'tag',
910
name: '',
@@ -13,8 +14,17 @@ module.exports = function (tag) {
1314
};
1415

1516
tag.replace(attrRE, function (match) {
16-
if (i % 2) {
17-
key = match;
17+
if (match === '=') {
18+
expectingValueAfterEquals = true;
19+
i++;
20+
return;
21+
}
22+
23+
if (!expectingValueAfterEquals) {
24+
if (key) {
25+
res.attrs[key] = key; // boolean attribute
26+
}
27+
key=match;
1828
} else {
1929
if (i === 0) {
2030
if (voidElements[match] || tag.charAt(tag.length - 2) === '/') {
@@ -23,9 +33,11 @@ module.exports = function (tag) {
2333
res.name = match;
2434
} else {
2535
res.attrs[key] = match.replace(/^['"]|['"]$/g, '');
36+
key=undefined;
2637
}
2738
}
2839
i++;
40+
expectingValueAfterEquals = false;
2941
});
3042

3143
return res;

test/parse-tag.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test('parseTag', function (t) {
6666
voidElement: false,
6767
children: []
6868
});
69-
69+
7070
tag = '<textarea placeholder=\'Hey Usher, \n\nAre these modals for real?!\' class=\'placeholder-value\'>';
7171

7272
t.deepEqual(parseTag(tag), {
@@ -80,5 +80,19 @@ test('parseTag', function (t) {
8080
children: []
8181
});
8282

83+
tag = '<input checked name="sad" type="checkbox">';
84+
85+
t.deepEqual(parseTag(tag), {
86+
type: 'tag',
87+
attrs: {
88+
type: 'checkbox',
89+
checked: 'checked',
90+
name: 'sad'
91+
},
92+
name: 'input',
93+
voidElement: true,
94+
children: []
95+
});
96+
8397
t.end();
8498
});

0 commit comments

Comments
 (0)