add some docs, rename to textContent and innerHTML

pull/2996/head
Rich Harris 6 years ago
parent dddc69ec7f
commit 86c5086c54

@ -510,6 +510,14 @@ When the value of an `<option>` matches its text content, the attribute can be o
</select> </select>
``` ```
---
Elements with the `contenteditable` attribute support `innerHTML` and `textContent` bindings.
```html
<div contenteditable="true" bind:innerHTML={html}></div>
```
##### Media element bindings ##### Media element bindings
--- ---

@ -608,8 +608,8 @@ export default class Element extends Node {
}); });
} }
} else if ( } else if (
name === 'text' || name === 'textContent' ||
name === 'html' name === 'innerHTML'
) { ) {
const contenteditable = this.attributes.find( const contenteditable = this.attributes.find(
(attribute: Attribute) => attribute.name === 'contenteditable' (attribute: Attribute) => attribute.name === 'contenteditable'
@ -618,7 +618,7 @@ export default class Element extends Node {
if (!contenteditable) { if (!contenteditable) {
component.error(binding, { component.error(binding, {
code: `missing-contenteditable-attribute`, code: `missing-contenteditable-attribute`,
message: `'contenteditable' attribute is required for text and html two-way bindings` message: `'contenteditable' attribute is required for texContent and innerHTML two-way bindings`
}); });
} else if (contenteditable && !contenteditable.is_static) { } else if (contenteditable && !contenteditable.is_static) {
component.error(contenteditable, { component.error(contenteditable, {

@ -133,11 +133,11 @@ export default class BindingWrapper {
break; break;
} }
case 'text': case 'textContent':
update_conditions.push(`${this.snippet} !== ${parent.var}.textContent`); update_conditions.push(`${this.snippet} !== ${parent.var}.textContent`);
break; break;
case 'html': case 'innerHTML':
update_conditions.push(`${this.snippet} !== ${parent.var}.innerHTML`); update_conditions.push(`${this.snippet} !== ${parent.var}.innerHTML`);
break; break;
@ -170,7 +170,7 @@ export default class BindingWrapper {
); );
} }
if (this.node.name === 'html' || this.node.name === 'text') { if (this.node.name === 'innerHTML' || this.node.name === 'textContent') {
block.builders.mount.add_block(`if (${this.snippet} !== void 0) ${update_dom}`); block.builders.mount.add_block(`if (${this.snippet} !== void 0) ${update_dom}`);
} else if (!/(currentTime|paused)/.test(this.node.name)) { } else if (!/(currentTime|paused)/.test(this.node.name)) {
block.builders.mount.add_block(update_dom); block.builders.mount.add_block(update_dom);
@ -208,14 +208,6 @@ function get_dom_updater(
return `${element.var}.checked = ${condition};`; return `${element.var}.checked = ${condition};`;
} }
if (binding.node.name === 'text') {
return `${element.var}.textContent = ${binding.snippet};`;
}
if (binding.node.name === 'html') {
return `${element.var}.innerHTML = ${binding.snippet};`;
}
return `${element.var}.${binding.node.name} = ${binding.snippet};`; return `${element.var}.${binding.node.name} = ${binding.snippet};`;
} }
@ -328,14 +320,6 @@ function get_value_from_dom(
return `@time_ranges_to_array(this.${name})`; return `@time_ranges_to_array(this.${name})`;
} }
if (name === 'text') {
return `this.textContent`;
}
if (name === 'html') {
return `this.innerHTML`;
}
// everything else // everything else
return `this.${name}`; return `this.${name}`;
} }

@ -30,7 +30,7 @@ const events = [
{ {
event_names: ['input'], event_names: ['input'],
filter: (node: Element, name: string) => filter: (node: Element, name: string) =>
(name === 'text' || name === 'html') && (name === 'textContent' || name === 'innerHTML') &&
node.attributes.some(attribute => attribute.name === 'contenteditable') node.attributes.some(attribute => attribute.name === 'contenteditable')
}, },
{ {
@ -515,8 +515,8 @@ export default class ElementWrapper extends Wrapper {
group.bindings.find(binding => { group.bindings.find(binding => {
return ( return (
binding.node.name === 'indeterminate' || binding.node.name === 'indeterminate' ||
binding.node.name === 'text' || binding.node.name === 'textContent' ||
binding.node.name === 'html' || binding.node.name === 'innerHTML' ||
binding.is_readonly_media_attribute() binding.is_readonly_media_attribute()
); );
}) })

@ -154,9 +154,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
if (name === 'group') { if (name === 'group') {
// TODO server-render group bindings // TODO server-render group bindings
} else if (contenteditable && (name === 'text' || name === 'html')) { } else if (contenteditable && (name === 'textContent' || name === 'innerHTML')) {
node_contents = snip(expression); node_contents = snip(expression);
value = name === 'text' ? '@escape($$value)' : '$$value'; value = name === 'textContent' ? '@escape($$value)' : '$$value';
} else if (binding.name === 'value' && node.name === 'textarea') { } else if (binding.name === 'value' && node.name === 'textarea') {
const snippet = snip(expression); const snippet = snip(expression);
node_contents = '${(' + snippet + ') || ""}'; node_contents = '${(' + snippet + ') || ""}';

@ -2,7 +2,7 @@
export let name; export let name;
</script> </script>
<editor contenteditable="true" bind:html={name}> <editor contenteditable="true" bind:innerHTML={name}>
<b>world</b> <b>world</b>
</editor> </editor>
<p>hello {@html name}</p> <p>hello {@html name}</p>

@ -2,5 +2,5 @@
export let name; export let name;
</script> </script>
<editor contenteditable="true" bind:html={name}></editor> <editor contenteditable="true" bind:innerHTML={name}></editor>
<p>hello {@html name}</p> <p>hello {@html name}</p>

@ -2,7 +2,7 @@
export let name; export let name;
</script> </script>
<editor contenteditable="true" bind:text={name}> <editor contenteditable="true" bind:textContent={name}>
<b>world</b> <b>world</b>
</editor> </editor>
<p>hello {name}</p> <p>hello {name}</p>

@ -2,5 +2,5 @@
export let name; export let name;
</script> </script>
<editor contenteditable="true" bind:text={name}></editor> <editor contenteditable="true" bind:textContent={name}></editor>
<p>hello {name}</p> <p>hello {name}</p>

@ -3,4 +3,4 @@
let toggle = false; let toggle = false;
</script> </script>
<editor contenteditable={toggle} bind:html={name}></editor> <editor contenteditable={toggle} bind:innerHTML={name}></editor>

@ -1,6 +1,6 @@
[{ [{
"code": "missing-contenteditable-attribute", "code": "missing-contenteditable-attribute",
"message": "'contenteditable' attribute is required for text and html two-way bindings", "message": "'contenteditable' attribute is required for textContent and innerHTML two-way bindings",
"start": { "start": {
"line": 4, "line": 4,
"column": 8, "column": 8,

@ -1,4 +1,4 @@
<script> <script>
export let name; export let name;
</script> </script>
<editor bind:text={name}></editor> <editor bind:textContent={name}></editor>

Loading…
Cancel
Save