jindw / xmldom

A PURE JS W3C Standard based(XML DOM Level2 CORE) DOMParser and XMLSerializer.
Other
819 stars 265 forks source link

createElementNS does not seem to work #176

Closed BobFrankston closed 7 years ago

BobFrankston commented 8 years ago

the NS argument in createElementNS seems to be ignored. I'm attaching sample code (generated from a larger typescript example).

The browser version does better but it is a very sloppy implementation which is another issue.

temp (2).txt

jindw commented 7 years ago

yes it's a bug can you push a test case?

BobFrankston commented 7 years ago

The temp (2).txt is a test case. But it's been a while -- I've since programmed around it.

"use strict";
var xdom = require('xmldom');

// Just for debugging
var w3 = "http://www.w3.org/1999/xhtml";
var n1 = "http://www.frankston.com/public";
var n2 = "http://rmf.vc/n2";
var n3 = "http://rmf.vc/n3";
var hx = "<html xmlns='" + w3 + "' xmlns:rmf='" + n1 + "'><rmf:foo hello='asdfa'/></html>";

var dp = new xdom.DOMParser();
var xs = new xdom.XMLSerializer();
var ss = function (xe) { return xs.serializeToString(xe); };
var xd = dp.parseFromString(hx, "text/xml");
var de = xd.documentElement;
var els = [].slice.call(xd.documentElement.getElementsByTagNameNS(n1, "foo"));
for (var _i = 0, els_1 = els; _i < els_1.length; _i++) {
    var el = els_1[_i];
    var te = xd.createElementNS(n1, "test");
    te.setAttributeNS(n1, "bar", "valx");
    console.log("New Elm: " + ss(te));
    el.appendChild(te);
    var tx = xd.createElementNS(n2, "test");
    tx.setAttributeNS(n2, "bar", "valx");
    console.log("New Elm: " + ss(tx));
    el.appendChild(tx);
    console.log("Element: " + ss(tx));
}
var sr = xs.serializeToString(xd);
console.log("Serialized: " + sr.replace(/>/g, ">\n  "));
debugger;
jindw commented 7 years ago

thanks

wingedfox commented 7 years ago

Hi,

Unfortunately this issue is not fixed for all the cases. With 0.1.27 I see the following

<my:node-collection xmlns:my="http://example.com">
  <node xmlns="http://example.com">value1</node>
  <node>value2</node>
</my:node-collection>

To be honest, I'd prefer to see the correct prefixes added to all nodes.