-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EditContext] Limit to valid element types
In w3c/edit-context#19 the Editing WG resolved that EditContext would support the same element types as shadow root, plus `<canvas>`. Update the implementation accordingly. See also the updated spec at [2]. [1] w3c/edit-context#19 [2] https://w3c.github.io/edit-context/#extensions-to-the-htmlelement-interface Bug: 999184 Change-Id: I2a8a040a279e14e9b98ece3370acdbdd8c3597ab
- Loading branch information
1 parent
aca430c
commit d46086d
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>EditContext: The HTMLElement.editContext property</title> | ||
<meta name="author" title="Dan Clark" href="mailto:daniec@microsoft.com"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src='../../html/resources/common.js'></script> | ||
</head> | ||
<body> | ||
<script> | ||
|
||
test(function () { | ||
assert_true('editContext' in HTMLElement.prototype, 'Element.prototype.editContext must exist'); | ||
assert_equals(typeof(document.createElement('div').editContext), 'object', 'An instance of div must have editContext which is an object'); | ||
}, 'Check the existence of HTMLElement.editContext'); | ||
|
||
test(function () { | ||
assert_false('editContext' in Node.prototype, 'Node.prototype.editContext must not exist'); | ||
assert_false('editContext' in Element.prototype, 'Element.prototype.editContext must not exist'); | ||
assert_false('editContext' in CharacterData.prototype, 'CharacterData.prototype.editContext must not exist'); | ||
assert_false('editContext' in Comment.prototype, 'Comment.prototype.editContext must not exist'); | ||
assert_equals(typeof(document.createComment('').editContext), 'undefined', 'An instance of comment must not have editContext'); | ||
assert_false('editContext' in Document.prototype, 'Document.prototype.editContext must not exist'); | ||
assert_equals(typeof(document.editContext), 'undefined', 'An instance of document must not have editContext which is a function'); | ||
assert_false('editContext' in DocumentFragment.prototype, 'DocumentFragment.prototype.editContext must not exist'); | ||
assert_equals(typeof((new DOMParser()).parseFromString('', 'text/html').editContext), 'undefined', 'An instance of document must not have editContext which is a function'); | ||
assert_false('editContext' in Text.prototype, 'Text.prototype.editContext must not exist'); | ||
assert_equals(typeof(document.createTextNode('').editContext), 'undefined', 'An instance of text node must not have editContext'); | ||
}, 'Nodes other than Element should not have editContext'); | ||
|
||
test(function () { | ||
assert_throws_js(TypeError, function () { | ||
document.createElement('div').editContext = "hello"; | ||
}, 'editContext must throw a TypeError when set to a string'); | ||
|
||
assert_throws_js(TypeError, function () { | ||
document.createElement('div').editContext = 42; | ||
}, 'editContext must throw a TypeError when set to a number'); | ||
|
||
assert_throws_js(TypeError, function () { | ||
document.createElement('div').editContext = document.createElement('span'); | ||
}, 'editContext must throw a TypeError when set to a node'); | ||
}, 'HTMLElement.editContext must throw a TypeError if set to something other than an EditContext'); | ||
|
||
test(function () { | ||
const EDIT_CONTEXT_ALLOWED_ELEMENTS = HTML5_SHADOW_ALLOWED_ELEMENTS.concat(['canvas']); | ||
for (const elementName of EDIT_CONTEXT_ALLOWED_ELEMENTS) { | ||
const element = document.createElement(elementName); | ||
const ec = new EditContext(); | ||
element.editContext = ec; | ||
assert_equals(element.editContext, ec, 'Getting HTMLElement.editContext should yield the same EditContext instance'); | ||
} | ||
}, 'HTMLElement.editContext can be set on the shadow root elements plus canvas.'); | ||
|
||
test(function () { | ||
// EditContext shares all of the shadow root disallowed elements except for canvas. | ||
const EDIT_CONTEXT_DISALLOWED_ELEMENTS = HTML5_SHADOW_DISALLOWED_ELEMENTS.toSpliced(HTML5_SHADOW_DISALLOWED_ELEMENTS.indexOf('canvas'), 1); | ||
for (const elementName of EDIT_CONTEXT_DISALLOWED_ELEMENTS) { | ||
const element = document.createElement(elementName); | ||
const ec = new EditContext(); | ||
assert_throws_dom('NotSupportedError', () => { | ||
element.editContext = ec; | ||
}, `Setting editContext on <${elementName}> must throw.`); | ||
assert_equals(element.editContext, null); | ||
} | ||
}, 'Setting HTMLElement.editContext must throw a NotSupportedError for disallowed elements'); | ||
|
||
</script> | ||
</body> | ||
</html> |