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

Fix handling of compound ::slotted selectors #1575

Merged
merged 2 commits into from
Feb 15, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/giant-coins-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-cascade": patch
---

**Fixed:** `::slotted` selectors within a compound selector are now correctly handled.
26 changes: 14 additions & 12 deletions packages/alfa-cascade/src/selector-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,22 @@ export namespace SelectorMap {
const add = (block: Block<Block.Source>): void => {
const keySelector = block.selector.key;

if (Selector.isShadow(block.selector)) {
// These selectors select nodes in the light tree, they are stored
// separately and need to be checked when building the cascade of
// the hosting tree, not of the same tree.
shadow.push(block);
return;
}

if (!keySelector.isSome()) {
if (Selector.isShadow(block.selector)) {
// These selectors select nodes in the light tree, they are stored
// separately and need to be checked when building the cascade of
// the hosting tree, not of the same tree.
shadow.push(block);
} else {
other.push(block);
}
} else {
const key = keySelector.get();
const buckets = { id: ids, class: classes, type: types };
buckets[key.type].add(key.name, block);
other.push(block);
return;
}

const key = keySelector.get();
const buckets = { id: ids, class: classes, type: types };
buckets[key.type].add(key.name, block);
};

const visit = (rule: Rule) => {
Expand Down
61 changes: 57 additions & 4 deletions packages/alfa-cascade/test/cascade.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ test(".get() fetches `::slotted` rules from shadow, when relevant.", (t) => {
h.rule.style("::slotted(*)", { color: "green" }),
];

const slotted1 = <div class="foo">Hello</div>;
const slotted = <div class="foo">Hello</div>;

const document = h.document([
<main>
{slotted1}
{slotted}
{h.shadow(
[
<b>
Expand All @@ -226,10 +226,10 @@ test(".get() fetches `::slotted` rules from shadow, when relevant.", (t) => {
]);
const cascade = Cascade.from(document, device);

// The rule tree has 4 items on the way to slotted1:
// The rule tree has 4 items on the way to slotted:
// The fake root, the UA rule `div { display: block }`, and the 2 relevant rules declared here.
// We thus just grab and check the path down from the fake root.
t.deepEqual(Iterable.toJSON(cascade.get(slotted1).inclusiveAncestors())[3], {
t.deepEqual(Iterable.toJSON(cascade.get(slotted).inclusiveAncestors())[3], {
// fake root
block: Block.empty().toJSON(),
children: [
Expand Down Expand Up @@ -265,6 +265,58 @@ test(".get() fetches `::slotted` rules from shadow, when relevant.", (t) => {
});
});

test(".get() matches `::slotted` rules within compound selector.", (t) => {
const rule = h.rule.style(".foo::slotted(*)", { color: "green" })

const slotted = <div>Hello</div>;

const document = h.document([
<main>
{slotted}
{h.shadow(
[<slot class="foo"></slot>],
[h.sheet([rule])],
)}
</main>,
]);
const cascade = Cascade.from(document, device);

// The rule tree has 3 items on the way to slotted:
// The fake root, the UA rule `div { display: block }`, and the rule declared here.
// We thus just grab and check the path down from the fake root.
t.deepEqual(Iterable.toJSON(cascade.get(slotted).inclusiveAncestors())[2], {
// fake root
block: Block.empty().toJSON(),
children: [
// The <main> element also generates a node with the UA block, on a separate branch.
{
block: {
...UAblock,
source: {
...UAblock.source,
selector: {
type: "type",
specificity: { a: 0, b: 0, c: 1 },
key: "main",
name: "main",
namespace: null,
},
},
},
children: [],
},
{
// UA rule
block: UAblock,
children: [
// Actual rule, there are 58 rules in the UA sheet.
{ block: getBlock(rule, 58, 2), children: [] },
],
},
],
});
});

test(".get() correctly sort rules from different depths.", (t) => {
const outerNormalRule = h.rule.style("div", { color: "blue" });
const outerImportantRule = h.rule.style("div", {
Expand Down Expand Up @@ -373,3 +425,4 @@ test(".get() correctly sort rules from different depths.", (t) => {
],
});
});