JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / xml / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.xml} class, which represents a\r
8  *              loaded XML document.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         CKEDITOR.plugins.add( 'xml', {});\r
14 \r
15         /**\r
16          * Represents a loaded XML document.\r
17          * @constructor\r
18          * @param {object|string} xmlObjectOrData A native XML (DOM document) object or\r
19          *              a string containing the XML definition to be loaded.\r
20          * @example\r
21          * var xml = <b>new CKEDITOR.xml( '<books><book title="My Book" /></books>' )</b>;\r
22          */\r
23         CKEDITOR.xml = function( xmlObjectOrData )\r
24         {\r
25                 var baseXml = null;\r
26 \r
27                 if ( typeof xmlObjectOrData == 'object' )\r
28                         baseXml = xmlObjectOrData;\r
29                 else\r
30                 {\r
31                         var data = ( xmlObjectOrData || '' ).replace( /&nbsp;/g, '\xA0' );\r
32                         if ( window.DOMParser )\r
33                                 baseXml = (new DOMParser()).parseFromString( data, 'text/xml' );\r
34                         else if ( window.ActiveXObject )\r
35                         {\r
36                                 try { baseXml = new ActiveXObject( 'MSXML2.DOMDocument' ); }\r
37                                 catch(e)\r
38                                 {\r
39                                         try { baseXml = new ActiveXObject( 'Microsoft.XmlDom' ); } catch(e) {}\r
40                                 }\r
41 \r
42                                 if ( baseXml )\r
43                                 {\r
44                                         baseXml.async = false;\r
45                                         baseXml.resolveExternals = false;\r
46                                         baseXml.validateOnParse = false;\r
47                                         baseXml.loadXML( data );\r
48                                 }\r
49                         }\r
50                 }\r
51 \r
52                 /**\r
53                  * The native XML (DOM document) used by the class instance.\r
54                  * @type object\r
55                  * @example\r
56                  */\r
57                 this.baseXml = baseXml;\r
58         };\r
59 \r
60         CKEDITOR.xml.prototype =\r
61         {\r
62                 /**\r
63                  * Get a single node from the XML document, based on a XPath query.\r
64                  * @param {String} xpath The XPath query to execute.\r
65                  * @param {Object} [contextNode] The XML DOM node to be used as the context\r
66                  *              for the XPath query. The document root is used by default.\r
67                  * @returns {Object} A XML node element or null if the query has no results.\r
68                  * @example\r
69                  * // Create the XML instance.\r
70                  * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
71                  * // Get the first <item> node.\r
72                  * var itemNode = <b>xml.selectSingleNode( 'list/item' )</b>;\r
73                  * // Alert "item".\r
74                  * alert( itemNode.nodeName );\r
75                  */\r
76                 selectSingleNode : function( xpath, contextNode )\r
77                 {\r
78                         var baseXml = this.baseXml;\r
79 \r
80                         if ( contextNode || ( contextNode = baseXml ) )\r
81                         {\r
82                                 if ( CKEDITOR.env.ie || contextNode.selectSingleNode )  // IE\r
83                                         return contextNode.selectSingleNode( xpath );\r
84                                 else if ( baseXml.evaluate )                                                    // Others\r
85                                 {\r
86                                         var result = baseXml.evaluate( xpath, contextNode, null, 9, null);\r
87                                         return ( result && result.singleNodeValue ) || null;\r
88                                 }\r
89                         }\r
90 \r
91                         return null;\r
92                 },\r
93 \r
94                 /**\r
95                  * Gets a list node from the XML document, based on a XPath query.\r
96                  * @param {String} xpath The XPath query to execute.\r
97                  * @param {Object} [contextNode] The XML DOM node to be used as the context\r
98                  *              for the XPath query. The document root is used by default.\r
99                  * @returns {ArrayLike} An array containing all matched nodes. The array will\r
100                  *              be empty if the query has no results.\r
101                  * @example\r
102                  * // Create the XML instance.\r
103                  * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
104                  * // Get the first <item> node.\r
105                  * var itemNodes = xml.selectSingleNode( 'list/item' );\r
106                  * // Alert "item" twice, one for each <item>.\r
107                  * for ( var i = 0 ; i < itemNodes.length ; i++ )\r
108                  *     alert( itemNodes[i].nodeName );\r
109                  */\r
110                 selectNodes : function( xpath, contextNode )\r
111                 {\r
112                         var baseXml = this.baseXml,\r
113                                 nodes = [];\r
114 \r
115                         if ( contextNode || ( contextNode = baseXml ) )\r
116                         {\r
117                                 if ( CKEDITOR.env.ie || contextNode.selectNodes )               // IE\r
118                                         return contextNode.selectNodes( xpath );\r
119                                 else if ( baseXml.evaluate )                                                    // Others\r
120                                 {\r
121                                         var result = baseXml.evaluate( xpath, contextNode, null, 5, null);\r
122 \r
123                                         if ( result )\r
124                                         {\r
125                                                 var node;\r
126                                                 while ( ( node = result.iterateNext() ) )\r
127                                                         nodes.push( node );\r
128                                         }\r
129                                 }\r
130                         }\r
131 \r
132                         return nodes;\r
133                 },\r
134 \r
135                 /**\r
136                  * Gets the string representation of hte inner contents of a XML node,\r
137                  * based on a XPath query.\r
138                  * @param {String} xpath The XPath query to execute.\r
139                  * @param {Object} [contextNode] The XML DOM node to be used as the context\r
140                  *              for the XPath query. The document root is used by default.\r
141                  * @returns {String} The textual representation of the inner contents of\r
142                  *              the node or null if the query has no results.\r
143                  * @example\r
144                  * // Create the XML instance.\r
145                  * var xml = new CKEDITOR.xml( '<list><item id="test1" /><item id="test2" /></list>' );\r
146                  * // Alert "<item id="test1" /><item id="test2" />".\r
147                  * alert( xml.getInnerXml( 'list' ) );\r
148                  */\r
149                 getInnerXml : function( xpath, contextNode )\r
150                 {\r
151                         var node = this.selectSingleNode( xpath, contextNode ),\r
152                                 xml = [];\r
153                         if ( node )\r
154                         {\r
155                                 node = node.firstChild;\r
156                                 while ( node )\r
157                                 {\r
158                                         if ( node.xml )                         // IE\r
159                                                 xml.push( node.xml );\r
160                                         else if ( window.XMLSerializer )        // Others\r
161                                                 xml.push( ( new XMLSerializer() ).serializeToString( node ) );\r
162 \r
163                                         node = node.nextSibling;\r
164                                 }\r
165                         }\r
166 \r
167                         return xml.length ? xml.join( '' ) : null;\r
168                 }\r
169         };\r
170 })();\r