-
Notifications
You must be signed in to change notification settings - Fork 302
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
Proposal: Element.assign() #477
Comments
This seems like a duplicate of #150. |
They are at least similar, @annevk, and I'm happy to move the discussion wherever. Thank you for tracking these things. What may be unique is that I've made specific proposals using much of the same prior art originally provided in #150, and that this discussion hasn't (yet) moved to template strings. On its own merits, I hope this proposal can be endorsed, rejected, discussed, or otherwise ignored. I am also happy to produce a speculative polyfill with the desired semantics. |
I like the Idea, I just don't like it being too polymorphic specially the part: Sub-Proposal 2: Appending nodes with the second argument onward:
Sub-Proposal 3: Setting attributes from properties with the second argument onward:
I would rather have , than the "sub-proposal 3" colide with the other examples.
|
I have updated the proposal to match the suggestion of @victornpb, which is also the cowpath used by most React/JSX compatible libraries. |
Element.assign
^ or something like this name borrowed from
Object.assign
.I would prefer an easier way to create and manipulate elements.
The Element.assign() method would assign attributes and children to Elements.
node
benull
.element
is anDOMString
, then:node
to the result of invokingcreateElement
usingelement
aslocalName
.node
toelement
.name
andvalue
inproperties
:name
is a specified property innode
or its prototype chain:node
’sname
property tovalue
.value
isnull
:removeAttribute
usingname
asqualifiedName
.setAttribute
usingname
asqualifiedName
andvalue
asvalue
.child
be the result of convertingnodes
into a node given nodes and context object’s node document.child
tonode
.node
.Creating an Element when the first argument is a
DOMString
:Assigning properties when the second argument is an
Object
:Assigning attributes when the second argument is an
Object
:Creating an element, assigning a property, and appending text:
Speculative Polyfill
This is not an original idea. Prior art includes libraries like the minimal Bliss.js by @LeaVerou, or Facebook’s React, and a handful of other popular libraries which all include very similar methods for creating new elements. This functionality has existed in other forms of older libraries as well, going back to at least jQuery’s innerHTML to Collection sugar (
$('<div></div>')
).The text was updated successfully, but these errors were encountered: