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
49 changes: 49 additions & 0 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,50 @@ class Specificity {
public tag = 0;
}

/**
* Renders `:has(relative-selector)` as nested HTML under the subject element.
* Descendant (implicit) and child (`>`) combinators are supported. Sibling
* combinators (`+`, `~`) keep the attribute form because they need a parent
* context the simple-selector builder does not have yet.
* Returns true when the pseudo was handled as `:has()`.
*/
function appendHasArgument(subject: Element, pseudo: nodes.Node): boolean {
const identifier = pseudo.getChild(0);
if (!identifier || !/^has$/i.test(unescape(identifier.getText()))) {
return false;
}

const selectorList = pseudo.getChildren().find(c => c.type === nodes.NodeType.SelectorList);
const firstSelector = selectorList?.getChild(0);
if (!(firstSelector instanceof nodes.Selector)) {
return false;
}

const first = firstSelector.getChild(0);
if (first && (first.matches('+') || first.matches('~'))) {
return false;
}

const startsWithCombinator = !!first && (
first.type === nodes.NodeType.SelectorCombinatorParent ||
first.type === nodes.NodeType.SelectorCombinatorShadowPiercingDescendant ||
first.type === nodes.NodeType.SelectorCombinator ||
first.type === nodes.NodeType.SelectorCombinatorSibling ||
first.type === nodes.NodeType.SelectorCombinatorAllSiblings
);

// Relative selectors without a leading combinator imply a descendant.
let attachPoint: Element = subject;
if (!startsWithCombinator) {
const ellipsis = new LabelElement('\u2026');
subject.addChild(ellipsis);
attachPoint = ellipsis;
}

new SelectorElementBuilder(attachPoint).processSelector(firstSelector);
return true;
}

export function toElement(node: nodes.SimpleSelector, parentElement?: Element | null): Element {
let result = new Element();
for (const child of node.getChildren()) {
Expand Down Expand Up @@ -280,6 +324,11 @@ export function toElement(node: nodes.SimpleSelector, parentElement?: Element |
result.addAttr('class', (<nodes.MixinDeclaration>child).getName());
break;
case nodes.NodeType.PseudoSelector:
// :has() matches relative to the subject — render the argument as nested HTML
// instead of a misleading attribute like `<article :has>`.
if (appendHasArgument(result, child)) {
break;
}
// Only unescape the name; scanning the arguments as an unquoted string truncates them.
const pseudoIdentifier = child.getChild(0);
const pseudoText = child.getText();
Expand Down
27 changes: 24 additions & 3 deletions src/test/css/selectorPrinting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ suite('CSS - MarkedStringPrinter selectors', () => {
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 0, 2)'
]);
});
test('has pseudo-class renders relative selector as nested HTML', function () {
let p = new Parser();
// microsoft/vscode#191897 — :has() must not render as a bogus HTML attribute
assertSelectorMarkdown(p, 'article:has(.alert) { }', 'article', [
{ language: 'html', value: '<article>\n …\n <element class="alert">' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)'
]);
assertSelectorMarkdown(p, 'article:has(> .alert) { }', 'article', [
{ language: 'html', value: '<article>\n <element class="alert">' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)'
]);
assertSelectorMarkdown(p, 'article:has(.a, .b) { }', 'article', [
{ language: 'html', value: '<article>\n …\n <element class="a">' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)'
]);
// Sibling relative selectors keep the attribute form (need parent context)
assertSelectorMarkdown(p, 'article:has(+ .sib) { }', 'article', [
{ language: 'html', value: '<article :has(+ .sib)>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)'
]);
});
test('sibling selector referenced by a nested parent selector', function () {
const assertHtml = (p: Parser, input: string, expected: string) => {
const selector = doParse(p, input, '&');
Expand Down Expand Up @@ -290,15 +311,15 @@ suite('CSS - MarkedStringPrinter selectors specificities', () => {
]);

assertSelectorMarkdown(p, '#s12:has(foo)', '#s12', [
{ language: 'html', value: '<element id="s12" :has(foo)>' },
{ language: 'html', value: '<element id="s12">\n …\n <foo>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)'
]);
assertSelectorMarkdown(p, '#s12:has(foo > foo)', '#s12', [
{ language: 'html', value: '<element id="s12" :has(foo > foo)>' },
{ language: 'html', value: '<element id="s12">\n …\n <foo>\n <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: '<element id="s12" :has(foo > foo, .bar > baz)>' },
{ language: 'html', value: '<element id="s12">\n …\n <foo>\n <foo>' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)'
]);

Expand Down