-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Dynamic elements implementation <svelte:element>
#5481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
alfredringstad
wants to merge
7
commits into
sveltejs:master
from
alfredringstad:dynamic-elements-implementation
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f001107
Implement svelte:element for dynamically setting HTML DOM type
4fcc681
Add add_css_class method to DynamicElement
54e1c23
Reuse DOM elements if possible. Add dev warnings when passing nullish…
392bdc6
Add documentation
7d308cb
Merge branch 'master' into dynamic-elements-implementation
bd41b85
tag -> this
45d3ab0
fix: css classes for dynamic elements
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
import Node from './shared/Node'; | ||
import Attribute from './Attribute'; | ||
import Binding from './Binding'; | ||
import EventHandler from './EventHandler'; | ||
import Let from './Let'; | ||
import TemplateScope from './shared/TemplateScope'; | ||
import { INode } from './interfaces'; | ||
import Expression from './shared/Expression'; | ||
import Component from '../Component'; | ||
import map_children from './shared/map_children'; | ||
import Class from './Class'; | ||
import Transition from './Transition'; | ||
import Animation from './Animation'; | ||
import Action from './Action'; | ||
import { string_literal } from '../utils/stringify'; | ||
import { Literal } from 'estree'; | ||
import Text from './Text'; | ||
|
||
export default class DynamicElement extends Node { | ||
type: 'DynamicElement'; | ||
name: string; | ||
tag: Expression; | ||
attributes: Attribute[] = []; | ||
actions: Action[] = []; | ||
bindings: Binding[] = []; | ||
classes: Class[] = []; | ||
handlers: EventHandler[] = []; | ||
lets: Let[] = []; | ||
intro?: Transition = null; | ||
outro?: Transition = null; | ||
animation?: Animation = null; | ||
children: INode[]; | ||
scope: TemplateScope; | ||
needs_manual_style_scoping: boolean; | ||
|
||
constructor(component: Component, parent, scope, info) { | ||
super(component, parent, scope, info); | ||
|
||
this.name = info.name; | ||
|
||
if (typeof info.tag === 'string') { | ||
this.tag = new Expression(component, this, scope, string_literal(info.tag) as Literal); | ||
} else { | ||
this.tag = new Expression(component, this, scope, info.tag); | ||
} | ||
|
||
info.attributes.forEach((node) => { | ||
switch (node.type) { | ||
case 'Action': | ||
this.actions.push(new Action(component, this, scope, node)); | ||
break; | ||
|
||
case 'Attribute': | ||
case 'Spread': | ||
this.attributes.push(new Attribute(component, this, scope, node)); | ||
break; | ||
|
||
case 'Binding': | ||
this.bindings.push(new Binding(component, this, scope, node)); | ||
break; | ||
|
||
case 'Class': | ||
this.classes.push(new Class(component, this, scope, node)); | ||
break; | ||
|
||
case 'EventHandler': | ||
this.handlers.push(new EventHandler(component, this, scope, node)); | ||
break; | ||
|
||
case 'Let': { | ||
const l = new Let(component, this, scope, node); | ||
this.lets.push(l); | ||
const dependencies = new Set([l.name.name]); | ||
|
||
l.names.forEach((name) => { | ||
scope.add(name, dependencies, this); | ||
}); | ||
break; | ||
} | ||
|
||
case 'Transition': { | ||
const transition = new Transition(component, this, scope, node); | ||
if (node.intro) this.intro = transition; | ||
if (node.outro) this.outro = transition; | ||
break; | ||
} | ||
|
||
case 'Animation': | ||
this.animation = new Animation(component, this, scope, node); | ||
break; | ||
|
||
default: | ||
throw new Error(`Not implemented: ${node.type}`); | ||
} | ||
}); | ||
|
||
this.scope = scope; | ||
|
||
this.children = map_children(component, this, this.scope, info.children); | ||
|
||
this.validate(); | ||
|
||
// TODO create BaseElement class or an interface which both DynamicElement and Element use | ||
// to resolve the hacky cast | ||
component.apply_stylesheet(this as any); | ||
} | ||
|
||
validate() { | ||
this.bindings.forEach(binding => { | ||
if (binding.name !== 'this') { | ||
this.component.error(binding, { | ||
code: 'invalid-binding', | ||
message: `'${binding.name}' is not a valid binding. svelte:element only supports bind:this` | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
add_css_class() { | ||
if (this.attributes.some(attr => attr.is_spread)) { | ||
this.needs_manual_style_scoping = true; | ||
return; | ||
} | ||
|
||
const { id } = this.component.stylesheet; | ||
|
||
const class_attribute = this.attributes.find(a => a.name === 'class'); | ||
|
||
if (class_attribute && !class_attribute.is_true) { | ||
if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') { | ||
(class_attribute.chunks[0] as Text).data += ` ${id}`; | ||
} else { | ||
(class_attribute.chunks as Node[]).push( | ||
new Text(this.component, this, this.scope, { | ||
type: 'Text', | ||
data: ` ${id}`, | ||
synthetic: true | ||
} as any) | ||
); | ||
} | ||
} else { | ||
this.attributes.push( | ||
new Attribute(this.component, this, this.scope, { | ||
type: 'Attribute', | ||
name: 'class', | ||
value: [{ type: 'Text', data: id, synthetic: true }] | ||
} as any) | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the docs generally use single quotes for JS strings
I noticed this same thing in the tests. It might be nice to cleanup there as well though less important