JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / xml.js
diff --git a/_source/core/xml.js b/_source/core/xml.js
new file mode 100644 (file)
index 0000000..a4e3af0
--- /dev/null
@@ -0,0 +1,165 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview Defines the {@link CKEDITOR.xml} class, which represents a\r
+ *             loaded XML document.\r
+ */\r
+\r
+/**\r
+ * Represents a loaded XML document.\r
+ * @constructor\r
+ * @param {object|string} xmlObjectOrData A native XML (DOM document) object or\r
+ *             a string containing the XML definition to be loaded.\r
+ * @example\r
+ * var xml = <b>new CKEDITOR.xml( '<books><book title="My Book" /></books>' )</b>;\r
+ */\r
+CKEDITOR.xml = function( xmlObjectOrData )\r
+{\r
+       var baseXml = null;\r
+\r
+       if ( typeof xmlObjectOrData == 'object' )\r
+               baseXml = xmlObjectOrData;\r
+       else\r
+       {\r
+               var data = ( xmlObjectOrData || '' ).replace( /&nbsp;/g, '\xA0' );\r
+               if ( window.DOMParser )\r
+                       baseXml = (new DOMParser()).parseFromString( data, 'text/xml' );\r
+               else if ( window.ActiveXObject )\r
+               {\r
+                       try { baseXml = new ActiveXObject( 'MSXML2.DOMDocument' ); }\r
+                       catch(e)\r
+                       {\r
+                               try { baseXml = new ActiveXObject( 'Microsoft.XmlDom' ); } catch(e) {}\r
+                       }\r
+\r
+                       if ( baseXml )\r
+                       {\r
+                               baseXml.async = false;\r
+                               baseXml.resolveExternals = false;\r
+                               baseXml.validateOnParse = false;\r
+                               baseXml.loadXML( data );\r
+                       }\r
+               }\r
+       }\r
+\r
+       /**\r
+        * The native XML (DOM document) used by the class instance.\r
+        * @type object\r
+        * @example\r
+        */\r
+       this.baseXml = baseXml;\r
+};\r
+\r
+CKEDITOR.xml.prototype =\r
+{\r
+       /**\r
+        * Get a single node from the XML document, based on a XPath query.\r
+        * @param {String} xpath The XPath query to execute.\r
+        * @param {Object} [contextNode] The XML DOM node to be used as the context\r
+        *              for the XPath query. The document root is used by default.\r
+        * @returns {Object} A XML node element or null if the query has no results.\r
+        * @example\r
+        * // Create the XML instance.\r
+        * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
+        * // Get the first <item> node.\r
+        * var itemNode = <b>xml.selectSingleNode( 'list/item' )</b>;\r
+        * // Alert "item".\r
+        * alert( itemNode.nodeName );\r
+        */\r
+       selectSingleNode : function( xpath, contextNode )\r
+       {\r
+               var baseXml = this.baseXml;\r
+\r
+               if ( contextNode || ( contextNode = baseXml ) )\r
+               {\r
+                       if ( CKEDITOR.env.ie || contextNode.selectSingleNode )  // IE\r
+                               return contextNode.selectSingleNode( xpath );\r
+                       else if ( baseXml.evaluate )                                                    // Others\r
+                       {\r
+                               var result = baseXml.evaluate( xpath, contextNode, null, 9, null);\r
+                               return ( result && result.singleNodeValue ) || null;\r
+                       }\r
+               }\r
+\r
+               return null;\r
+       },\r
+\r
+       /**\r
+        * Gets a list node from the XML document, based on a XPath query.\r
+        * @param {String} xpath The XPath query to execute.\r
+        * @param {Object} [contextNode] The XML DOM node to be used as the context\r
+        *              for the XPath query. The document root is used by default.\r
+        * @returns {ArrayLike} An array containing all matched nodes. The array will\r
+        *              be empty if the query has no results.\r
+        * @example\r
+        * // Create the XML instance.\r
+        * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
+        * // Get the first <item> node.\r
+        * var itemNodes = xml.selectSingleNode( 'list/item' );\r
+        * // Alert "item" twice, one for each <item>.\r
+        * for ( var i = 0 ; i < itemNodes.length ; i++ )\r
+        *     alert( itemNodes[i].nodeName );\r
+        */\r
+       selectNodes : function( xpath, contextNode )\r
+       {\r
+               var baseXml = this.baseXml,\r
+                       nodes = [];\r
+\r
+               if ( contextNode || ( contextNode = baseXml ) )\r
+               {\r
+                       if ( CKEDITOR.env.ie || contextNode.selectNodes )               // IE\r
+                               return contextNode.selectNodes( xpath );\r
+                       else if ( baseXml.evaluate )                                                    // Others\r
+                       {\r
+                               var result = baseXml.evaluate( xpath, contextNode, null, 5, null);\r
+\r
+                               if ( result )\r
+                               {\r
+                                       var node;\r
+                                       while( ( node = result.iterateNext() ) )\r
+                                               nodes.push( node );\r
+                               }\r
+                       }\r
+               }\r
+\r
+               return nodes;\r
+       },\r
+\r
+       /**\r
+        * Gets the string representation of hte inner contents of a XML node,\r
+        * based on a XPath query.\r
+        * @param {String} xpath The XPath query to execute.\r
+        * @param {Object} [contextNode] The XML DOM node to be used as the context\r
+        *              for the XPath query. The document root is used by default.\r
+        * @returns {String} The textual representation of the inner contents of\r
+        *              the node or null if the query has no results.\r
+        * @example\r
+        * // Create the XML instance.\r
+        * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
+        * // Alert "<item id="test1" /><item id="test2" />".\r
+        * alert( xml.getInnerXml( 'list' ) );\r
+        */\r
+       getInnerXml : function( xpath, contextNode )\r
+       {\r
+               var node = this.selectSingleNode( xpath, contextNode ),\r
+                       xml = [];\r
+               if ( node )\r
+               {\r
+                       node = node.firstChild;\r
+                       while ( node )\r
+                       {\r
+                               if ( node.xml )                         // IE\r
+                                       xml.push( node.xml );\r
+                               else if ( window.XMLSerializer )        // Others\r
+                                       xml.push( ( new XMLSerializer() ).serializeToString( node ) );\r
+\r
+                               node = node.nextSibling;\r
+                       }\r
+               }\r
+\r
+               return xml.length ? xml.join( '' ) : null;\r
+       }\r
+};\r