|
1 | | -/** |
2 | | - * @author Titus Wormer |
3 | | - * @copyright 2016 Titus Wormer |
4 | | - * @license MIT |
5 | | - * @module hast:util:parse-selector |
6 | | - * @fileoverview Parse a simple CSS selector to a HAST node. |
7 | | - */ |
8 | | - |
9 | 1 | 'use strict'; |
10 | 2 |
|
11 | | -/* eslint-env commonjs */ |
12 | | - |
13 | | -/* |
14 | | - * Characters |
15 | | - */ |
| 3 | +/* Expose. */ |
| 4 | +module.exports = parse; |
16 | 5 |
|
17 | | -var CC_DOT = '.'.charCodeAt(0); |
18 | | -var CC_HASH = '#'.charCodeAt(0); |
| 6 | +/* Characters */ |
| 7 | +var dot = '.'.charCodeAt(0); |
| 8 | +var hash = '#'.charCodeAt(0); |
19 | 9 |
|
20 | | -/** |
21 | | - * Parse a simple CSS selector into a HAST node. |
22 | | - * |
23 | | - * @example |
24 | | - * parse('foo#bar.baz.qux'); |
25 | | - * // { |
26 | | - * // 'type': 'element', |
27 | | - * // 'tagName': 'foo', |
28 | | - * // 'properties': { |
29 | | - * // 'id': 'bar', |
30 | | - * // 'className': ['baz', 'qux'] |
31 | | - * // }, |
32 | | - * // 'children': [] |
33 | | - * // } |
34 | | - * |
35 | | - * @param {string?} selector - Simple CSS selector. |
36 | | - * @return {Node} - HAST node. |
37 | | - */ |
| 10 | +/* Parse a simple CSS selector into a HAST node. */ |
38 | 11 | function parse(selector) { |
39 | | - var id = null; |
40 | | - var className = []; |
41 | | - var value = selector || ''; |
42 | | - var name = 'div'; |
43 | | - var node; |
44 | | - var type = null; |
45 | | - var index = -1; |
46 | | - var code; |
47 | | - var length = value.length; |
48 | | - var subvalue; |
49 | | - var lastIndex; |
50 | | - |
51 | | - node = { |
52 | | - 'type': 'element', |
53 | | - 'tagName': null, |
54 | | - 'properties': {}, |
55 | | - 'children': [] |
56 | | - }; |
57 | | - |
58 | | - type = null; |
59 | | - |
60 | | - while (++index <= length) { |
61 | | - code = value.charCodeAt(index); |
62 | | - |
63 | | - if (!code || code === CC_DOT || code === CC_HASH) { |
64 | | - subvalue = value.slice(lastIndex, index); |
65 | | - |
66 | | - if (subvalue) { |
67 | | - if (type === CC_DOT) { |
68 | | - className.push(subvalue); |
69 | | - } else if (type === CC_HASH) { |
70 | | - id = subvalue; |
71 | | - } else { |
72 | | - name = subvalue; |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - lastIndex = index + 1; |
77 | | - type = code; |
| 12 | + var id = null; |
| 13 | + var className = []; |
| 14 | + var value = selector || ''; |
| 15 | + var name = 'div'; |
| 16 | + var node; |
| 17 | + var type = null; |
| 18 | + var index = -1; |
| 19 | + var code; |
| 20 | + var length = value.length; |
| 21 | + var subvalue; |
| 22 | + var lastIndex; |
| 23 | + |
| 24 | + node = { |
| 25 | + type: 'element', |
| 26 | + tagName: null, |
| 27 | + properties: {}, |
| 28 | + children: [] |
| 29 | + }; |
| 30 | + |
| 31 | + type = null; |
| 32 | + |
| 33 | + while (++index <= length) { |
| 34 | + code = value.charCodeAt(index); |
| 35 | + |
| 36 | + if (!code || code === dot || code === hash) { |
| 37 | + subvalue = value.slice(lastIndex, index); |
| 38 | + |
| 39 | + if (subvalue) { |
| 40 | + if (type === dot) { |
| 41 | + className.push(subvalue); |
| 42 | + } else if (type === hash) { |
| 43 | + id = subvalue; |
| 44 | + } else { |
| 45 | + name = subvalue; |
78 | 46 | } |
79 | | - } |
| 47 | + } |
80 | 48 |
|
81 | | - node.tagName = name; |
82 | | - |
83 | | - if (id) { |
84 | | - node.properties.id = id; |
| 49 | + lastIndex = index + 1; |
| 50 | + type = code; |
85 | 51 | } |
| 52 | + } |
86 | 53 |
|
87 | | - if (className.length) { |
88 | | - node.properties.className = className; |
89 | | - } |
| 54 | + node.tagName = name; |
90 | 55 |
|
91 | | - return node; |
92 | | -} |
| 56 | + if (id) { |
| 57 | + node.properties.id = id; |
| 58 | + } |
93 | 59 |
|
94 | | -/* |
95 | | - * Expose. |
96 | | - */ |
| 60 | + if (className.length !== 0) { |
| 61 | + node.properties.className = className; |
| 62 | + } |
97 | 63 |
|
98 | | -module.exports = parse; |
| 64 | + return node; |
| 65 | +} |
0 commit comments