Skip to content

Commit 3592016

Browse files
committed
Add some tests for future features
1 parent 56ae734 commit 3592016

File tree

3 files changed

+121
-27
lines changed

3 files changed

+121
-27
lines changed

lib/pseudo.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ function getCachedNthCheck(query) {
304304
if (!fn) {
305305
const value = query.argument
306306
assert(value, 'expected `argument`')
307-
assert(value.type === 'Formula', 'expected `Formula`')
307+
308+
if (value.type !== 'Formula') {
309+
throw new Error(
310+
'Expected `nth` formula, such as `even` or `2n+1` (`of` is not yet supported)'
311+
)
312+
}
308313

309314
fn = nthCheck(value.a + 'n+' + value.b)
310315
// @ts-expect-error: cache.
@@ -364,16 +369,16 @@ function invalidPseudo() {
364369
* Query.
365370
* @param {Element} element
366371
* Element.
367-
* @param {number | undefined} _
372+
* @param {number | undefined} _1
368373
* Index of `element` in `parent`.
369-
* @param {Parents | undefined} parent
374+
* @param {Parents | undefined} _2
370375
* Parent of `element`.
371376
* @param {State} state
372377
* State.
373378
* @returns {boolean}
374379
* Whether `element` matches `query`.
375380
*/
376-
function is(query, element, _, parent, state) {
381+
function is(query, element, _1, _2, state) {
377382
assert(query.argument, 'expected `argument`')
378383
assert(query.argument.type === 'Selector', 'expected selector')
379384

readme.md

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -308,60 +308,66 @@ type Space = 'html' | 'svg'
308308
## Unsupported
309309
310310
* [ ] † `||` (column combinator)
311-
* [ ] `ns|E` (namespace type selector)
312-
* [ ] `*|E` (any namespace type selector)
313-
* [ ] `|E` (no namespace type selector)
314-
* [ ] `[ns|attr]` (namespace attribute)
315-
* [ ] `[*|attr]` (any namespace attribute)
316-
* [ ] `[|attr]` (no namespace attribute)
311+
* [ ] `ns|E` (namespace type selector)
312+
* [ ] `*|E` (any namespace type selector)
313+
* [ ] `|E` (no namespace type selector)
314+
* [ ] `[ns|attr]` (namespace attribute)
315+
* [ ] `[*|attr]` (any namespace attribute)
316+
* [ ] `[|attr]` (no namespace attribute)
317317
* [ ] ‖ `:nth-child(n of S)` (functional pseudo-class, note: scoping to
318318
parents is not supported)
319319
* [ ] ‖ `:nth-last-child(n of S)` (functional pseudo-class, note: scoping to
320320
parents is not supported)
321321
* [ ] † `:active` (pseudo-class)
322+
* [ ] † `:autofill` (pseudo-class)
323+
* [ ] † `:buffering` (pseudo-class)
324+
* [ ] § `:closed` (pseudo-class)
322325
* [ ] † `:current` (pseudo-class)
323326
* [ ] † `:current()` (functional pseudo-class)
324327
* [ ] † `:default` (pseudo-class)
325328
* [ ] † `:defined` (pseudo-class)
326-
* [ ] † `:drop` (pseudo-class)
327-
* [ ] † `:drop()` (functional pseudo-class)
328329
* [ ] † `:focus` (pseudo-class)
329330
* [ ] † `:focus-visible` (pseudo-class)
330331
* [ ] † `:focus-within` (pseudo-class)
331332
* [ ] † `:fullscreen` (pseudo-class)
332333
* [ ] † `:future` (pseudo-class)
333-
* [ ] `:host()` (functional pseudo-class)
334-
* [ ] `:host-context()` (functional pseudo-class)
334+
* [ ] § `:host()` (functional pseudo-class)
335+
* [ ] § `:host-context()` (functional pseudo-class)
335336
* [ ] † `:hover` (pseudo-class)
336-
* [ ] § `:in-range` (pseudo-class)
337+
* [ ] `:in-range` (pseudo-class)
337338
* [ ] † `:indeterminate` (pseudo-class)
338-
* [ ] § `:invalid` (pseudo-class)
339+
* [ ] `:invalid` (pseudo-class)
339340
* [ ] † `:link` (pseudo-class)
340341
* [ ] † `:local-link` (pseudo-class)
341-
* [ ] † `:nth-column()` (functional pseudo-class)
342-
* [ ] † `:nth-last-column()` (functional pseudo-class)
343-
* [ ] § `:out-of-range` (pseudo-class)
342+
* [ ] † `:modal` (pseudo-class)
343+
* [ ] † `:muted` (pseudo-class)
344+
* [ ] † `:nth-col()` (functional pseudo-class)
345+
* [ ] † `:nth-last-col()` (functional pseudo-class)
346+
* [ ] § `:open` (pseudo-class)
347+
* [ ] ‡ `:out-of-range` (pseudo-class)
344348
* [ ] † `:past` (pseudo-class)
345349
* [ ] † `:paused` (pseudo-class)
346350
* [ ] † `:placeholder-shown` (pseudo-class)
347351
* [ ] † `:playing` (pseudo-class)
348-
* [ ] ‖ `:something()` (functional pseudo-class)
352+
* [ ] † `:seeking` (pseudo-class)
353+
* [ ] † `:stalled` (pseudo-class)
349354
* [ ] † `:target` (pseudo-class)
350355
* [ ] † `:target-within` (pseudo-class)
351-
* [ ] † `:user-error` (pseudo-class)
352356
* [ ] † `:user-invalid` (pseudo-class)
353-
* [ ] § `:valid` (pseudo-class)
357+
* [ ] `:valid` (pseudo-class)
354358
* [ ] † `:visited` (pseudo-class)
359+
* [ ] † `:volume-locked` (pseudo-class)
360+
* [ ] § `:where()` (functional pseudo-class)
355361
* [ ] † `::before` (pseudo-elements: none are supported)
356362
357363
###### Notes
358364
359365
* \* — not supported in `matches`
360-
* † — needs a user, browser, interactivity, or scripting to make sense
361-
* ‡ — not supported by the underlying algorithm
362-
* § — not very interested in writing / including the code for this
363-
* — too new, the spec is still changing
364-
* requires whole CSS files, not just selectors, to make sense
366+
* † — needs a user, browser, interactivity, scripting, or whole CSS to make
367+
sense
368+
* — not very interested in writing / including the code for this
369+
* § — too new, the spec is still changing
370+
* pr wanted!
365371
* `:any()` and `:matches()` are renamed to `:is()` in CSS.
366372
367373
## Types

test/select-all.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,89 @@ test('select.selectAll()', async function (t) {
479479
[h('li', 'Bravo'), h('li', 'Delta'), h('li', 'Foxtrot')]
480480
)
481481
})
482+
483+
await t.test(
484+
'should throw on unsupported `of` syntax',
485+
async function () {
486+
assert.throws(function () {
487+
selectAll(':nth-child(odd of a)', h('span'))
488+
}, /Expected `nth` formula, such as `even` or `2n\+1` \(`of` is not yet supported\)/)
489+
}
490+
)
491+
492+
// To do: add `of Selector` syntax.
493+
// These can be deep, such as `:nth-child(even of section h1)`.
494+
// To add this, I think we need to add support to `matches` to pass a
495+
// list of ancestors (which is likely useful in and of itself).
496+
// In `:nth` stuff we can than filter nodes both on the `nth`, and the
497+
// `of` selector.
498+
// await t.test('should support `of` syntax (reference)', async function () {
499+
// const tree = h('section', [
500+
// h('p', 'Alpha'),
501+
// h('p', 'Bravo'),
502+
// h('h1', 'Charlie'),
503+
// h('p', 'Delta'),
504+
// h('p', 'Echo'),
505+
// h('p', 'Foxtrot'),
506+
// h('h1', 'Golf')
507+
// ])
508+
509+
// assert.deepEqual(selectAll(':is(h1, p):nth-child(even)', tree), [
510+
// tree.children[1],
511+
// tree.children[3],
512+
// tree.children[5]
513+
// ])
514+
// })
515+
516+
// await t.test('should support `of` syntax (#1)', async function () {
517+
// const tree = h('section', [
518+
// h('p', 'Alpha'),
519+
// h('p', 'Bravo'),
520+
// h('h1', 'Charlie'),
521+
// h('p', 'Delta'),
522+
// h('p', 'Echo'),
523+
// h('p', 'Foxtrot'),
524+
// h('h1', 'Golf')
525+
// ])
526+
527+
// assert.deepEqual(selectAll(':is(h1, p):nth-child(even of p)', tree), [
528+
// tree.children[1],
529+
// tree.children[4]
530+
// ])
531+
// })
532+
533+
// await t.test('should support `of` syntax (#2)', async function () {
534+
// const tree = h('section', [
535+
// h('p', 'Alpha'),
536+
// h('p', 'Bravo'),
537+
// h('h1', 'Charlie'),
538+
// h('p', 'Delta'),
539+
// h('p', 'Echo'),
540+
// h('p', 'Foxtrot'),
541+
// h('h1', 'Golf')
542+
// ])
543+
544+
// assert.deepEqual(selectAll(':is(h1, p):nth-child(even of h1)', tree), [
545+
// tree.children[6]
546+
// ])
547+
// })
548+
549+
// await t.test('should support `of` syntax (#3)', async function () {
550+
// const tree = h('section', [
551+
// h('p', 'Alpha'),
552+
// h('p', 'Bravo'),
553+
// h('h1', 'Charlie'),
554+
// h('p', 'Delta'),
555+
// h('p', 'Echo'),
556+
// h('p', 'Foxtrot'),
557+
// h('h1', 'Golf')
558+
// ])
559+
560+
// assert.deepEqual(
561+
// selectAll(':is(h1, p):nth-child(even of section h1)', tree),
562+
// [tree.children[6]]
563+
// )
564+
// })
482565
})
483566

484567
await t.test(':nth-last-child', async function (t) {

0 commit comments

Comments
 (0)