forked from HenrikJoreteg/html-parse-stringify
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparse-tag.js
More file actions
44 lines (40 loc) · 1.08 KB
/
parse-tag.js
File metadata and controls
44 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var attrRE = /([\w-]+)|=|(['"])([.\s\S]*?)\2/g;
var voidElements = require('void-elements');
module.exports = function (tag) {
var i = 0;
var key;
var expectingValueAfterEquals = true;
var res = {
type: 'tag',
name: '',
voidElement: false,
attrs: {},
children: []
};
tag.replace(attrRE, function (match) {
if (match === '=') {
expectingValueAfterEquals = true;
i++;
return;
}
if (!expectingValueAfterEquals) {
if (key) {
res.attrs[key] = key; // boolean attribute
}
key=match;
} else {
if (i === 0) {
if (voidElements[match] || tag.charAt(tag.length - 2) === '/') {
res.voidElement = true;
}
res.name = match;
} else {
res.attrs[key] = match; // .replace(/^['"]|['"]$/g, '');
key=undefined;
}
}
i++;
expectingValueAfterEquals = false;
});
return res;
};