pull/2992/head
Richard Harris 5 years ago
commit 8d25f5da0a

@ -607,7 +607,26 @@ export default class Element extends Node {
message: `'${binding.name}' is not a valid binding on void elements like <${this.name}>. Use a wrapper element instead`
});
}
} else if (name !== 'this') {
} else if (
name === 'text' ||
name === 'html'
){
const contenteditable = this.attributes.find(
(attribute: Attribute) => attribute.name === 'contenteditable'
);
if (!contenteditable) {
component.error(binding, {
code: `missing-contenteditable-attribute`,
message: `'contenteditable' attribute is required for text and html two-way bindings`
});
} else if (contenteditable && !contenteditable.is_static) {
component.error(contenteditable, {
code: `dynamic-contenteditable-attribute`,
message: `'contenteditable' attribute cannot be dynamic if element uses two-way binding`
});
}
} else if (name !== 'this') {
component.error(binding, {
code: `invalid-binding`,
message: `'${binding.name}' is not a valid binding`

@ -198,6 +198,14 @@ function get_dom_updater(
return `${element.var}.checked = ${condition};`;
}
if (binding.node.name === 'text') {
return `if (${binding.snippet} !== ${element.var}.textContent) ${element.var}.textContent = ${binding.snippet};`;
}
if (binding.node.name === 'html') {
return `if (${binding.snippet} !== ${element.var}.innerHTML) ${element.var}.innerHTML = ${binding.snippet};`;
}
return `${element.var}.${binding.node.name} = ${binding.snippet};`;
}
@ -310,6 +318,14 @@ function get_value_from_dom(
return `@time_ranges_to_array(this.${name})`;
}
if (name === 'text') {
return `this.textContent`;
}
if (name === 'html') {
return `this.innerHTML`;
}
// everything else
return `this.${name}`;
}

@ -27,6 +27,12 @@ const events = [
node.name === 'textarea' ||
node.name === 'input' && !/radio|checkbox|range/.test(node.get_static_attribute_value('type') as string)
},
{
event_names: ['input'],
filter: (node: Element, name: string) =>
(name === 'text' || name === 'html') &&
node.attributes.some(attribute => attribute.name === 'contenteditable')
},
{
event_names: ['change'],
filter: (node: Element, _name: string) =>

@ -53,7 +53,12 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
slot_scopes: Map<any, any>;
}) {
let opening_tag = `<${node.name}`;
let textarea_contents; // awkward special case
let node_contents; // awkward special case
const contenteditable = (
node.name !== 'textarea' &&
node.name !== 'input' &&
node.attributes.some((attribute) => attribute.name === 'contenteditable')
);
const slot = node.get_static_attribute_value('slot');
const component = node.find_nearest(/InlineComponent/);
@ -90,7 +95,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
args.push(snip(attribute.expression));
} else {
if (attribute.name === 'value' && node.name === 'textarea') {
textarea_contents = stringify_attribute(attribute, true);
node_contents = stringify_attribute(attribute, true);
} else if (attribute.is_true) {
args.push(`{ ${quote_name_if_necessary(attribute.name)}: true }`);
} else if (
@ -112,7 +117,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
if (attribute.type !== 'Attribute') return;
if (attribute.name === 'value' && node.name === 'textarea') {
textarea_contents = stringify_attribute(attribute, true);
node_contents = stringify_attribute(attribute, true);
} else if (attribute.is_true) {
opening_tag += ` ${attribute.name}`;
} else if (
@ -145,9 +150,17 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
if (name === 'group') {
// TODO server-render group bindings
} else if (contenteditable && (name === 'text' || name === 'html')) {
const snippet = snip(expression)
if (name == 'text') {
node_contents = '${@escape(' + snippet + ')}'
} else {
// Do not escape HTML content
node_contents = '${' + snippet + '}'
}
} else if (binding.name === 'value' && node.name === 'textarea') {
const snippet = snip(expression);
textarea_contents='${(' + snippet + ') || ""}';
node_contents='${(' + snippet + ') || ""}';
} else {
const snippet = snip(expression);
opening_tag += ' ${(v => v ? ("' + name + '" + (v === true ? "" : "=" + JSON.stringify(v))) : "")(' + snippet + ')}';
@ -162,8 +175,8 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
renderer.append(opening_tag);
if (node.name === 'textarea' && textarea_contents !== undefined) {
renderer.append(textarea_contents);
if ((node.name === 'textarea' || contenteditable) && node_contents !== undefined) {
renderer.append(node_contents);
} else {
renderer.render(node.children, options);
}

@ -0,0 +1,43 @@
export default {
props: {
name: '<b>world</b>',
},
html: `
<editor><b>world</b></editor>
<p>hello <b>world</b></p>
`,
ssrHtml: `
<editor contenteditable="true"><b>world</b></editor>
<p>hello <b>world</b></p>
`,
async test({ assert, component, target, window }) {
const el = target.querySelector('editor');
assert.equal(el.innerHTML, '<b>world</b>');
el.innerHTML = 'every<span>body</span>';
// No updates to data yet
assert.htmlEqual(target.innerHTML, `
<editor>every<span>body</span></editor>
<p>hello <b>world</b></p>
`);
// Handle user input
const event = new window.Event('input');
await el.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `
<editor>every<span>body</span></editor>
<p>hello every<span>body</span></p>
`);
component.name = 'good<span>bye</span>';
assert.equal(el.innerHTML, 'good<span>bye</span>');
assert.htmlEqual(target.innerHTML, `
<editor>good<span>bye</span></editor>
<p>hello good<span>bye</span></p>
`);
},
};

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

@ -0,0 +1,37 @@
export default {
props: {
name: 'world',
},
html: `
<editor>world</editor>
<p>hello world</p>
`,
ssrHtml: `
<editor contenteditable="true">world</editor>
<p>hello world</p>
`,
async test({ assert, component, target, window }) {
const el = target.querySelector('editor');
assert.equal(el.textContent, 'world');
const event = new window.Event('input');
el.textContent = 'everybody';
await el.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `
<editor>everybody</editor>
<p>hello everybody</p>
`);
component.name = 'goodbye';
assert.equal(el.textContent, 'goodbye');
assert.htmlEqual(target.innerHTML, `
<editor>goodbye</editor>
<p>hello goodbye</p>
`);
},
};

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

@ -0,0 +1,15 @@
[{
"code": "dynamic-contenteditable-attribute",
"message": "'contenteditable' attribute cannot be dynamic if element uses two-way binding",
"start": {
"line": 6,
"column": 8,
"character": 73
},
"end": {
"line": 6,
"column": 32,
"character": 97
},
"pos": 73
}]

@ -0,0 +1,6 @@
<script>
export let name;
let toggle = false;
</script>
<editor contenteditable={toggle} bind:html={name}></editor>

@ -0,0 +1,15 @@
[{
"code": "missing-contenteditable-attribute",
"message": "'contenteditable' attribute is required for text and html two-way bindings",
"start": {
"line": 4,
"column": 8,
"character": 48
},
"end": {
"line": 4,
"column": 24,
"character": 64
},
"pos": 48
}]

@ -0,0 +1,4 @@
<script>
export let name;
</script>
<editor bind:text={name}></editor>
Loading…
Cancel
Save