1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1037519
-->
<head>
<title>Test for CSSStyleRule::selectorMatchesElement</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style type="text/css">
#foo,
#bar,
#foo::before {
color: red;
}
#foo {
div& {
outline: 1px solid gold;
}
}
#foo::before,
#bar::before {
content: 'foo-before';
color: green;
}
#foo::after,
#bar::after {
content: 'foo-after';
color: blue;
}
#foo::first-line,
#bar::first-line {
text-decoration: underline;
}
#foo::first-letter,
#bar::first-letter {
font-variant: small-caps;
}
::view-transition-group(root), #bar, ::view-transition-group(*) {
animation-duration: 3600s;
}
</style>
</head>
<body>
<div id="foo">foo content</div>
<script>
const InspectorUtils = SpecialPowers.InspectorUtils;
add_task(async () => {
const element = document.querySelector("#foo");
const elementRules = InspectorUtils.getMatchingCSSRules(element);
const multiSelectorRule = elementRules[2];
is(
multiSelectorRule.selectorText,
`#foo, #bar, #foo::before`,
"Got expected multi-selector rule"
);
ok(
multiSelectorRule.selectorMatchesElement(0, element),
"Matches #foo"
);
ok(
!multiSelectorRule.selectorMatchesElement(1, element),
"Doesn't match #bar"
);
ok(
!multiSelectorRule.selectorMatchesElement(0, element, ":bogus"),
"Doesn't match #foo with a bogus pseudo"
);
ok(
!multiSelectorRule.selectorMatchesElement(2, element, ":bogus"),
"Doesn't match #foo::before with bogus pseudo"
);
ok(
!multiSelectorRule.selectorMatchesElement(0, element, ":after"),
"Does match #foo::before with the :after pseudo"
);
const nestedRule = elementRules[4];
is(nestedRule.selectorText, `div&`, "Got expected nested rule");
ok(nestedRule.selectorMatchesElement(0, element), "Matches div&");
checkPseudo(":before");
checkPseudo(":after");
checkPseudo(":first-letter");
checkPseudo(":first-line");
function checkPseudo(pseudo) {
const rule = InspectorUtils.getMatchingCSSRules(element, pseudo).at(-1);
ok(
!rule.selectorMatchesElement(0, element),
"Doesn't match without " + pseudo
);
ok(
!rule.selectorMatchesElement(1, element),
"Doesn't match without " + pseudo
);
ok(
rule.selectorMatchesElement(0, element, pseudo),
"Matches on #foo" + pseudo
);
ok(
!rule.selectorMatchesElement(1, element, pseudo),
"Doesn't match on #bar" + pseudo
);
}
info("Create a view transition");
const transition = document.startViewTransition(() => {
element.replaceChildren("updated foo content");
});
await transition.ready;
await transition.updateCallbackDone;
const viewTransitionGroupRule = InspectorUtils.getMatchingCSSRules(
document.documentElement,
"::view-transition-group(root)"
).find(rule => {
try {
return rule.selectorText === "::view-transition-group(root), #bar, ::view-transition-group(*)";
} catch {}
return false;
});
ok(
!!viewTransitionGroupRule,
"Got the expected ::view-transition-group rule"
);
ok(
viewTransitionGroupRule.selectorMatchesElement(
0,
document.documentElement,
"::view-transition-group(root)"
),
"Matches ::view-transition-group(root)"
);
ok(
!viewTransitionGroupRule.selectorMatchesElement(1, document.documentElement),
"Doesn't match #bar"
);
ok(
viewTransitionGroupRule.selectorMatchesElement(
2,
document.documentElement,
"::view-transition-group(root)"
),
"Matches ::view-transition-group(*)"
);
});
</script>
</body>
</html>
|