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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
<!DOCTYPE html>
<html>
<body>
<meta name="author" title="Siye Liu" href="mailto:siliu@microsoft.com">
<meta name="assert" content="Document's caretPositionFromPoint should return a CaretPosition inside Shadow Root which is provided as argument.">
<link rel="help" href="https://www.w3.org/TR/cssom-view-1/#dom-document-caretpositionfrompoint">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
<script>
test(() => {
assert_throws_js(TypeError, () => { document.caretPositionFromPoint(5, 5, "foo"); });
assert_throws_js(TypeError, () => { document.caretPositionFromPoint(5, 5, 6); });
}, "document.caretPositionFromPoint() throws when called without the correct parameters");
test(() => {
container.setHTMLUnsafe(`<span>hello, world</span>`);
const rect = container.firstChild.getBoundingClientRect();
const characterWidth = rect.width / container.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `he|llo, world`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Text);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container.firstChild.firstChild);
assert_equals(caretPosition.offset, characterIndex);
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location");
test(() => {
container.setHTMLUnsafe(`<input value='text inside input' />`);
const rect = container.firstChild.getBoundingClientRect();
// Get x and y coordinate at left-most location inside input element.
const x = rect.left + 1;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y);
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container.firstChild);
assert_equals(caretPosition.offset, 0);
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to an input element which is the offsetNode.");
test(() => {
container.setHTMLUnsafe(`<textarea rows="2" cols="4">12345678901234567890</textarea>`);
const rect = container.firstChild.getBoundingClientRect();
// Get x and y coordinate at "1234|5678..."
const x = rect.left + 1;
const y = rect.top + rect.height * 0.75;
const caretPosition = document.caretPositionFromPoint(x, y);
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container.firstChild);
assert_equals(caretPosition.offset, 4);
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to a textarea element which is the offsetNode.");
test(() => {
container.setHTMLUnsafe(`a<div id="host"></div>b`);
const shadowRoot = host.attachShadow({mode: 'closed'});
shadowRoot.setHTMLUnsafe(`<span>hello, world</span>`);
const rect = shadowRoot.firstChild.getBoundingClientRect();
const characterWidth = rect.width / shadowRoot.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `he|llo, world`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [shadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Text);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, shadowRoot.firstChild.firstChild);
assert_equals(caretPosition.offset, characterIndex);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to a closed shadaw tree when the shadow tree is specified as an argument');
test(() => {
container.setHTMLUnsafe(`
<span>abcd</span>
<div id="host">
<template shadowrootmode=open>
<span>hello, world</span>
</template>
</div>efg`);
const shadowRoot = host.shadowRoot;
const spanElement = document.querySelector("span");
const rect = spanElement.getBoundingClientRect();
const characterWidth = rect.width / spanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `ab|cd`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [shadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Text);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, spanElement.firstChild);
assert_equals(caretPosition.offset, characterIndex);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location when the non-intersecting shadow tree is specified as an argument');
test(() => {
container.setHTMLUnsafe(`
a<div id="host">
<template shadowrootmode=open>
<input value='text inside input' />
</template>
</div>efg`);
const shadowRoot = host.shadowRoot;
const shadowRootInputElement = shadowRoot.querySelector("input");
const rect = shadowRootInputElement.getBoundingClientRect();
// Get x and y coordinate at left-most location inside input element.
const x = rect.left + 1;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [shadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, shadowRootInputElement);
assert_equals(caretPosition.offset, 0);
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to an input element when the shadow tree is specified as an argument.");
test(() => {
container.setHTMLUnsafe(`
a<div id="host">
<template shadowrootmode=open>
<input value='text inside input' />
</template>
</div>efg`);
const shadowRoot = host.shadowRoot;
const shadowRootInputElement = shadowRoot.querySelector("input");
const rect = shadowRootInputElement.getBoundingClientRect();
// Get x and y coordinate at left-most location inside input element.
const x = rect.left + 1;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: []});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container);
assert_equals(caretPosition.offset, 1);
}, "document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the input element's shadow host\'s parent when the shadow tree is not specified as an argument.");
test(() => {
container.setHTMLUnsafe(`
a<div id="host">
<template shadowrootmode=open>
<span>hello, world</span>
</template>
</div>b`);
const shadowRoot = host.shadowRoot;
const shadowRootSpanElement = shadowRoot.querySelector("span");
const rect = shadowRootSpanElement.getBoundingClientRect();
const characterWidth = rect.width / shadowRootSpanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `he|llo, world`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y);
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container);
assert_equals(caretPosition.offset, 1);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the shadow host\'s parent when the shadow tree is not specified as an argument');
test(() => {
container.setHTMLUnsafe(`
a<div id="outerHost">
<template shadowrootmode=open>
<div id="innerHost">
<template shadowrootmode=open>
<span>some text</span>
</template>
</div>
<div>world</div>
</template>
</div>b`);
const outerShadowRoot = outerHost.shadowRoot;
const innerShadowRoot = outerShadowRoot.getElementById('innerHost').shadowRoot;
const innerShadowRootSpanElement = innerShadowRoot.querySelector("span");
const rect = innerShadowRootSpanElement.getBoundingClientRect();
const characterWidth = rect.width / innerShadowRootSpanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `so|me text`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y);
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, container);
assert_equals(caretPosition.offset, 1);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the outer shadow host\'s parent when the point is in an inner shadow tree and no shadow tree is specified as an argument');
test(() => {
container.setHTMLUnsafe(`
a<div id="outerHost">
<template shadowrootmode=open>
<div id="innerHost">
<template shadowrootmode=open>
<span>some text</span>
</template>
</div>
<div>world</div>
</template>
</div>b`);
const outerShadowRoot = outerHost.shadowRoot;
const innerShadowRoot = outerShadowRoot.getElementById('innerHost').shadowRoot;
const innerShadowRootSpanElement = innerShadowRoot.querySelector("span");
const rect = innerShadowRootSpanElement.getBoundingClientRect();
const characterWidth = rect.width / innerShadowRootSpanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `so|me text`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [innerShadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Text);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, innerShadowRootSpanElement.firstChild);
assert_equals(caretPosition.offset, characterIndex);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the inner shadow tree when the point is in an inner shadow tree and the inner shadow tree is specified as an argument');
test(() => {
container.setHTMLUnsafe(`
a<div id="outerHost">
<template shadowrootmode=open>
<div id="innerHost">
<template shadowrootmode=open>
<span>some text</span>
</template>
</div>
<div>world</div>
</template>
</div>b`);
const outerShadowRoot = outerHost.shadowRoot;
const innerShadowRoot = outerShadowRoot.getElementById('innerHost').shadowRoot;
const innerShadowRootSpanElement = innerShadowRoot.querySelector("span");
const rect = innerShadowRootSpanElement.getBoundingClientRect();
const characterWidth = rect.width / innerShadowRootSpanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `so|me text`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [outerShadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Node);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, outerShadowRoot);
assert_equals(caretPosition.offset, 1);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the outer shadow tree when the point is in an inner shadow tree and the outer shadow tree is specified as an argument');
test(() => {
container.setHTMLUnsafe(`
a<div id="outerHost">
<template shadowrootmode=open>
<div id="innerHost">
<template shadowrootmode=open>
<span>some text</span>
</template>
</div>
<div>world</div>
</template>
</div>b`);
const outerShadowRoot = outerHost.shadowRoot;
const innerShadowRoot = outerShadowRoot.getElementById('innerHost').shadowRoot;
const innerShadowRootSpanElement = innerShadowRoot.querySelector("span");
const rect = innerShadowRootSpanElement.getBoundingClientRect();
const characterWidth = rect.width / innerShadowRootSpanElement.textContent.length;
const characterIndex = 2
// Get x and y coordinate at `so|me text`.
const x = rect.left + characterWidth * characterIndex;
const y = rect.top + rect.height / 2;
const caretPosition = document.caretPositionFromPoint(x, y, {shadowRoots: [innerShadowRoot, outerShadowRoot]});
assert_true(caretPosition instanceof CaretPosition);
assert_true(caretPosition.offsetNode instanceof Text);
assert_equals(typeof(caretPosition.offset), "number");
assert_equals(caretPosition.offsetNode, innerShadowRootSpanElement.firstChild);
assert_equals(caretPosition.offset, characterIndex);
}, 'document.caretPositionFromPoint() should return a CaretPosition at the specified location pointing to the inner shadow tree when the point is in an inner shadow tree and the inner shadow tree and the outer shadow tree are specified as an argument');
</script>
</body>
</html>
|