diff --git a/src/services/selectorPrinting.ts b/src/services/selectorPrinting.ts index 5ddf0d0c..58a5fdf5 100644 --- a/src/services/selectorPrinting.ts +++ b/src/services/selectorPrinting.ts @@ -266,7 +266,11 @@ export function toElement(node: nodes.SimpleSelector, parentElement?: Element | result.addAttr('class', (child).getName()); break; case nodes.NodeType.PseudoSelector: - result.addAttr(unescape(child.getText()), ''); + // Only unescape the name; scanning the arguments as an unquoted string truncates them. + const pseudoIdentifier = child.getChild(0); + const pseudoText = child.getText(); + const identifierEnd = pseudoIdentifier ? pseudoIdentifier.end - child.offset : pseudoText.length; + result.addAttr(unescape(pseudoText.substring(0, identifierEnd)) + pseudoText.substring(identifierEnd), ''); break; case nodes.NodeType.AttributeSelector: const selector = child; diff --git a/src/test/css/hover.test.ts b/src/test/css/hover.test.ts index 34b69288..2a4af810 100644 --- a/src/test/css/hover.test.ts +++ b/src/test/css/hover.test.ts @@ -71,6 +71,14 @@ suite('CSS Hover', () => { // }); }); + test('functional pseudo selector arguments', () => { + for (const language of ['css', 'scss', 'less']) { + assertHover('.|foo:not(.bar) {}', { + contents: [{ language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 2, 0)'], + }, language); + } + }); + test('specificity', () => { assertHover('.|foo {}', { contents: [{ language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 0)'], diff --git a/src/test/css/selectorPrinting.test.ts b/src/test/css/selectorPrinting.test.ts index 9f7505ac..1b73cbfa 100644 --- a/src/test/css/selectorPrinting.test.ts +++ b/src/test/css/selectorPrinting.test.ts @@ -55,6 +55,15 @@ suite('CSS - Selector Printing', () => { assertElement(p, '[enabled]', [{ name: 'enabled', value: void 0 }]); }); + test('functional pseudo selectors preserve their arguments', function () { + const p = new Parser(); + for (const input of [':not(.class)', ':not([disabled])', ':not(.foo, .bar)', ':not(:is(.foo, .bar))', ':nth-child(2n + 1)']) { + assertElement(p, input, [{ name: input, value: '' }]); + } + assertElement(p, String.raw`:n\6ft(.class)`, [{ name: ':not(.class)', value: '' }]); + assertElement(p, String.raw`:not(.\66 oo)`, [{ name: String.raw`:not(.\66 oo)`, value: '' }]); + }); + test('simple selector', function () { let p = new Parser(); assertSelector(p, 'element { }', 'element', '{element}'); @@ -193,152 +202,152 @@ suite('CSS - MarkedStringPrinter selectors specificities', () => { '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 0, 0)' ]); assertSelectorMarkdown(p, '#s12:not(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)' ]); }); test('where specificity', function () { assertSelectorMarkdown(p, '#s12:where(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 0)' ]); assertSelectorMarkdown(p, '#s12:where(foo > foo, .bar > baz)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo, .bar > baz)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 0)' ]); }); test('has, not, is specificity', function () { assertSelectorMarkdown(p, '#s12:not(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)' ]); assertSelectorMarkdown(p, '#s12:not(foo > foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 2)' ]); assertSelectorMarkdown(p, '#s12:not(foo > foo, .bar > baz)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo, .bar > baz)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]); assertSelectorMarkdown(p, '#s12:has(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)' ]); assertSelectorMarkdown(p, '#s12:has(foo > foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 2)' ]); assertSelectorMarkdown(p, '#s12:has(foo > foo, .bar > baz)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo, .bar > baz)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]); assertSelectorMarkdown(p, '#s12:is(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)' ]); assertSelectorMarkdown(p, '#s12:is(foo > foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 2)' ]); assertSelectorMarkdown(p, '#s12:is(foo > foo, .bar > baz)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo, .bar > baz)>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]); assertSelectorMarkdown(p, '#s12:lang(en, fr)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 0)' ]); assertSelectorMarkdown(p, '#s12:is(foo > foo, :not(.bar > baz, :has(.bar > .baz)))', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: ' foo, :not(.bar > baz, :has(.bar > .baz)))>' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)' ]); }); test('nth-child, nth-last-child specificity', function () { assertSelectorMarkdown(p, '#foo:nth-child(2)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(even)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(-n + 2)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(n of.li)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(n of.li,.li.li)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 3, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(n of.li, .li.li)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 3, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-child(n of li)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]); assertSelectorMarkdown(p, '#foo:nth-child(-n+3 of li.important)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 1)' ]); assertSelectorMarkdown(p, '#foo:nth-child(-n+3 of li.important, .class1.class2.class3)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 4, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-last-child(-n+3 of li, .important)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)' ]); }); test('incomplete nth-child specificity', function () { assertSelectorMarkdown(p, ':nth-child()', ':nth-child', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 0)' ]); assertSelectorMarkdown(p, '#foo:nth-last-child()', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 0)' ]); }); test('host, host-context specificity', function () { assertSelectorMarkdown(p, '#foo:host(.foo)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)' ]); assertSelectorMarkdown(p, '#foo:host-context(foo)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]); }); test('slotted specificity', function () { assertSelectorMarkdown(p, '#foo::slotted(foo)', '#foo', [ - { language: 'html', value: '' }, + { language: 'html', value: '' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 2)' ]); });