|
1 | | -'use strict' |
2 | | - |
3 | | -var test = require('tape') |
4 | | -var h = require('hastscript') |
5 | | -var s = require('hastscript/svg') |
6 | | -var from = require('.') |
| 1 | +import test from 'tape' |
| 2 | +import {h, s} from 'hastscript' |
| 3 | +import {fromSelector} from './index.js' |
7 | 4 |
|
8 | 5 | test('fromSelector()', function (t) { |
9 | | - t.equal(typeof from, 'function', 'should expose a function') |
10 | | - |
11 | 6 | t.throws( |
12 | 7 | function () { |
13 | | - from('@supports (transform-origin: 5% 5%) {}') |
| 8 | + fromSelector('@supports (transform-origin: 5% 5%) {}') |
14 | 9 | }, |
15 | 10 | /Error: Rule expected but "@" found/, |
16 | 11 | 'should throw w/ invalid selector' |
17 | 12 | ) |
18 | 13 |
|
19 | 14 | t.throws( |
20 | 15 | function () { |
21 | | - from('a, b') |
| 16 | + fromSelector('a, b') |
22 | 17 | }, |
23 | 18 | /Error: Cannot handle selector list/, |
24 | 19 | 'should throw w/ multiple selector' |
25 | 20 | ) |
26 | 21 |
|
27 | 22 | t.throws( |
28 | 23 | function () { |
29 | | - from('a + b') |
| 24 | + fromSelector('a + b') |
30 | 25 | }, |
31 | 26 | /Error: Cannot handle sibling combinator `\+` at root/, |
32 | 27 | 'should throw w/ next-sibling combinator at root' |
33 | 28 | ) |
34 | 29 |
|
35 | 30 | t.throws( |
36 | 31 | function () { |
37 | | - from('a ~ b') |
| 32 | + fromSelector('a ~ b') |
38 | 33 | }, |
39 | 34 | /Error: Cannot handle sibling combinator `~` at root/, |
40 | 35 | 'should throw w/ subsequent-sibling combinator at root' |
41 | 36 | ) |
42 | 37 |
|
43 | 38 | t.throws( |
44 | 39 | function () { |
45 | | - from('[foo%=bar]') |
| 40 | + fromSelector('[foo%=bar]') |
46 | 41 | }, |
47 | 42 | /Error: Expected "=" but "%" found./, |
48 | 43 | 'should throw w/ attribute modifiers' |
49 | 44 | ) |
50 | 45 |
|
51 | 46 | t.throws( |
52 | 47 | function () { |
53 | | - from('[foo~=bar]') |
| 48 | + fromSelector('[foo~=bar]') |
54 | 49 | }, |
55 | 50 | /Error: Cannot handle attribute equality modifier `~=`/, |
56 | 51 | 'should throw w/ attribute modifiers' |
57 | 52 | ) |
58 | 53 |
|
59 | 54 | t.throws( |
60 | 55 | function () { |
61 | | - from(':active') |
| 56 | + fromSelector(':active') |
62 | 57 | }, |
63 | 58 | /Error: Cannot handle pseudo-selector `active`/, |
64 | 59 | 'should throw on pseudo classes' |
65 | 60 | ) |
66 | 61 |
|
67 | 62 | t.throws( |
68 | 63 | function () { |
69 | | - from(':nth-foo(2n+1)') |
| 64 | + fromSelector(':nth-foo(2n+1)') |
70 | 65 | }, |
71 | 66 | /Error: Cannot handle pseudo-selector `nth-foo`/, |
72 | 67 | 'should throw on pseudo class “functions”' |
73 | 68 | ) |
74 | 69 |
|
75 | 70 | t.throws( |
76 | 71 | function () { |
77 | | - from('::before') |
| 72 | + fromSelector('::before') |
78 | 73 | }, |
79 | 74 | /Error: Cannot handle pseudo-element or empty pseudo-class/, |
80 | 75 | 'should throw on invalid pseudo elements' |
81 | 76 | ) |
82 | 77 |
|
83 | | - t.deepEqual(from(), h(), 'should support no selector') |
84 | | - t.deepEqual(from(''), h(), 'should support the empty string') |
85 | | - t.deepEqual(from(' '), h(), 'should support whitespace only') |
86 | | - t.deepEqual(from('*'), h(), 'should support the universal selector') |
| 78 | + t.deepEqual(fromSelector(), h(''), 'should support no selector') |
| 79 | + t.deepEqual(fromSelector(''), h(''), 'should support the empty string') |
| 80 | + t.deepEqual(fromSelector(' '), h(''), 'should support whitespace only') |
| 81 | + t.deepEqual(fromSelector('*'), h(''), 'should support the universal selector') |
87 | 82 |
|
88 | 83 | t.deepEqual( |
89 | | - from('p i s'), |
| 84 | + fromSelector('p i s'), |
90 | 85 | h('p', h('i', h('s'))), |
91 | 86 | 'should support the descendant combinator' |
92 | 87 | ) |
93 | 88 |
|
94 | 89 | t.deepEqual( |
95 | | - from('p > i > s'), |
| 90 | + fromSelector('p > i > s'), |
96 | 91 | h('p', h('i', h('s'))), |
97 | 92 | 'should support the child combinator' |
98 | 93 | ) |
99 | 94 |
|
100 | 95 | t.deepEqual( |
101 | | - from('p i + s'), |
| 96 | + fromSelector('p i + s'), |
102 | 97 | h('p', [h('i'), h('s')]), |
103 | 98 | 'should support the next-sibling combinator' |
104 | 99 | ) |
105 | 100 |
|
106 | 101 | t.deepEqual( |
107 | | - from('p i ~ s'), |
| 102 | + fromSelector('p i ~ s'), |
108 | 103 | h('p', [h('i'), h('s')]), |
109 | 104 | 'should support the subsequent-sibling combinator' |
110 | 105 | ) |
111 | 106 |
|
112 | | - t.deepEqual(from('a'), h('a'), 'should support a tag name') |
113 | | - t.deepEqual(from('.a'), h('.a'), 'should support a class') |
114 | | - t.deepEqual(from('a.b'), h('a.b'), 'should support a tag and a class') |
115 | | - t.deepEqual(from('#b'), h('#b'), 'should support an id') |
116 | | - t.deepEqual(from('a#b'), h('a#b'), 'should support a tag and an id') |
117 | | - t.deepEqual(from('a#b.c.d'), h('a#b.c.d'), 'should support all together') |
118 | | - t.deepEqual(from('a#b#c'), h('a#c'), 'should use the last id') |
119 | | - t.deepEqual(from('A').tagName, 'a', 'should normalize casing') |
| 107 | + t.deepEqual(fromSelector('a'), h('a'), 'should support a tag name') |
| 108 | + t.deepEqual(fromSelector('.a'), h('.a'), 'should support a class') |
| 109 | + t.deepEqual(fromSelector('a.b'), h('a.b'), 'should support a tag and a class') |
| 110 | + t.deepEqual(fromSelector('#b'), h('#b'), 'should support an id') |
| 111 | + t.deepEqual(fromSelector('a#b'), h('a#b'), 'should support a tag and an id') |
| 112 | + t.deepEqual( |
| 113 | + fromSelector('a#b.c.d'), |
| 114 | + h('a#b.c.d'), |
| 115 | + 'should support all together' |
| 116 | + ) |
| 117 | + t.deepEqual(fromSelector('a#b#c'), h('a#c'), 'should use the last id') |
| 118 | + t.deepEqual(fromSelector('A').tagName, 'a', 'should normalize casing') |
120 | 119 |
|
121 | | - t.deepEqual(from('[a]'), h('', {a: true}), 'should support attributes (#1)') |
122 | | - t.deepEqual(from('[a=b]'), h('', {a: 'b'}), 'should support attributes (#2)') |
| 120 | + t.deepEqual( |
| 121 | + fromSelector('[a]'), |
| 122 | + h('', {a: true}), |
| 123 | + 'should support attributes (#1)' |
| 124 | + ) |
| 125 | + t.deepEqual( |
| 126 | + fromSelector('[a=b]'), |
| 127 | + h('', {a: 'b'}), |
| 128 | + 'should support attributes (#2)' |
| 129 | + ) |
123 | 130 |
|
124 | 131 | t.deepEqual( |
125 | | - from('.a.b[class=c]'), |
| 132 | + fromSelector('.a.b[class=c]'), |
126 | 133 | h('.a.b.c'), |
127 | 134 | 'should support class and class attributes' |
128 | 135 | ) |
129 | 136 |
|
130 | | - t.deepEqual(from('altGlyph').tagName, 'altglyph', 'space (#1)') |
| 137 | + t.deepEqual(fromSelector('altGlyph').tagName, 'altglyph', 'space (#1)') |
131 | 138 |
|
132 | | - t.deepEqual(from('altGlyph', 'svg').tagName, 'altGlyph', 'space (#2)') |
| 139 | + t.deepEqual(fromSelector('altGlyph', 'svg').tagName, 'altGlyph', 'space (#2)') |
133 | 140 |
|
134 | 141 | t.deepEqual( |
135 | | - from('svg altGlyph').children[0].tagName, |
| 142 | + fromSelector('svg altGlyph').children[0].tagName, |
136 | 143 | 'altGlyph', |
137 | 144 | 'space (#3)' |
138 | 145 | ) |
139 | 146 |
|
140 | 147 | t.deepEqual( |
141 | | - from('div svg + altGlyph').children[1].tagName, |
| 148 | + fromSelector('div svg + altGlyph').children[1].tagName, |
142 | 149 | 'altglyph', |
143 | 150 | 'space (#4)' |
144 | 151 | ) |
145 | 152 |
|
146 | 153 | t.deepEqual( |
147 | | - from('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10] altGlyph'), |
| 154 | + fromSelector( |
| 155 | + 'p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10] altGlyph' |
| 156 | + ), |
148 | 157 | h('p', [ |
149 | 158 | s('svg', {viewBox: '0 0 10 10'}, [ |
150 | 159 | s('circle', {cx: '10', cy: '10', r: '10'}, [s('altGlyph')]) |
|
0 commit comments