[Greasemonkey] e4x

Stéphane Payrard stef at stefp.dyndns.org
Tue Feb 14 16:12:45 EST 2006


Mor Roses a écrit :
| I got bored at home so i made you a function for importing XML string into a
| document usage:
| node=importNode(<p id="test"><a href="http://www.google.com">Click
| me</a></p>,document)
| element.appendChild(node);
| 
| the function:
| 
| function importNode(xmlObj,doc) {
|     var me=arguments.callee;
|     var xhtmlObj=<testing xmlns={me.Const.xhtmlns} />;
|     if(!me.Static.parser) me.Static.parser=new me.Const.parseObject;
|     xhtmlObj.test=xmlObj;
|     var domTree=me.Static.parser[me.Const.parseFunction](
| xhtmlObj.toXMLString(),me.Const.mimeType);
|     var importMe=domTree.documentElement.firstChild;
|     while(importMe && importMe.nodeType!=1) importMe=importMe.nextSibling;
|     return (importMe)?doc.importNode(importMe,true):null;
| }
| 
| importNode.Const={
|     'xhtmlns' : "http://www.w3.org/1999/xhtml",
|     'mimeType' : "text/xml",
|     'parseObject' : DOMParser,
|     'parseFunction' : 'parseFromString'
| };
| 
| importNode.Static={};
| 

Why factorising stuff in importNode.Const when you have only one parser?
This code seems artificially complicated, but may be you wrote it as a
showcase of js features. 
Sure, it learnt me a few things. So, many thanks/

--
 cognominal stef


More information about the Greasemonkey mailing list