| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview |
| 17 |
* XML utilities. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.xml'); |
| 22 |
|
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.NodeType'); |
| 25 |
goog.require('goog.userAgent'); |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Max XML size for MSXML2. Used to prevent potential DoS attacks. |
| 30 |
* @type {number} |
| 31 |
*/ |
| 32 |
goog.dom.xml.MAX_XML_SIZE_KB = 2 * 1024; // In kB |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Max XML size for MSXML2. Used to prevent potential DoS attacks. |
| 37 |
* @type {number} |
| 38 |
*/ |
| 39 |
goog.dom.xml.MAX_ELEMENT_DEPTH = 256; // Same default as MSXML6. |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Check for ActiveXObject support by the browser. |
| 44 |
* @return {boolean} true if browser has ActiveXObject support. |
| 45 |
* @private |
| 46 |
*/ |
| 47 |
goog.dom.xml.hasActiveXObjectSupport_ = function() { |
| 48 |
if (!goog.userAgent.IE) { |
| 49 |
// Avoid raising useless exception in case code is not compiled |
| 50 |
// and browser is not MSIE. |
| 51 |
return false; |
| 52 |
} |
| 53 |
try { |
| 54 |
// Due to lot of changes in IE 9, 10 & 11 behaviour and ActiveX being |
| 55 |
// totally disableable using MSIE's security level, trying to create the |
| 56 |
// ActiveXOjbect is a lot more reliable than testing for the existence of |
| 57 |
// window.ActiveXObject |
| 58 |
new ActiveXObject('MSXML2.DOMDocument'); |
| 59 |
return true; |
| 60 |
} catch (e) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* True if browser has ActiveXObject support. |
| 68 |
* Possible override if this test become wrong in coming IE versions. |
| 69 |
* @type {boolean} |
| 70 |
*/ |
| 71 |
goog.dom.xml.ACTIVEX_SUPPORT = |
| 72 |
goog.userAgent.IE && goog.dom.xml.hasActiveXObjectSupport_(); |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Creates an XML document appropriate for the current JS runtime |
| 77 |
* @param {string=} opt_rootTagName The root tag name. |
| 78 |
* @param {string=} opt_namespaceUri Namespace URI of the document element. |
| 79 |
* @param {boolean=} opt_preferActiveX Whether to default to ActiveXObject to |
| 80 |
* create Document in IE. Use this if you need xpath support in IE (e.g., |
| 81 |
* selectSingleNode or selectNodes), but be aware that the ActiveXObject does |
| 82 |
* not support various DOM-specific Document methods and attributes. |
| 83 |
* @return {Document} The new document. |
| 84 |
* @throws {Error} if browser does not support creating new documents or |
| 85 |
* namespace is provided without a root tag name. |
| 86 |
*/ |
| 87 |
goog.dom.xml.createDocument = function( |
| 88 |
opt_rootTagName, opt_namespaceUri, opt_preferActiveX) { |
| 89 |
if (opt_namespaceUri && !opt_rootTagName) { |
| 90 |
throw new Error('Can\'t create document with namespace and no root tag'); |
| 91 |
} |
| 92 |
// If document.implementation.createDocument is available and they haven't |
| 93 |
// explicitly opted to use ActiveXObject when possible. |
| 94 |
if (document.implementation && document.implementation.createDocument && |
| 95 |
!(goog.dom.xml.ACTIVEX_SUPPORT && opt_preferActiveX)) { |
| 96 |
return document.implementation.createDocument( |
| 97 |
opt_namespaceUri || '', opt_rootTagName || '', null); |
| 98 |
} else if (goog.dom.xml.ACTIVEX_SUPPORT) { |
| 99 |
var doc = goog.dom.xml.createMsXmlDocument_(); |
| 100 |
if (doc) { |
| 101 |
if (opt_rootTagName) { |
| 102 |
doc.appendChild( |
| 103 |
doc.createNode( |
| 104 |
goog.dom.NodeType.ELEMENT, opt_rootTagName, |
| 105 |
opt_namespaceUri || '')); |
| 106 |
} |
| 107 |
return doc; |
| 108 |
} |
| 109 |
} |
| 110 |
throw new Error('Your browser does not support creating new documents'); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Creates an XML document from a string |
| 116 |
* @param {string} xml The text. |
| 117 |
* @param {boolean=} opt_preferActiveX Whether to default to ActiveXObject to |
| 118 |
* create Document in IE. Use this if you need xpath support in IE (e.g., |
| 119 |
* selectSingleNode or selectNodes), but be aware that the ActiveXObject does |
| 120 |
* not support various DOM-specific Document methods and attributes. |
| 121 |
* @return {Document} XML document from the text. |
| 122 |
* @throws {Error} if browser does not support loading XML documents. |
| 123 |
*/ |
| 124 |
goog.dom.xml.loadXml = function(xml, opt_preferActiveX) { |
| 125 |
if (typeof DOMParser != 'undefined' && |
| 126 |
!(goog.dom.xml.ACTIVEX_SUPPORT && opt_preferActiveX)) { |
| 127 |
return new DOMParser().parseFromString(xml, 'application/xml'); |
| 128 |
} else if (goog.dom.xml.ACTIVEX_SUPPORT) { |
| 129 |
var doc = goog.dom.xml.createMsXmlDocument_(); |
| 130 |
doc.loadXML(xml); |
| 131 |
return doc; |
| 132 |
} |
| 133 |
throw new Error('Your browser does not support loading xml documents'); |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Serializes an XML document or subtree to string. |
| 139 |
* @param {Document|Element} xml The document or the root node of the subtree. |
| 140 |
* @return {string} The serialized XML. |
| 141 |
* @throws {Error} if browser does not support XML serialization. |
| 142 |
*/ |
| 143 |
goog.dom.xml.serialize = function(xml) { |
| 144 |
// Compatible with IE/ActiveXObject. |
| 145 |
var text = xml.xml; |
| 146 |
if (text) { |
| 147 |
return text; |
| 148 |
} |
| 149 |
// Compatible with Firefox, Opera and WebKit. |
| 150 |
if (typeof XMLSerializer != 'undefined') { |
| 151 |
return new XMLSerializer().serializeToString(xml); |
| 152 |
} |
| 153 |
throw new Error('Your browser does not support serializing XML documents'); |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Selects a single node using an Xpath expression and a root node |
| 159 |
* @param {Node} node The root node. |
| 160 |
* @param {string} path Xpath selector. |
| 161 |
* @return {Node} The selected node, or null if no matching node. |
| 162 |
*/ |
| 163 |
goog.dom.xml.selectSingleNode = function(node, path) { |
| 164 |
if (typeof node.selectSingleNode != 'undefined') { |
| 165 |
var doc = goog.dom.getOwnerDocument(node); |
| 166 |
if (typeof doc.setProperty != 'undefined') { |
| 167 |
doc.setProperty('SelectionLanguage', 'XPath'); |
| 168 |
} |
| 169 |
return node.selectSingleNode(path); |
| 170 |
} else if (document.implementation.hasFeature('XPath', '3.0')) { |
| 171 |
var doc = goog.dom.getOwnerDocument(node); |
| 172 |
var resolver = doc.createNSResolver(doc.documentElement); |
| 173 |
var result = doc.evaluate( |
| 174 |
path, node, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null); |
| 175 |
return result.singleNodeValue; |
| 176 |
} |
| 177 |
// This browser does not support xpath for the given node. If IE, ensure XML |
| 178 |
// Document was created using ActiveXObject |
| 179 |
// TODO(joeltine): This should throw instead of return null. |
| 180 |
return null; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Selects multiple nodes using an Xpath expression and a root node |
| 186 |
* @param {Node} node The root node. |
| 187 |
* @param {string} path Xpath selector. |
| 188 |
* @return {(!NodeList<!Node>|!Array<!Node>)} The selected nodes, or empty array |
| 189 |
* if no matching nodes. |
| 190 |
*/ |
| 191 |
goog.dom.xml.selectNodes = function(node, path) { |
| 192 |
if (typeof node.selectNodes != 'undefined') { |
| 193 |
var doc = goog.dom.getOwnerDocument(node); |
| 194 |
if (typeof doc.setProperty != 'undefined') { |
| 195 |
doc.setProperty('SelectionLanguage', 'XPath'); |
| 196 |
} |
| 197 |
return node.selectNodes(path); |
| 198 |
} else if (document.implementation.hasFeature('XPath', '3.0')) { |
| 199 |
var doc = goog.dom.getOwnerDocument(node); |
| 200 |
var resolver = doc.createNSResolver(doc.documentElement); |
| 201 |
var nodes = doc.evaluate( |
| 202 |
path, node, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); |
| 203 |
var results = []; |
| 204 |
var count = nodes.snapshotLength; |
| 205 |
for (var i = 0; i < count; i++) { |
| 206 |
results.push(nodes.snapshotItem(i)); |
| 207 |
} |
| 208 |
return results; |
| 209 |
} else { |
| 210 |
// This browser does not support xpath for the given node. If IE, ensure XML |
| 211 |
// Document was created using ActiveXObject. |
| 212 |
// TODO(joeltine): This should throw instead of return empty array. |
| 213 |
return []; |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Sets multiple attributes on an element. Differs from goog.dom.setProperties |
| 220 |
* in that it exclusively uses the element's setAttributes method. Use this |
| 221 |
* when you need to ensure that the exact property is available as an attribute |
| 222 |
* and can be read later by the native getAttribute method. |
| 223 |
* @param {!Element} element XML or DOM element to set attributes on. |
| 224 |
* @param {!Object<string, string>} attributes Map of property:value pairs. |
| 225 |
*/ |
| 226 |
goog.dom.xml.setAttributes = function(element, attributes) { |
| 227 |
for (var key in attributes) { |
| 228 |
if (attributes.hasOwnProperty(key)) { |
| 229 |
element.setAttribute(key, attributes[key]); |
| 230 |
} |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Creates an instance of the MSXML2.DOMDocument. |
| 237 |
* @return {Document} The new document. |
| 238 |
* @private |
| 239 |
*/ |
| 240 |
goog.dom.xml.createMsXmlDocument_ = function() { |
| 241 |
var doc = new ActiveXObject('MSXML2.DOMDocument'); |
| 242 |
if (doc) { |
| 243 |
// Prevent potential vulnerabilities exposed by MSXML2, see |
| 244 |
// http://b/1707300 and http://wiki/Main/ISETeamXMLAttacks for details. |
| 245 |
doc.resolveExternals = false; |
| 246 |
doc.validateOnParse = false; |
| 247 |
// Add a try catch block because accessing these properties will throw an |
| 248 |
// error on unsupported MSXML versions. This affects Windows machines |
| 249 |
// running IE6 or IE7 that are on XP SP2 or earlier without MSXML updates. |
| 250 |
// See http://msdn.microsoft.com/en-us/library/ms766391(VS.85).aspx for |
| 251 |
// specific details on which MSXML versions support these properties. |
| 252 |
try { |
| 253 |
doc.setProperty('ProhibitDTD', true); |
| 254 |
doc.setProperty('MaxXMLSize', goog.dom.xml.MAX_XML_SIZE_KB); |
| 255 |
doc.setProperty('MaxElementDepth', goog.dom.xml.MAX_ELEMENT_DEPTH); |
| 256 |
} catch (e) { |
| 257 |
// No-op. |
| 258 |
} |
| 259 |
} |
| 260 |
return doc; |
| 261 |
}; |