diff --git a/lib/OpenLayers/Format/XML.js b/lib/OpenLayers/Format/XML.js index f4c13499c0..d06e1e8317 100644 --- a/lib/OpenLayers/Format/XML.js +++ b/lib/OpenLayers/Format/XML.js @@ -848,6 +848,29 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, { return uri; }, + /** + * Method: getXMLDoc + * Get an XML document for nodes that are not supported in HTML (e.g. + * createCDATASection). On IE, this will either return an existing or + * create a new on the instance. On other browsers, this will + * either return an existing or create a new shared document (see + * ). + * + * Returns: + * {XMLDocument} + */ + getXMLDoc: function() { + if (!OpenLayers.Format.XML.document && !this.xmldom) { + if (document.implementation && document.implementation.createDocument) { + OpenLayers.Format.XML.document = + document.implementation.createDocument("", "", null); + } else if (!this.xmldom && window.ActiveXObject) { + this.xmldom = new ActiveXObject("Microsoft.XMLDOM"); + } + } + return OpenLayers.Format.XML.document || this.xmldom; + }, + CLASS_NAME: "OpenLayers.Format.XML" }); @@ -879,3 +902,10 @@ OpenLayers.Format.XML.lookupNamespaceURI = OpenLayers.Function.bind( OpenLayers.Format.XML.prototype.lookupNamespaceURI, OpenLayers.Format.XML.prototype ); + +/** + * Property: OpenLayers.Format.XML.document + * {XMLDocument} XML document to reuse for creating non-HTML compliant nodes, + * like document.createCDATASection. + */ +OpenLayers.Format.XML.document = null; diff --git a/tests/Format/XML.html b/tests/Format/XML.html index 12f12afc64..8da8fbf1e3 100644 --- a/tests/Format/XML.html +++ b/tests/Format/XML.html @@ -849,6 +849,19 @@ } + function test_getXMLDoc(t) { + t.plan(2); + var format = new OpenLayers.Format.XML(); + var doc = format.getXMLDoc(); + t.ok(doc !== document, "document returned from getXMLDoc is not the page's html doc"); + var root = format.createElementNS("http://test", "root"); + // appending CDATA created from a different document + var cdata = doc.createCDATASection(""); + root.appendChild(cdata); + var result = format.write(root); + var expect = ']]>'; + t.eq(result, expect, "document with CDATA section serialized correctly"); + }