Lively Kernel canvas
// this script is evaluated on world load= 0) {
if (string[lineS]]>', jsObj.tagName, nsDecl), baseDoc);
// set attributes
Properties.own(jsObj)
.reject(function(key) { return key == 'tagName' || key == 'children' })
.forEach(function(key) {
var value = jsObj[key];
if (key.include(':')) {
var prefix = key.split(':')[0];
var ns = nsMapping[prefix];
if (!ns) throw new Error('JS object includes node with tagname having a NS prefix but the NS cannot be found in the nsMapping!');
node.setAttributeNS(ns, key, value);
} else {
node.setAttribute(key, value);
}
})
// add childnodes
jsObj.children && jsObj.children.forEach(function(childJsObj) {
node.appendChild(this.convertToXML(childJsObj, nsMapping, baseDoc, true));
}, this);
return node;
}]]>');
var result = this.sut.convertToJSON(xml);
this.assert(result.tagName, 'test');
this.assertEqual(Properties.all(result).length, 1);
}]]>');
var result = this.sut.convertToJSON(xml);
this.assertEqual(result.id, '23');
this.assertEqual(result.x, 'foobar');
}]]>');
var result = this.sut.convertToJSON(xml);
this.assertEqual(result.children.length, 2);
this.assertEqual(result.children[0].tagName, 'test2');
this.assertEqual(result.children[1].abc, 'def');
}]]> baz');
var result = this.sut.convertToJSON(xml);
this.assertEqual(result.children[0].tagName, 'cdataSection');
this.assertEqual(result.children[0].data, 'foobar');
this.assertEqual(result.children[1].tagName, 'textNode');
this.assertEqual(result.children[1].data, ' baz');
}]]>
savefalsenull
truenullfalse
hmmmmmhmmmmmmmmmm14
10000.005379005962927533Wikicontroltrue
10000.42863508946292295Wikicontroltrue
10003.3230330571020116e-11Wikicontroltrue
Helper
RunTestfalsenull>' + result.selector;\n\t\t\tmsg += '\\n';\n\t\t\tmsg += result.err.message;\n\t\t\treturn msg;\n\t\t}).join('\\n---------\\n');\n\t\t$morph('testResult').setExtent(pt(400,20))\n\t\t$morph('testResult').setFill(Color.red);\n\t\t$morph('testResult').setTextString(failStr);\n\t} /* runFinished */"]]>null
10000.4782970521641952Wikicontroltrue
10006.06349666386225Wikicontroltrue
nulltrueExporter.stringify(x)x=stringToXML('<test><![CDATA[foobar\]\]\>baz</test>')Exporter.stringify(x)c=newXMLConverter()js=c.convertToJSON(x)c.toJSONString(js)Properties.all(document)document.TEXT_NODE baz')\nExporter.stringify(x)\n\nc=new XMLConverter()\njs=c.convertToJSON(x)\nc.toJSONString(js)\n\nProperties.all(document)\n\ndocument.TEXT_NODE"]]>truetruefalse11truetruefalse
false1truefalsetruetruefalse
nullfalsetruenullfalse
falsenullfalse
false
false
falsenullfalsefalsenullnullnullnullfalse
nullfalsetruenullfalse
XMLConverter[class]falsenullfalse
false
false
falsenullfalse
localrequirements[doit]falsetruefalse0false
initializer[doit]falsetruefalse0false
TextMorph.replaceCurrentLine[doit]falsetruefalse0false
XMLConverter[class]falsetruefalse0false
Tests.Helper.XMLConverterTest[class]falsetruefalse0false3103nullnulltruefalsenullnulltruetrue
1true
null
null
null
nulltruetrue
convertToJSONfalsetruefalse0false
storeXMLDataIntofalsetruefalse0false
toJSONStringfalsetruefalse0false
convertToXMLfalsetruefalse0false
createNodeFromStringfalsetruefalse0false310-1nullnullnulltruefalsenullnulltruetrue
1true
null
null
null
nulltruetrue
Object.subclass('XMLConverter',{convertToJSON:function(xml){returnthis.storeXMLDataInto(xml,{});},storeXMLDataInto:function(xml,jsObj){jsObj.tagName=xml.tagName;$A(xml.attributes).forEach(function(attr){jsObj[attr.name]=attr.value});if(xml.childNodes.length===0)returnjsObj;jsObj.children=$A(xml.childNodes).collect(function(node){if(node.nodeType==Global.document.CDATA_SECTION_NODE){return{tagName:'cdataSection',data:node.data};}if(node.nodeType==Global.document.TEXT_NODE){return{tagName:'textNode',data:node.data};}returnthis.storeXMLDataInto(node,{});},this);returnjsObj;},toJSONString:function(jsObj,indent){if(!indent)indent='';result='{';for(varkeyinjsObj){varvalue=jsObj[key];result+='\n\t'+indent+'"'+key+'":';if(Object.isNumber(value)){result+=value;}elseif(Object.isString(value)){result+='"'+value+'"';}elseif(Object.isArray(value)){result+='['+value.collect(function(item){returnthis.toJSONString(item,indent+'\t');},this).join(',')+']';}else{result+=this.toJSONString(value,indent+'\t');}result+=',';}result+='\n'+indent+'}';returnresult;},convertToXML:function(jsObj,nsMapping,baseDoc,nsWereDeclared){if(!jsObj.tagName)thrownewError('CannotconvertJSobjectwithoutattribute"tagName"toXML!');//dealwithspecialnodesif(jsObj.tagName==='cdataSection')returnbaseDoc.createCDATASection(jsObj.data);if(jsObj.tagName==='textNode')returnbaseDoc.createTextNode(jsObj.data);//createnodevarnsDecl=nsWereDeclared?'':Properties.own(nsMapping).collect(function(prefix){returnStrings.format('xmlns:%s="%s"',prefix,nsMapping[prefix])}).join('');varnode=this.createNodeFromString(Strings.format('<%s%s/>',jsObj.tagName,nsDecl),baseDoc);//setattributesProperties.own(jsObj).reject(function(key){returnkey=='tagName'||key=='children'}).forEach(function(key){varvalue=jsObj[key];if(key.include(':')){varprefix=key.split(':')[0];varns=nsMapping[prefix];if(!ns)thrownewError('JSobjectincludesnodewithtagnamehavingaNSprefixbuttheNScannotbefoundinthensMapping!');node.setAttributeNS(ns,key,value);}else{node.setAttribute(key,value);}})//addchildnodesjsObj.children&&jsObj.children.forEach(function(childJsObj){node.appendChild(this.convertToXML(childJsObj,nsMapping,baseDoc,true));},this);returnnode;},createNodeFromString:function(string,baseDoc){returnbaseDoc.adoptNode(newDOMParser().parseFromString(string,"text/xml").documentElement);},});', jsObj.tagName, nsDecl), baseDoc);\n\t\n\t// set attributes\n\tProperties.own(jsObj)\n\t\t.reject(function(key) { return key == 'tagName' || key == 'children' })\n\t\t.forEach(function(key) {\n\t\t\tvar value = jsObj[key];\n\t\t\tif (key.include(':')) {\n\t\t\t\tvar prefix = key.split(':')[0];\n\t\t\t\tvar ns = nsMapping[prefix];\n\t\t\t\tif (!ns) throw new Error('JS object includes node with tagname having a NS prefix but the NS cannot be found in the nsMapping!');\n\t\t\t\tnode.setAttributeNS(ns, key, value);\n\t\t\t} else {\n\t\t\t\tnode.setAttribute(key, value);\n\t\t\t}\n\t\t})\n\t\n\t// add childnodes\n\tjsObj.children && jsObj.children.forEach(function(childJsObj) {\n\t\tnode.appendChild(this.convertToXML(childJsObj, nsMapping, baseDoc, true));\n\t}, this);\n\treturn node;\n},\n\ncreateNodeFromString: function(string, baseDoc) {\n\treturn baseDoc.adoptNode(new DOMParser().parseFromString(string, \"text/xml\").documentElement);\n},\n\n});"]]>11truetrue2000000', jsObj.tagName, nsDecl), baseDoc);\n\t\n\t// set attributes\n\tProperties.own(jsObj)\n\t\t.reject(function(key) { return key == 'tagName' || key == 'children' })\n\t\t.forEach(function(key) {\n\t\t\tvar value = jsObj[key];\n\t\t\tif (key.include(':')) {\n\t\t\t\tvar prefix = key.split(':')[0];\n\t\t\t\tvar ns = nsMapping[prefix];\n\t\t\t\tif (!ns) throw new Error('JS object includes node with tagname having a NS prefix but the NS cannot be found in the nsMapping!');\n\t\t\t\tnode.setAttributeNS(ns, key, value);\n\t\t\t} else {\n\t\t\t\tnode.setAttribute(key, value);\n\t\t\t}\n\t\t})\n\t\n\t// add childnodes\n\tjsObj.children && jsObj.children.forEach(function(childJsObj) {\n\t\tnode.appendChild(this.convertToXML(childJsObj, nsMapping, baseDoc, true));\n\t}, this);\n\treturn node;\n},\n\ncreateNodeFromString: function(string, baseDoc) {\n\treturn baseDoc.adoptNode(new DOMParser().parseFromString(string, \"text/xml\").documentElement);\n},\n\n});"]]>truetrue
1truetruetrue
Browseworld...falsenullfalsetrue
Pushchangesbackfalsenullfalsefalse
Refreshfalsenullfalsetrue
Evalonfalsenullfalsetrue
Sortfalsenullfalsetrue
truetruenullfalsenullnullnullnullfalse
144
nullfalsetruenullfalse
Tests.Helper.XMLConverterTest[class]falsenullfalse
false
false
falsenullfalse
localrequirements[doit]falsetruefalse0false
initializer[doit]falsetruefalse0false
TextMorph.replaceCurrentLine[doit]falsetruefalse0false
XMLConverter[class]falsetruefalse0false
Tests.Helper.XMLConverterTest[class]falsetruefalse0false321.54nullnulltruefalsenullnulltruetrue
1true
null
null
null
nulltruetrue
toXMLfalsetruefalse0false
setUpfalsetruefalse0false
test01XMLNodeToJSONfalsetruefalse0false
test02XMLNodeWithAttributesToJSONfalsetruefalse0false
test03XMLNodeWithAttributesAndChildrenToJSONfalsetruefalse0false
test03CDATAAndTextfalsetruefalse0false
test04JStoXMLfalsetruefalse0false
test05JStoXMLWithChildNodesAndTextContentfalsetruefalse0false321.5-1nullnullnulltruefalsenullnulltruetrue
1true
null
null
null
nulltruetrue
1true
TestCase.subclass('Tests.Helper.XMLConverterTest',{toXML:function(string){returnnewDOMParser().parseFromString(string,"text/xml").documentElement;},setUp:function($super){$super();this.sut=newXMLConverter();},test01XMLNodeToJSON:function(){varxml=this.toXML('<test/>');varresult=this.sut.convertToJSON(xml);this.assert(result.tagName,'test');this.assertEqual(Properties.all(result).length,1);},test02XMLNodeWithAttributesToJSON:function(){varxml=this.toXML('<testid="23"x="foobar"/>');varresult=this.sut.convertToJSON(xml);this.assertEqual(result.id,'23');this.assertEqual(result.x,'foobar');},test03XMLNodeWithAttributesAndChildrenToJSON:function(){varxml=this.toXML('<testx="foo"><test2/><test3abc="def"/></test>');varresult=this.sut.convertToJSON(xml);this.assertEqual(result.children.length,2);this.assertEqual(result.children[0].tagName,'test2');this.assertEqual(result.children[1].abc,'def');},test03CDATAAndText:function(){varxml=this.toXML('<test><![CDATA[foobar\]\]\>baz</test>');varresult=this.sut.convertToJSON(xml);this.assertEqual(result.children[0].tagName,'cdataSection');this.assertEqual(result.children[0].data,'foobar');this.assertEqual(result.children[1].tagName,'textNode');this.assertEqual(result.children[1].data,'baz');},test04JStoXML:function(){varjsObj={tagName:'script',type:'foo','xlink:href':'foo.js'};varnsMapping={xlink:Namespace.XLINK};varresult=this.sut.convertToXML(jsObj,nsMapping,Global.document);this.assertEqual(result.tagName,'script');this.assertEqual(result.getAttribute('type'),'foo');this.assertEqual(result.getAttributeNS(Namespace.XLINK,'href'),'foo.js');},test05JStoXMLWithChildNodesAndTextContent:function(){varjsObj={tagName:'foo',children:[{tagName:'bar',children:[{tagName:'textNode',data:'Hello'},{tagName:'cdataSection',data:'World'}]}]};varnsMapping={};varresult=this.sut.convertToXML(jsObj,nsMapping,Global.document);this.assertEqual(result.tagName,'foo');this.assertEqual(result.childNodes.length,1);this.assertEqual(result.childNodes[0].childNodes.length,2);this.assertEqual(result.childNodes[0].textContent,'HelloWorld');},});');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assert(result.tagName, 'test');\n\tthis.assertEqual(Properties.all(result).length, 1);\n},\n\ntest02XMLNodeWithAttributesToJSON: function() {\n\tvar xml = this.toXML('');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.id, '23');\n\tthis.assertEqual(result.x, 'foobar');\n},\n\ntest03XMLNodeWithAttributesAndChildrenToJSON: function() {\n\tvar xml = this.toXML('');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.children.length, 2);\n\tthis.assertEqual(result.children[0].tagName, 'test2');\n\tthis.assertEqual(result.children[1].abc, 'def');\n},\n\ntest03CDATAAndText: function() {\n\tvar xml = this.toXML(' baz');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.children[0].tagName, 'cdataSection');\n\tthis.assertEqual(result.children[0].data, 'foobar');\n\tthis.assertEqual(result.children[1].tagName, 'textNode');\n\tthis.assertEqual(result.children[1].data, ' baz');\n},\n\ntest04JStoXML: function() {\n\tvar jsObj = {tagName: 'script', type: 'foo', 'xlink:href': 'foo.js'};\n\tvar nsMapping = {xlink: Namespace.XLINK};\n\tvar result = this.sut.convertToXML(jsObj, nsMapping, Global.document);\n\tthis.assertEqual(result.tagName, 'script');\n\tthis.assertEqual(result.getAttribute('type'), 'foo');\n\tthis.assertEqual(result.getAttributeNS(Namespace.XLINK, 'href'), 'foo.js');\n},\n\ntest05JStoXMLWithChildNodesAndTextContent: function() {\n\tvar jsObj = {\n\t\ttagName: 'foo',\n\t\tchildren: [{\n\t\t\ttagName: 'bar',\n\t\t\tchildren: [{\n\t\t\t\ttagName: 'textNode',\n\t\t\t\tdata: 'Hello '\n\t\t\t}, {\n\t\t\t\ttagName: 'cdataSection',\n\t\t\t\tdata: 'World'\n\t\t\t}]\n\t\t}]\n\t};\n\tvar nsMapping = {};\n\tvar result = this.sut.convertToXML(jsObj, nsMapping, Global.document);\n\tthis.assertEqual(result.tagName, 'foo');\n\tthis.assertEqual(result.childNodes.length, 1);\n\tthis.assertEqual(result.childNodes[0].childNodes.length, 2);\n\tthis.assertEqual(result.childNodes[0].textContent, 'Hello World');\n},\n\n});"]]>7truetrue2000000');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assert(result.tagName, 'test');\n\tthis.assertEqual(Properties.all(result).length, 1);\n},\n\ntest02XMLNodeWithAttributesToJSON: function() {\n\tvar xml = this.toXML('');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.id, '23');\n\tthis.assertEqual(result.x, 'foobar');\n},\n\ntest03XMLNodeWithAttributesAndChildrenToJSON: function() {\n\tvar xml = this.toXML('');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.children.length, 2);\n\tthis.assertEqual(result.children[0].tagName, 'test2');\n\tthis.assertEqual(result.children[1].abc, 'def');\n},\n\ntest03CDATAAndText: function() {\n\tvar xml = this.toXML(' baz');\n\tvar result = this.sut.convertToJSON(xml);\n\tthis.assertEqual(result.children[0].tagName, 'cdataSection');\n\tthis.assertEqual(result.children[0].data, 'foobar');\n\tthis.assertEqual(result.children[1].tagName, 'textNode');\n\tthis.assertEqual(result.children[1].data, ' baz');\n},\n\ntest04JStoXML: function() {\n\tvar jsObj = {tagName: 'script', type: 'foo', 'xlink:href': 'foo.js'};\n\tvar nsMapping = {xlink: Namespace.XLINK};\n\tvar result = this.sut.convertToXML(jsObj, nsMapping, Global.document);\n\tthis.assertEqual(result.tagName, 'script');\n\tthis.assertEqual(result.getAttribute('type'), 'foo');\n\tthis.assertEqual(result.getAttributeNS(Namespace.XLINK, 'href'), 'foo.js');\n},\n\ntest05JStoXMLWithChildNodesAndTextContent: function() {\n\tvar jsObj = {\n\t\ttagName: 'foo',\n\t\tchildren: [{\n\t\t\ttagName: 'bar',\n\t\t\tchildren: [{\n\t\t\t\ttagName: 'textNode',\n\t\t\t\tdata: 'Hello '\n\t\t\t}, {\n\t\t\t\ttagName: 'cdataSection',\n\t\t\t\tdata: 'World'\n\t\t\t}]\n\t\t}]\n\t};\n\tvar nsMapping = {};\n\tvar result = this.sut.convertToXML(jsObj, nsMapping, Global.document);\n\tthis.assertEqual(result.tagName, 'foo');\n\tthis.assertEqual(result.childNodes.length, 1);\n\tthis.assertEqual(result.childNodes[0].childNodes.length, 2);\n\tthis.assertEqual(result.childNodes[0].textContent, 'Hello World');\n},\n\n});"]]>truetruetruetrue
Browseworld...falsenullfalsetrue
Pushchangesbackfalsenullfalsefalse
Refreshfalsenullfalsetrue
Evalonfalsenullfalsetrue
Sortfalsenullfalsetrue
truetruenullfalsenullnullnullnullfalsenull46