-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #3311 --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
- Loading branch information
1 parent
a45afd5
commit f56fe33
Showing
14 changed files
with
198 additions
and
33 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
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
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
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
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
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
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
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
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
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,57 @@ | ||
// Utilities for managing contenteditable nodes | ||
import Attribute from '../nodes/Attribute'; | ||
import Element from '../nodes/Element'; | ||
|
||
export const CONTENTEDITABLE_BINDINGS = [ | ||
'textContent', | ||
'innerHTML', | ||
'innerText' | ||
]; | ||
|
||
/** | ||
* Returns true if node is an 'input' or 'textarea'. | ||
* @param {Element} node The element to be checked | ||
*/ | ||
function is_input_or_textarea(node: Element): boolean { | ||
return node.name === 'textarea' || node.name === 'input'; | ||
} | ||
|
||
/** | ||
* Check if a given attribute is 'contenteditable'. | ||
* @param {Attribute} attribute A node.attribute | ||
*/ | ||
function is_attr_contenteditable(attribute: Attribute): boolean { | ||
return attribute.name === 'contenteditable'; | ||
} | ||
|
||
/** | ||
* Check if any of a node's attributes are 'contentenditable'. | ||
* @param {Element} node The element to be checked | ||
*/ | ||
export function has_contenteditable_attr(node: Element): boolean { | ||
return node.attributes.some(is_attr_contenteditable); | ||
} | ||
|
||
/** | ||
* Returns true if node is not textarea or input, but has 'contenteditable' attribute. | ||
* @param {Element} node The element to be tested | ||
*/ | ||
export function is_contenteditable(node: Element): boolean { | ||
return !is_input_or_textarea(node) && has_contenteditable_attr(node); | ||
} | ||
|
||
/** | ||
* Returns true if a given binding/node is contenteditable. | ||
* @param {string} name A binding or node name to be checked | ||
*/ | ||
export function is_name_contenteditable(name: string): boolean { | ||
return CONTENTEDITABLE_BINDINGS.includes(name); | ||
} | ||
|
||
/** | ||
* Returns the contenteditable attribute from the node (if it exists). | ||
* @param {Element} node The element to get the attribute from | ||
*/ | ||
export function get_contenteditable_attr(node: Element): Attribute | undefined { | ||
return node.attributes.find(is_attr_contenteditable); | ||
} |
25 changes: 25 additions & 0 deletions
25
test/runtime/samples/binding-contenteditable-innertext/_config.js
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,25 @@ | ||
export default { | ||
props: { | ||
name: 'world' | ||
}, | ||
|
||
ssrHtml: ` | ||
<editor contenteditable="true">world</editor> | ||
<p>hello world</p> | ||
`, | ||
|
||
async test({ assert, component, target, window }) { | ||
// JSDom doesn't support innerText yet, so the test is not ideal | ||
// https://github.com/jsdom/jsdom/issues/1245 | ||
const el = target.querySelector('editor'); | ||
assert.equal(el.innerText, 'world'); | ||
|
||
const event = new window.Event('input'); | ||
el.innerText = 'everybody'; | ||
await el.dispatchEvent(event); | ||
assert.equal(component.name, 'everybody'); | ||
|
||
component.name = 'goodbye'; | ||
assert.equal(el.innerText, 'goodbye'); | ||
} | ||
}; |
6 changes: 6 additions & 0 deletions
6
test/runtime/samples/binding-contenteditable-innertext/main.svelte
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,6 @@ | ||
<script> | ||
export let name; | ||
</script> | ||
|
||
<editor contenteditable="true" bind:innerText={name} /> | ||
<p>hello {name}</p> |
2 changes: 1 addition & 1 deletion
2
test/validator/samples/a11y-contenteditable-element-without-child/errors.json
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
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