Files: 2244524e466413910788cad98baa61df4f340866 / lib / parse-tag.js
1049 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 notClassId = /^\.|#/ |
9 | |
10 | module.exports = parseTag |
11 | |
12 | function parseTag (tag, attributes, namespace) { |
13 | if (!tag) { |
14 | return 'DIV' |
15 | } |
16 | |
17 | var noId = !(attributes.hasOwnProperty('id')) |
18 | |
19 | var tagParts = split(tag, classIdSplit) |
20 | var tagName = null |
21 | |
22 | if (notClassId.test(tagParts[1])) { |
23 | tagName = 'DIV' |
24 | } |
25 | |
26 | var classes, part, type, i, id |
27 | |
28 | for (i = 0; i < tagParts.length; i++) { |
29 | part = tagParts[i] |
30 | |
31 | if (!part) { |
32 | continue |
33 | } |
34 | |
35 | type = part.charAt(0) |
36 | |
37 | if (!tagName) { |
38 | tagName = part |
39 | } else if (type === '.') { |
40 | classes = classes || [] |
41 | classes.push(part.substring(1, part.length)) |
42 | } else if (type === '#' && noId) { |
43 | id = part.substring(1, part.length) |
44 | } |
45 | } |
46 | |
47 | return { |
48 | tagName: namespace ? tagName : tagName.toUpperCase(), |
49 | classes: classes, |
50 | id: id |
51 | } |
52 | } |
53 |
Built with git-ssb-web