Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /dom/ranges/Range-deleteContents-in-ShadowRoot.html - WPT Dashboard Interop Dashboard
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Range.deleteContents() should work when the range container is a ShadowRoot</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
"use strict";
addEventListener("load", () => {
test(t => {
const host = document.createElement("div");
t.add_cleanup(() => host.remove());
const shadowRoot = host.attachShadow({mode: "open"});
const range = document.createRange();
range.setStart(shadowRoot, 0);
try {
range.deleteContents();
} catch (e) {
assert_true(
false,
`${t.name}: range.deleteContents() shouldn't throw exception`
);
}
assert_true(
!!host.shadowRoot,
`${t.name}: range.deleteContents() shouldn't delete the ShadowRoot`
);
assert_equals(
host.shadowRoot.innerHTML,
"",
`${t.name}: range.deleteContents() shouldn't modify the ShadowRoot`
);
}, 'Range.deleteContents() when "<div><shadow-root>{}</shadow-root></div>"');
test(t => {
const host = document.createElement("div");
t.add_cleanup(() => host.remove());
const shadowRoot = host.attachShadow({mode: "open"});
shadowRoot.innerHTML = "<span>ABC</span>";
const range = document.createRange();
range.setStart(shadowRoot, 0);
range.setEnd(shadowRoot, 1);
try {
range.deleteContents();
} catch (e) {
assert_true(
false,
`${t.name}: range.deleteContents() shouldn't throw exception`
);
}
assert_true(
!!host.shadowRoot,
`${t.name}: range.deleteContents() shouldn't delete the ShadowRoot`
);
assert_equals(
host.shadowRoot.innerHTML,
"",
`${t.name}: range.deleteContents() should delete all children of ShadowRoot`
);
}, 'Range.deleteContents() when "<div><shadow-root>{<span>ABC</span>}</shadow-root></div>"');
test(t => {
const host = document.createElement("div");
t.add_cleanup(() => host.remove());
const shadowRoot = host.attachShadow({mode: "open"});
shadowRoot.innerHTML = "<span>ABC</span><span>DEF</span>";
const range = document.createRange();
range.setStart(shadowRoot, 0);
range.setEnd(shadowRoot, 1);
try {
range.deleteContents();
} catch (e) {
assert_true(
false,
`${t.name}: range.deleteContents() shouldn't throw exception`
);
}
assert_true(
!!host.shadowRoot,
`${t.name}: range.deleteContents() shouldn't delete the ShadowRoot`
);
assert_equals(
host.shadowRoot.innerHTML,
"<span>DEF</span>",
`${t.name}: range.deleteContents() should delete only the first child of ShadowRoot`
);
}, 'Range.deleteContents() when "<div><shadow-root>{<span>ABC</span>}<span>DEF</span></shadow-root></div>"');
test(t => {
const host = document.createElement("div");
t.add_cleanup(() => host.remove());
const shadowRoot = host.attachShadow({mode: "open"});
shadowRoot.innerHTML = "<span>ABC</span><span>DEF</span>";
const range = document.createRange();
range.setStart(shadowRoot, 1);
range.setEnd(shadowRoot, 2);
try {
range.deleteContents();
} catch (e) {
assert_true(
false,
`${t.name}: range.deleteContents() shouldn't throw exception`
);
}
assert_true(
!!host.shadowRoot,
`${t.name}: range.deleteContents() shouldn't delete the ShadowRoot`
);
assert_equals(
host.shadowRoot.innerHTML,
"<span>ABC</span>",
`${t.name}: range.deleteContents() should delete only the last child of ShadowRoot`
);
}, 'Range.deleteContents() when "<div><shadow-root><span>ABC</span>{<span>DEF</span>}</shadow-root></div>"');
}, {once: true});
</script>
</head>
<body></body>
</html>