Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested specificity fix #311

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
40 changes: 38 additions & 2 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ class Specificity {
public attr = 0;
/** Count of tag names (`div`), and pseudo-elements (`::before`) */
public tag = 0;
public merge(specificity_to_merge: Specificity) {
this.id += specificity_to_merge.id;
this.attr += specificity_to_merge.attr;
this.tag += specificity_to_merge.tag;
}
}

export function toElement(node: nodes.SimpleSelector, parentElement?: Element | null): Element {
Expand Down Expand Up @@ -449,9 +454,40 @@ export class SelectorPrinting {

return specificity;
};
let total_specificity = new Specificity();
let initial: boolean = true;
/*
Loop past the first Ruleset.
This is to handle the case where the deepest Selector is comma separated
and the user might hover over just one of them
*/
let pastInitial: boolean = false;
while (node.parent instanceof nodes.Node) {
/*
Only Ruleset nodes are useful to calculate the ancestor selectors
Exception is the deepest selector which is a Node
*/
if (!initial && !(node instanceof nodes.RuleSet)) {
node = node.parent;
continue;
}
if (!pastInitial && node instanceof nodes.RuleSet) {
node = node.parent;
pastInitial = true;
continue;
}
let specificity: Specificity = new Specificity();
if (initial) {
specificity = calculateScore(node);
initial = false;
} else if (node instanceof nodes.RuleSet) {
specificity = calculateScore(node.getSelectors().getChild(0) ?? node.getSelectors());
}
total_specificity.merge(specificity);
node = node.parent;
}

const specificity = calculateScore(node);;
return l10n.t("[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): ({0}, {1}, {2})", specificity.id, specificity.attr, specificity.tag);
return l10n.t("[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): ({0}, {1}, {2})", total_specificity.id, total_specificity.attr, total_specificity.tag);
}

}
Expand Down
60 changes: 60 additions & 0 deletions src/test/css/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,66 @@ suite('SCSS Hover', () => {
{
contents: [
{ language: 'html', value: '<div>\n …\n <div>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 0, 2)'
]
},
'scss'
);
assertHover(
'.foo { p, .foo { |p {} } }',
{
contents: [
{ language: 'html', value: '<element class="foo">\n …\n <p>\n …\n <p>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 1, 2)'
]
},
'scss'
);
assertHover(
'.foo { p, .fo|o { |p {} } }',
{
contents: [
{ language: 'html', value: '<element class="foo">\n …\n <element class="foo">' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 2, 0)'
]
},
'scss'
);
assertHover(
'input { &:hover, &:focus { &::|placeholder {} } }',
{
contents: [
{ language: 'html', value: '<input :hover ::placeholder>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 1, 2)'
]
},
'scss'
);
assertHover(
'input { &:hover, &:fo|cus { &::placeholder {} } }',
{
contents: [
{ language: 'html', value: '<input :focus>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 1, 1)'
]
},
'scss'
);
assertHover(
'input { &:ho|ver, &:focus { &::placeholder {} } }',
{
contents: [
{ language: 'html', value: '<input :hover>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 1, 1)'
]
},
'scss'
);
assertHover(
'in|put { &:hover, &:focus { &::placeholder {} } }',
{
contents: [
{ language: 'html', value: '<input>' },
'[Selector Specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity): (0, 0, 1)'
]
},
Expand Down