Re: Element.create(): a proposal for more convenient element creation

Element.create looks neat. Three thoughts:

First, I think Element.create *and* constructors like new
HTMLDivElement(attributes, children) are both useful. Element.create is good
when you have a tag name in hand, are creating unknown elements, or are
creating elements that don’t have a specific constructor (ins/del.)

New is good when you’re creating an element “statically.” It’s succinct, and
if you misspell the name you get an exception instead of an unknown element,
for example:

Element.create('vdieo', {src: '…'}) → no error, HTMLUnknownElement that
behaves nothing like <video>

new HTMLVdieoElement({src: '…'}) → ReferenceError, stack trace points me to
the faulty line of code.

When the element being created is “static” I think constructors are more
readable, too:

var b = Element.create('button', {}, [
  Element.create('img', {src: 'http://'}),
  document.createTextNode('Click me!')
]);

compared to:

var b = new HTMLButtonElement({}, [
  new Image('http://'),
  new TextNode('Click me!')
]);

Let me briefly reiterate that I think we want *both* Element.create and
constructors; they have complementary uses.

---

Second, re: setAttribute vs setting properties, there might be types other
than functions we want to not treat as strings; for example if a UA
implements CSSOM it might be nice to be able to set a style without having
to serialize and reparse the CSSStyleDeclaration.

Can we spec whether something in the attributes hash is set via setAttribute
or via setting a property based on the IDL for that element?

Alternatively we could provide a syntax, eg '@class': 'foo' to
setAttribute('class', 'foo') and className: 'foo' to set elem.className =
'foo'.

--

Third, is the order of attributes significant for XML namespace
declarations? eg does this:

<x xmlns:foo="…" foo:bar="…" />

mean the same thing as

<x foo:bar="…" xmlns:foo="…" />

? If not, including namespaces in the attribute dictionary is fraught,
because the iteration order of properties is undefined.

Dominic

On Fri, Aug 5, 2011 at 3:22 PM, Garrett Smith <dhtmlkitchen@gmail.com>wrote:

> On 8/4/11, Garrett Smith <dhtmlkitchen@gmail.com> wrote:
>
> [...]
> ser-generated function function.
> >
> > The scope of handler attributes is explained in HTML 5, though
> > incompletely:
> >
> http://dev.w3.org/html5/spec/Overview.html#event-handler-content-attributes
> > That's incomplete.
> >
> Correction: It is complete, I just misread it.
> --
> Garrett
>
>

Received on Saturday, 6 August 2011 16:06:09 UTC