UNPKG

1.22 kBJavaScriptView Raw
1var attrRE = /([\w-]+)|['"]{1}([^'"]*)['"]{1}/g;
2
3// create optimized lookup object for
4// void elements as listed here:
5// http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
6var lookup = (Object.create) ? Object.create(null) : {};
7lookup.area = true;
8lookup.base = true;
9lookup.br = true;
10lookup.col = true;
11lookup.embed = true;
12lookup.hr = true;
13lookup.img = true;
14lookup.input = true;
15lookup.keygen = true;
16lookup.link = true;
17lookup.menuitem = true;
18lookup.meta = true;
19lookup.param = true;
20lookup.source = true;
21lookup.track = true;
22lookup.wbr = true;
23
24module.exports = function (tag) {
25 var i = 0;
26 var key;
27 var res = {
28 type: 'tag',
29 name: '',
30 voidElement: false,
31 attrs: {},
32 children: []
33 };
34
35 tag.replace(attrRE, function (match) {
36 if (i % 2) {
37 key = match;
38 } else {
39 if (i === 0) {
40 if (lookup[match] || tag.charAt(tag.length - 2) === '/') {
41 res.voidElement = true;
42 }
43 res.name = match;
44 } else {
45 res.attrs[key] = match.replace(/['"]/g, '');
46 }
47 }
48 i++;
49 });
50
51 return res;
52};