Re: Namespace-aware APIs do not need to suck

On Nov 19, 2009, at 7:09 AM, Julian Reschke wrote:

> Michael A. Puls II wrote:
>> ...
>> I tried the following with Opera:
>> (function() {
>>    var oldCreateElement = Document.prototype.createElement;
>>    Document.prototype.createElement = function() {
>>        var args = arguments;
>>        if (args.length !== 1 && args.length !== 2)
>>            throw new Error("WRONG_ARGUMENTS_ERR");
>>        if (args.length === 2)
>>            return this.createElementNS(args[0], args[1]);
>>        var pattern = /^\{(.+)\}(.+)$/g;
>>        var ref = this;
>>        var el = null;
>>        args[0].replace(pattern, function(match, ns, name) {
>>            el = ref.createElementNS(ns, name);
>>        });
>>        return el !== null ? el : oldCreateElement.call(this,  
>> args[0]);
>>    };
>> })();
>> var x = document.createElement("{http://www.w3.org/1999/xhtml}div");
>> var y = document.createElement("http://www.w3.org/1999/xhtml",  
>> "div");
>> var z = document.createElement("div");
>> alert(x);
>> alert(y);
>> alert(z);
>> Using the first way seems pretty cool by me fwiw.
>> ...
>
> Very cool.
>
> So how do we proceed?
>
> At TPAC I pointed out that we *could* enhance APIs with respect to  
> treating namespaces, and I was told there was no proposal.
>
> So here is a proposal. Should we expand it? For instance with  
> respect to  obtaining information from an existing DOM node, or with  
> respect to traversing the DOM?

I think to have a solid proposal we'd need:

A) More methods than just createElement()... a big part of the concern  
is using DOM APIs to deal with elements and attributes created by  
parsing markup.
B) Slightly more formal specificiation - a sample implementation in  
JavaScript is not easy to read or clear about what details would be  
normative.

Assuming this aligned with the DOM Core set of methods, then probably  
Web Apps WG would be the right venue for such a proposal.

Personally, I'm still somewhat dubious of this API. Having to  
predeclare all the tags you intend to use is inconvenient and may  
substantially increase code size. But it might be nicer if there were  
predeclared versions of the Clark notation strings for all standard  
elements and attributes. For example if you could write this without  
having to predeclare anything:

var x = document.createElement(svg.a);
x.setAttribute(xlink.href, "http://foobar.com");

... then that would be pretty sweet.

Regards,
Maciej

Received on Thursday, 19 November 2009 17:57:40 UTC