|
6 | 6 |
|
7 | 7 | import assert from 'assert/strict';
|
8 | 8 |
|
9 |
| -import FontSizeGather, { |
10 |
| - computeSelectorSpecificity, getEffectiveFontRule, |
11 |
| -} from '../../../../gather/gatherers/seo/font-size.js'; |
| 9 | +import FontSizeGather, {getEffectiveFontRule} from '../../../../gather/gatherers/seo/font-size.js'; |
12 | 10 |
|
13 | 11 | let fontSizeGather;
|
14 | 12 |
|
@@ -178,59 +176,6 @@ describe('Font size gatherer', () => {
|
178 | 176 | });
|
179 | 177 | });
|
180 | 178 |
|
181 |
| - describe('#computeSelectorSpecificity', () => { |
182 |
| - const compute = computeSelectorSpecificity; |
183 |
| - |
184 |
| - it('should handle basic selectors', () => { |
185 |
| - expect(compute('h1')).toEqual(1); |
186 |
| - expect(compute('h1 > p > span')).toEqual(3); |
187 |
| - }); |
188 |
| - |
189 |
| - it('should handle class selectors', () => { |
190 |
| - expect(compute('h1.foo')).toEqual(11); |
191 |
| - expect(compute('.foo')).toEqual(10); |
192 |
| - expect(compute('h1 > p.other.yeah > span')).toEqual(23); |
193 |
| - }); |
194 |
| - |
195 |
| - it('should handle ID selectors', () => { |
196 |
| - expect(compute('h1#awesome.foo')).toEqual(111); |
197 |
| - expect(compute('#awesome.foo')).toEqual(110); |
198 |
| - expect(compute('#awesome')).toEqual(100); |
199 |
| - expect(compute('h1 > p.other > span#the-text')).toEqual(113); |
200 |
| - }); |
201 |
| - |
202 |
| - it('should ignore the univeral selector', () => { |
203 |
| - expect(compute('.foo')).toEqual(10); |
204 |
| - expect(compute('* .foo')).toEqual(10); |
205 |
| - expect(compute('.foo *')).toEqual(10); |
206 |
| - }); |
207 |
| - |
208 |
| - // Examples https://drafts.csswg.org/selectors-3/#specificity |
209 |
| - it('should handle l3 spec selectors', () => { |
210 |
| - expect(compute('*')).toEqual(0); |
211 |
| - expect(compute('LI')).toEqual(1); |
212 |
| - expect(compute('UL LI')).toEqual(2); |
213 |
| - expect(compute('UL OL+LI')).toEqual(3); |
214 |
| - // expect(compute('H1 + *[REL=up]')).toEqual(11); // TODO: Handle attribute selectors |
215 |
| - expect(compute('UL OL LI.red')).toEqual(13); |
216 |
| - expect(compute('LI.red.level')).toEqual(21); |
217 |
| - expect(compute('#x34y')).toEqual(100); |
218 |
| - // expect(compute('#s12:not(FOO)')).toEqual(101); // TODO: Handle pseudo selectors |
219 |
| - }); |
220 |
| - |
221 |
| - // Examples from https://drafts.csswg.org/selectors-4/#specificity-rules |
222 |
| - it('should handle l4 spec selectors', () => { |
223 |
| - expect(compute(':is(em, #foo)')).toEqual(100); |
224 |
| - // expect(compute('.qux:where(em, #foo#bar#baz)')).toEqual(10); // TODO: Handle pseudo selectors |
225 |
| - // expect(compute(':nth-child(even of li, .item)')).toEqual(20); // TODO: Handle pseudo selectors |
226 |
| - // expect(compute(':not(em, strong#foo)')).toEqual(101); // TODO: Handle pseudo selectors |
227 |
| - }); |
228 |
| - |
229 |
| - it('should cap the craziness', () => { |
230 |
| - expect(compute('h1.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p')).toEqual(91); |
231 |
| - }); |
232 |
| - }); |
233 |
| - |
234 | 179 | describe('#getEffectiveFontRule', () => {
|
235 | 180 | const createProps = props => Object.entries(props).map(([name, value]) => ({name, value}));
|
236 | 181 | const createStyle = ({properties, id}) => ({
|
@@ -366,29 +311,29 @@ describe('Font size gatherer', () => {
|
366 | 311 | style: createStyle({id: 1, properties: {'font-size': '1em'}}),
|
367 | 312 | });
|
368 | 313 |
|
369 |
| - // Two matching selectors where 2nd is the global most specific, ID + class |
| 314 | + // Just ID |
370 | 315 | const fontRuleB = createRule({
|
371 | 316 | origin: 'regular',
|
372 |
| - selectors: ['html body *', '#main.foo'], |
373 |
| - style: createStyle({id: 2, properties: {'font-size': '2em'}}), |
| 317 | + selectors: ['#main'], |
| 318 | + style: createStyle({id: 2, properties: {'font-size': '3em'}}), |
374 | 319 | });
|
375 | 320 |
|
376 |
| - // Just ID |
| 321 | + // Two matching selectors where 2nd is the global most specific, ID + class |
377 | 322 | const fontRuleC = createRule({
|
378 | 323 | origin: 'regular',
|
379 |
| - selectors: ['#main'], |
380 |
| - style: createStyle({id: 3, properties: {'font-size': '3em'}}), |
| 324 | + selectors: ['html body *', '#main.foo'], |
| 325 | + style: createStyle({id: 3, properties: {'font-size': '2em'}}), |
381 | 326 | });
|
382 | 327 |
|
383 | 328 | const matchedCSSRules = [
|
384 | 329 | {rule: fontRuleA, matchingSelectors: [0]},
|
385 |
| - {rule: fontRuleB, matchingSelectors: [0, 1]}, |
386 |
| - {rule: fontRuleC, matchingSelectors: [0]}, |
| 330 | + {rule: fontRuleB, matchingSelectors: [0]}, |
| 331 | + {rule: fontRuleC, matchingSelectors: [0, 1]}, |
387 | 332 | ];
|
388 | 333 |
|
389 | 334 | const result = getEffectiveFontRule({matchedCSSRules});
|
390 |
| - // fontRuleB should have one for ID + class |
391 |
| - expect(result.styleSheetId).toEqual(2); |
| 335 | + // fontRuleC should have one for ID + class |
| 336 | + expect(result.styleSheetId).toEqual(3); |
392 | 337 | });
|
393 | 338 |
|
394 | 339 | it('should break ties with last one declared', () => {
|
|
0 commit comments