-
Notifications
You must be signed in to change notification settings - Fork 45
/
parse-tag.js
55 lines (42 loc) · 1018 Bytes
/
parse-tag.js
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
45
46
47
48
49
50
51
52
53
54
55
/* eslint-disable complexity, max-statements */
'use strict';
var classIdSplit = /([\.#]?[a-zA-Z0-9_:-]+)/;
var notClassId = /^\.|#/;
module.exports = parseTag;
function parseTag(tag, props) {
if (!tag) {
return 'div';
}
var noId = !('id' in props);
var tagParts = tag.split(classIdSplit);
var tagName = null;
if (notClassId.test(tagParts[1])) {
tagName = 'div';
}
var classes;
var part;
var type;
var i;
for (i = 0; i < tagParts.length; i++) {
part = tagParts[i];
if (!part) {
continue;
}
type = part.charAt(0);
if (!tagName) {
tagName = part;
} else if (type === '.') {
classes = classes || [];
classes.push(part.substring(1, part.length));
} else if (type === '#' && noId) {
props.id = part.substring(1, part.length);
}
}
if (classes) {
if (props.className) {
classes.push(props.className);
}
props.className = classes.join(' ');
}
return tagName ? tagName.toLowerCase() : 'div';
}