Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as nodes from '../parser/cssNodes.js';
import { MarkedString } from '../cssLanguageTypes.js';
import { Scanner } from '../parser/cssScanner.js';
import { Scanner, TokenType } from '../parser/cssScanner.js';
import * as l10n from '@vscode/l10n';
import { CSSDataManager } from '../languageFacts/dataManager.js';
import { Parser } from '../parser/cssParser.js';
Expand Down Expand Up @@ -489,16 +489,23 @@ export class SelectorPrinting {
continue elementLoop;
}

// Edge case: 'n' without integer prefix A, with B integer non-existent, is not regarded as a binary expression token.
const parser = new Parser();
// Edge case: when <an+b> is a single identifier (e.g. `n`, `-n`, `n-1`, `odd`, `even`), the parser reads
// `<an+b> of S` as a selector, so S has to be extracted from the text. The scanner skips comments and whitespace.
const pseudoSelectorText = childElements[1].getText();
parser.scanner.setSource(pseudoSelectorText);
const firstToken = parser.scanner.scan();
const secondToken = parser.scanner.scan();
const scanner = new Scanner();
scanner.setSource(pseudoSelectorText);
let anPlusBToken = scanner.scan();
if (anPlusBToken.type === TokenType.Delim && anPlusBToken.text === '+') {
anPlusBToken = scanner.scan();
}
const ofToken = scanner.scan();
const identifierAnPlusBOf = anPlusBToken.type === TokenType.Ident && /^(?:-?n(?:-\d*)?|odd|even)$/i.test(anPlusBToken.text)
&& ofToken.type === TokenType.Ident && ofToken.text.toLowerCase() === 'of';

if (firstToken.text === 'n' || (firstToken.text === '-n' && secondToken.text === 'of')) {
if (identifierAnPlusBOf) {
const parser = new Parser();
const complexSelectorListNodes: nodes.Node[] = [];
const complexSelectorText = pseudoSelectorText.slice(secondToken.offset + 2);
const complexSelectorText = pseudoSelectorText.slice(ofToken.offset + ofToken.len);
const complexSelectorArray = complexSelectorText.split(',');

for (const selector of complexSelectorArray) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/css/selectorPrinting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,50 @@ suite('CSS - MarkedStringPrinter selectors specificities', () => {
{ language: 'html', value: '<element id="foo" :nth-last-child(-n+3 of li, .important)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(odd of .li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(odd of .li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(even of #bar, .li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(even of #bar, .li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(ODD of li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(ODD of li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(n-1 of .li.li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(n-1 of .li.li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 3, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-last-child(N of .li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-last-child(N of .li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(n /*x*/ of #bar)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(n /*x*/ of #bar)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(-n /*x*/ of #bar)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(-n /*x*/ of #bar)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-child(odd /*x*/ of /*y*/ #bar)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-child(odd /*x*/ of /*y*/ #bar)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-last-child(n /*x*/ of #bar)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-last-child(n /*x*/ of #bar)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-last-child(-n /*x*/ of #bar)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-last-child(-n /*x*/ of #bar)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (2, 1, 0)'
]);
assertSelectorMarkdown(p, '#foo:nth-last-child(even /*x*/ of .li)', '#foo', [
{ language: 'html', value: '<element id="foo" :nth-last-child(even /*x*/ of .li)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 2, 0)'
]);
});

test('incomplete nth-child specificity', function () {
Expand Down