Skip to content

Commit b337e72

Browse files
committed
claim static html elements using innerHTML instead of deopt to creating the nodes
revert code update test case
1 parent 3a238fe commit b337e72

File tree

50 files changed

+217
-113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+217
-113
lines changed

src/compiler/compile/Component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import compiler_warnings from './compiler_warnings';
3737
import compiler_errors from './compiler_errors';
3838
import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore';
3939
import check_enable_sourcemap from './utils/check_enable_sourcemap';
40+
import Tag from './nodes/shared/Tag';
4041

4142
interface ComponentOptions {
4243
namespace?: string;
@@ -105,6 +106,8 @@ export default class Component {
105106
slots: Map<string, Slot> = new Map();
106107
slot_outlets: Set<string> = new Set();
107108

109+
tags: Tag[] = []
110+
108111
constructor(
109112
ast: Ast,
110113
source: string,
@@ -756,6 +759,7 @@ export default class Component {
756759

757760
this.hoist_instance_declarations();
758761
this.extract_reactive_declarations();
762+
this.check_if_tags_content_dynamic();
759763
}
760764

761765
post_template_walk() {
@@ -1427,6 +1431,12 @@ export default class Component {
14271431
unsorted_reactive_declarations.forEach(add_declaration);
14281432
}
14291433

1434+
check_if_tags_content_dynamic() {
1435+
this.tags.forEach(tag => {
1436+
tag.check_if_content_dynamic();
1437+
});
1438+
}
1439+
14301440
warn_if_undefined(name: string, node, template_scope: TemplateScope) {
14311441
if (name[0] === '$') {
14321442
if (name === '$' || name[1] === '$' && !is_reserved_keyword(name)) {

src/compiler/compile/nodes/Attribute.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export default class Attribute extends Node {
5959
return expression;
6060
});
6161
}
62+
63+
if (this.dependencies.size > 0) {
64+
parent.cannot_use_innerhtml();
65+
parent.not_static_content();
66+
}
6267
}
6368

6469
get_dependencies() {

src/compiler/compile/nodes/AwaitBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default class AwaitBlock extends Node {
2525

2626
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
2727
super(component, parent, scope, info);
28+
this.cannot_use_innerhtml();
29+
this.not_static_content();
2830

2931
this.expression = new Expression(component, this, scope, info.expression);
3032

src/compiler/compile/nodes/EachBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export default class EachBlock extends AbstractBlock {
3333

3434
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
3535
super(component, parent, scope, info);
36+
this.cannot_use_innerhtml();
37+
this.not_static_content();
3638

3739
this.expression = new Expression(component, this, scope, info.expression);
3840
this.context = info.context.name || 'each'; // TODO this is used to facilitate binding; currently fails with destructuring

src/compiler/compile/nodes/Element.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import map_children from './shared/map_children';
1414
import { dimensions, start_newline } from '../../utils/patterns';
1515
import fuzzymatch from '../../utils/fuzzymatch';
1616
import list from '../../utils/list';
17+
import hash from '../utils/hash';
1718
import Let from './Let';
1819
import TemplateScope from './shared/TemplateScope';
1920
import { INode } from './interfaces';
@@ -341,6 +342,23 @@ export default class Element extends Node {
341342
this.optimise();
342343

343344
component.apply_stylesheet(this);
345+
346+
if (this.parent) {
347+
if (this.actions.length > 0 ||
348+
this.animation ||
349+
this.bindings.length > 0 ||
350+
this.classes.length > 0 ||
351+
this.intro || this.outro ||
352+
this.handlers.length > 0 ||
353+
this.styles.length > 0 ||
354+
this.name === 'option' ||
355+
this.tag_expr.dynamic_dependencies().length ||
356+
component.compile_options.dev
357+
) {
358+
this.parent.cannot_use_innerhtml(); // need to use add_location
359+
this.parent.not_static_content();
360+
}
361+
}
344362
}
345363

346364
validate() {
@@ -884,6 +902,18 @@ export default class Element extends Node {
884902
}
885903
});
886904
}
905+
906+
get can_use_textcontent() {
907+
return this.is_static_content && this.children.every(node => node.type === 'Text' || node.type === 'MustacheTag');
908+
}
909+
910+
get can_optimise_to_html_string() {
911+
return !this.namespace && (this.can_use_innerhtml || this.can_use_textcontent) && this.children.length > 0;
912+
}
913+
914+
hash() {
915+
return `svelte-${hash(this.component.source.slice(this.start, this.end))}`;
916+
}
887917
}
888918

889919
function should_have_attribute(

src/compiler/compile/nodes/Head.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export default class Head extends Node {
1414
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
1515
super(component, parent, scope, info);
1616

17+
this.can_use_innerhtml = false;
18+
1719
if (info.attributes.length) {
1820
component.error(info.attributes[0], compiler_errors.invalid_attribute_head);
1921
return;

src/compiler/compile/nodes/IfBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export default class IfBlock extends AbstractBlock {
1414

1515
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
1616
super(component, parent, scope, info);
17+
this.cannot_use_innerhtml();
18+
this.not_static_content();
1719

1820
this.expression = new Expression(component, this, scope, info.expression);
1921
this.children = map_children(component, this, scope, info.children);

src/compiler/compile/nodes/InlineComponent.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default class InlineComponent extends Node {
2626
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
2727
super(component, parent, scope, info);
2828

29+
this.cannot_use_innerhtml();
30+
this.not_static_content();
31+
2932
if (info.name !== 'svelte:component' && info.name !== 'svelte:self') {
3033
const name = info.name.split('.')[0]; // accommodate namespaces
3134
component.warn_if_undefined(name, info, scope);

src/compiler/compile/nodes/KeyBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default class KeyBlock extends AbstractBlock {
1313

1414
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
1515
super(component, parent, scope, info);
16+
this.cannot_use_innerhtml();
17+
this.not_static_content();
1618

1719
this.expression = new Expression(component, this, scope, info.expression);
1820

src/compiler/compile/nodes/RawMustacheTag.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ import Tag from './shared/Tag';
22

33
export default class RawMustacheTag extends Tag {
44
type: 'RawMustacheTag'
5+
constructor(component, parent, scope, info) {
6+
super(component, parent, scope, info);
7+
this.not_static_content();
8+
}
59
}

0 commit comments

Comments
 (0)