Re: Element.createElement proposal

Henri Sivonen <[email protected]>
Newsgroups gmane.comp.mozilla.devel.dom
Message-ID <[email protected]>
In article <[email protected]>,
 Robert O'Callahan <[email protected]> wrote:

> On 11/8/09 1:20 PM, Johnny Stenback wrote:
> > On 08/10/2009 06:06 PM, Zack Weinberg wrote:
> >> What's wrong with
> >>
> >> var parent = document.getElementById("putsvghere"); // HTML elt
> >> var svg = parent.addChild('svg'); // HTML5 'svg' elt = SVG root elt
> >> var circle = svg.addChild('circle', {...}); // SVG 'circle'
> >
> > What should that code do if you replace parent.addChild('svg') with
> > parent.addChild('a')?
> 
> For simplicity I would say that e.addChild should always inherit the 
> namespace of e.
> 
> > And does this always append, or insert at the
> > beginning, what if I want to insert somewhere in the middle etc etc?
> 
> I think it should always append. The use cases for the other positions 
> are much fewer and can be handled with existing DOM APIs.

In article <[email protected]>,
 Robert O'Callahan <[email protected]> wrote:

> What about HTML5 SVG parsing? We want a solution that works well in an 
> HTML document.
> 
> So why not use the HTML5 parsing rules to determine which namespace the 
> element gets? The HTML5 parser already has a list of which elements 
> should be SVG or MathML.

The HTML5 parsing rules depend on context. Also, rules like deciding 
what to do with <font> only make sense in parsing.

However, I like the idea of addChild() and I like the idea of being able 
to pass attributes as a JS object of key-value pairs.

I suggest the following (inspired by the HTML5 parsing algorithm but 
simplified and XBL2 attribute support added):

 * Element.addChild(name, attrs=null): Creates an element in the same 
namespace as |this| with the local name /name/. If attrs is not null, 
adds attributes to the newly-created element as described below. Appends 
the newly-created element as the last child of |this|.

  - Exception: if |this| is in the http://www.w3.org/1999/xhtml 
namespace or one of the elements that make the HTML5 parsing algorithm 
allow HTML content: Behave like Document.createElement() below for the 
element creation part.

 * Document.createElement(name, attrs=null): If /name/ is "svg", creates 
an element in the http://www.w3.org/2000/svg namespace with the local 
name "svg". If the /name/ is "math", creates an element in the 
http://www.w3.org/1998/Math/MathML namespace with the local name "math" 
Otherwise, creates an element in the http://www.w3.org/1999/xhtml 
namespace with the local name /name/. If attrs is not null, adds 
attributes to the newly-created element as described below.

 * Adding attributes extracts the key-value pairs from the JS object and 
for each pair runs the following steps:
   - If /key/ starts with "xml:", call 
setAttributeNS("http://www.w3.org/XML/1998/namespace", key, value) on 
the newly-created element.
   - If /key/ starts with "xmlns:", call 
setAttributeNS("http://www.w3.org/2000/xmlns/", key, value) on the 
newly-created element.
   - If /key/ starts with "xlink:", call 
setAttributeNS("http://www.w3.org/1999/xlink", key, value) on the 
newly-created element.
   - If /key/ starts with "xbl:", call 
setAttributeNS("http://www.w3.org/ns/xbl", key, value) on the 
newly-created element.
   - Otherwise, call setAttributeNS(null, key, value).

-- 
Henri Sivonen
[email protected]
http://hsivonen.iki.fi/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.