Skip to content

Commit 2bdedb0

Browse files
committed
fix: prevent unintended :scope in selectors
refs #1101
1 parent d573648 commit 2bdedb0

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ export function* cssSelectorGenerator(
4949
// if failed to find single selector matching all elements, try to find
5050
// selector for each standalone element and join them together
5151
if (elements.length > 1) {
52+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
53+
const { maxResults: _ignored, ...elementOptions } = custom_options;
5254
yield elements
53-
.map((element) => getCssSelector(element, options))
55+
.map((element) => getCssSelector(element, elementOptions))
5456
.join(SELECTOR_SEPARATOR);
5557
foundResults++;
5658
if (foundResults >= options.maxResults) {

test/index.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ describe("CssSelectorGenerator", function () {
2727
});
2828
});
2929

30+
describe("multi-element selectors", function () {
31+
it("should not include :scope when root and useScope are not provided", () => {
32+
root.innerHTML = `<div><span></span></div><div><span></span></div>`;
33+
const elements = Array.from(root.querySelectorAll("span"));
34+
const result = getCssSelector(elements);
35+
assert.notInclude(result, ":scope");
36+
});
37+
});
38+
3039
describe("special scenarios", function () {
3140
it("should not crash on parent-less element", function () {
3241
const element = document.createElement("div");
@@ -94,7 +103,8 @@ describe("CssSelectorGenerator", function () {
94103
});
95104

96105
it("should work with mixed generated and regular classes", () => {
97-
root.innerHTML = '<div class="sc-xyz button-primary makeStyles-123"></div>';
106+
root.innerHTML =
107+
'<div class="sc-xyz button-primary makeStyles-123"></div>';
98108
const result = getCssSelector(root.firstElementChild, {
99109
ignoreGeneratedClassNames: true,
100110
});

0 commit comments

Comments
 (0)