Files: 384839d826d48b42af545900fe4d88c8df62357d / lib / parse-tag.js
1326 bytesRaw
1 | // FROM: https://raw.githubusercontent.com/Matt-Esch/virtual-dom/master/virtual-hyperscript/parse-tag.js |
2 | |
3 | 'use strict' |
4 | |
5 | var split = require('browser-split') |
6 | |
7 | var classIdSplit = /([\.#]?[a-zA-Z0-9\u007F-\uFFFF_:-]+)/ |
8 | var classOrId = /^(\.|#)/ |
9 | var mcssClass = /^(\-|[A-Z])/ |
10 | |
11 | module.exports = parseTag |
12 | |
13 | function parseTag (tag, attributes, namespace) { |
14 | var noId = !(attributes.hasOwnProperty('id')) |
15 | |
16 | var tagParts = split(tag, classIdSplit) |
17 | var tagName = null |
18 | |
19 | if (classOrId.test(tagParts[1]) || mcssClass.test(tagParts[1])) { |
20 | tagName = 'div' |
21 | } |
22 | |
23 | var classes, part, type, i, id |
24 | |
25 | for (i = 0; i < tagParts.length; i++) { |
26 | part = tagParts[i] |
27 | |
28 | if (!part) { |
29 | continue |
30 | } |
31 | |
32 | type = part.charAt(0) |
33 | |
34 | if (mcssClass.test(part)) { |
35 | // handle micro-css style classes |
36 | classes = classes || [] |
37 | classes.push(part) |
38 | } else if (!tagName) { |
39 | tagName = part |
40 | } else if (type === '.') { |
41 | classes = classes || [] |
42 | classes.push(part.substring(1, part.length)) |
43 | } else if (type === '#' && noId) { |
44 | id = part.substring(1, part.length) |
45 | } |
46 | } |
47 | |
48 | return { |
49 | tagName: namespace ? tagName : tagName.toUpperCase(), |
50 | classes: classes, |
51 | id: id |
52 | } |
53 | } |
54 | |
55 | function isUpperCase (text) { |
56 | if (typeof text === 'string' && text) { |
57 | return text.toUpperCase() === text |
58 | } |
59 | } |
60 |
Built with git-ssb-web