/testing/selenium-core/xpath/util.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 549 lines · 384 code · 69 blank · 96 comment · 118 complexity · d41835e4919b4163b6e0644850319e3a MD5 · raw file

  1. // Copyright 2005 Google
  2. //
  3. // Author: Steffen Meschkat <mesch@google.com>
  4. //
  5. // Miscellaneous utility and placeholder functions.
  6. // Dummy implmentation for the logging functions. Replace by something
  7. // useful when you want to debug.
  8. function xpathLog(msg) {};
  9. function xsltLog(msg) {};
  10. function xsltLogXml(msg) {};
  11. var ajaxsltIsIE6 = navigator.appVersion.match(/MSIE 6.0/);
  12. // Throws an exception if false.
  13. function assert(b) {
  14. if (!b) {
  15. throw "Assertion failed";
  16. }
  17. }
  18. // Splits a string s at all occurrences of character c. This is like
  19. // the split() method of the string object, but IE omits empty
  20. // strings, which violates the invariant (s.split(x).join(x) == s).
  21. function stringSplit(s, c) {
  22. var a = s.indexOf(c);
  23. if (a == -1) {
  24. return [ s ];
  25. }
  26. var parts = [];
  27. parts.push(s.substr(0,a));
  28. while (a != -1) {
  29. var a1 = s.indexOf(c, a + 1);
  30. if (a1 != -1) {
  31. parts.push(s.substr(a + 1, a1 - a - 1));
  32. } else {
  33. parts.push(s.substr(a + 1));
  34. }
  35. a = a1;
  36. }
  37. return parts;
  38. }
  39. // The following function does what document.importNode(node, true)
  40. // would do for us here; however that method is broken in Safari/1.3,
  41. // so we have to emulate it.
  42. function xmlImportNode(doc, node) {
  43. if (node.nodeType == DOM_TEXT_NODE) {
  44. return domCreateTextNode(doc, node.nodeValue);
  45. } else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
  46. return domCreateCDATASection(doc, node.nodeValue);
  47. } else if (node.nodeType == DOM_ELEMENT_NODE) {
  48. var newNode = domCreateElement(doc, node.nodeName);
  49. for (var i = 0; i < node.attributes.length; ++i) {
  50. var an = node.attributes[i];
  51. var name = an.nodeName;
  52. var value = an.nodeValue;
  53. domSetAttribute(newNode, name, value);
  54. }
  55. for (var c = node.firstChild; c; c = c.nextSibling) {
  56. var cn = arguments.callee(doc, c);
  57. domAppendChild(newNode, cn);
  58. }
  59. return newNode;
  60. } else {
  61. return domCreateComment(doc, node.nodeName);
  62. }
  63. }
  64. // A set data structure. It can also be used as a map (i.e. the keys
  65. // can have values other than 1), but we don't call it map because it
  66. // would be ambiguous in this context. Also, the map is iterable, so
  67. // we can use it to replace for-in loops over core javascript Objects.
  68. // For-in iteration breaks when Object.prototype is modified, which
  69. // some clients of the maps API do.
  70. //
  71. // NOTE(mesch): The set keys by the string value of its element, NOT
  72. // by the typed value. In particular, objects can't be used as keys.
  73. //
  74. // @constructor
  75. function Set() {
  76. this.keys = [];
  77. }
  78. Set.prototype.size = function() {
  79. return this.keys.length;
  80. }
  81. // Adds the entry to the set, ignoring if it is present.
  82. Set.prototype.add = function(key, opt_value) {
  83. var value = opt_value || 1;
  84. if (!this.contains(key)) {
  85. this[':' + key] = value;
  86. this.keys.push(key);
  87. }
  88. }
  89. // Sets the entry in the set, adding if it is not yet present.
  90. Set.prototype.set = function(key, opt_value) {
  91. var value = opt_value || 1;
  92. if (!this.contains(key)) {
  93. this[':' + key] = value;
  94. this.keys.push(key);
  95. } else {
  96. this[':' + key] = value;
  97. }
  98. }
  99. // Increments the key's value by 1. This works around the fact that
  100. // numbers are always passed by value, never by reference, so that we
  101. // can't increment the value returned by get(), or the iterator
  102. // argument. Sets the key's value to 1 if it doesn't exist yet.
  103. Set.prototype.inc = function(key) {
  104. if (!this.contains(key)) {
  105. this[':' + key] = 1;
  106. this.keys.push(key);
  107. } else {
  108. this[':' + key]++;
  109. }
  110. }
  111. Set.prototype.get = function(key) {
  112. if (this.contains(key)) {
  113. return this[':' + key];
  114. } else {
  115. var undefined;
  116. return undefined;
  117. }
  118. }
  119. // Removes the entry from the set.
  120. Set.prototype.remove = function(key) {
  121. if (this.contains(key)) {
  122. delete this[':' + key];
  123. removeFromArray(this.keys, key, true);
  124. }
  125. }
  126. // Tests if an entry is in the set.
  127. Set.prototype.contains = function(entry) {
  128. return typeof this[':' + entry] != 'undefined';
  129. }
  130. // Gets a list of values in the set.
  131. Set.prototype.items = function() {
  132. var list = [];
  133. for (var i = 0; i < this.keys.length; ++i) {
  134. var k = this.keys[i];
  135. var v = this[':' + k];
  136. list.push(v);
  137. }
  138. return list;
  139. }
  140. // Invokes function f for every key value pair in the set as a method
  141. // of the set.
  142. Set.prototype.map = function(f) {
  143. for (var i = 0; i < this.keys.length; ++i) {
  144. var k = this.keys[i];
  145. f.call(this, k, this[':' + k]);
  146. }
  147. }
  148. Set.prototype.clear = function() {
  149. for (var i = 0; i < this.keys.length; ++i) {
  150. delete this[':' + this.keys[i]];
  151. }
  152. this.keys.length = 0;
  153. }
  154. // Applies the given function to each element of the array, preserving
  155. // this, and passing the index.
  156. function mapExec(array, func) {
  157. for (var i = 0; i < array.length; ++i) {
  158. func.call(this, array[i], i);
  159. }
  160. }
  161. // Returns an array that contains the return value of the given
  162. // function applied to every element of the input array.
  163. function mapExpr(array, func) {
  164. var ret = [];
  165. for (var i = 0; i < array.length; ++i) {
  166. ret.push(func(array[i]));
  167. }
  168. return ret;
  169. };
  170. // Reverses the given array in place.
  171. function reverseInplace(array) {
  172. for (var i = 0; i < array.length / 2; ++i) {
  173. var h = array[i];
  174. var ii = array.length - i - 1;
  175. array[i] = array[ii];
  176. array[ii] = h;
  177. }
  178. }
  179. // Removes value from array. Returns the number of instances of value
  180. // that were removed from array.
  181. function removeFromArray(array, value, opt_notype) {
  182. var shift = 0;
  183. for (var i = 0; i < array.length; ++i) {
  184. if (array[i] === value || (opt_notype && array[i] == value)) {
  185. array.splice(i--, 1);
  186. shift++;
  187. }
  188. }
  189. return shift;
  190. }
  191. // Shallow-copies an array to the end of another array
  192. // Basically Array.concat, but works with other non-array collections
  193. function copyArray(dst, src) {
  194. if (!src) return;
  195. var dstLength = dst.length;
  196. for (var i = src.length - 1; i >= 0; --i) {
  197. dst[i+dstLength] = src[i];
  198. }
  199. }
  200. /**
  201. * This is an optimization for copying attribute lists in IE. IE includes many
  202. * extraneous properties in its DOM attribute lists, which take require
  203. * significant extra processing when evaluating attribute steps. With this
  204. * function, we ignore any such attributes that has an empty string value.
  205. */
  206. function copyArrayIgnoringAttributesWithoutValue(dst, src)
  207. {
  208. if (!src) return;
  209. for (var i = src.length - 1; i >= 0; --i) {
  210. // this test will pass so long as the attribute has a non-empty string
  211. // value, even if that value is "false", "0", "undefined", etc.
  212. if (src[i].nodeValue) {
  213. dst.push(src[i]);
  214. }
  215. }
  216. }
  217. // Returns the text value of a node; for nodes without children this
  218. // is the nodeValue, for nodes with children this is the concatenation
  219. // of the value of all children. Browser-specific optimizations are used by
  220. // default; they can be disabled by passing "true" in as the second parameter.
  221. function xmlValue(node, disallowBrowserSpecificOptimization) {
  222. if (!node) {
  223. return '';
  224. }
  225. var ret = '';
  226. if (node.nodeType == DOM_TEXT_NODE ||
  227. node.nodeType == DOM_CDATA_SECTION_NODE) {
  228. ret += node.nodeValue;
  229. } else if (node.nodeType == DOM_ATTRIBUTE_NODE) {
  230. if (ajaxsltIsIE6) {
  231. ret += xmlValueIE6Hack(node);
  232. } else {
  233. ret += node.nodeValue;
  234. }
  235. } else if (node.nodeType == DOM_ELEMENT_NODE ||
  236. node.nodeType == DOM_DOCUMENT_NODE ||
  237. node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
  238. if (!disallowBrowserSpecificOptimization) {
  239. // IE, Safari, Opera, and friends
  240. var innerText = node.innerText;
  241. if (innerText != undefined) {
  242. return innerText;
  243. }
  244. // Firefox
  245. var textContent = node.textContent;
  246. if (textContent != undefined) {
  247. return textContent;
  248. }
  249. }
  250. // pobrecito!
  251. var len = node.childNodes.length;
  252. for (var i = 0; i < len; ++i) {
  253. ret += arguments.callee(node.childNodes[i]);
  254. }
  255. }
  256. return ret;
  257. }
  258. function xmlValueIE6Hack(node) {
  259. // Issue 19, IE6 mangles href attribute when it's a javascript: url
  260. var nodeName = node.nodeName;
  261. var nodeValue = node.nodeValue;
  262. if (nodeName.length != 4) return nodeValue;
  263. if (!/^href$/i.test(nodeName)) return nodeValue;
  264. if (!/^javascript:/.test(nodeValue)) return nodeValue;
  265. return unescape(nodeValue);
  266. }
  267. // Returns the representation of a node as XML text.
  268. function xmlText(node, opt_cdata) {
  269. var buf = [];
  270. xmlTextR(node, buf, opt_cdata);
  271. return buf.join('');
  272. }
  273. function xmlTextR(node, buf, cdata) {
  274. if (node.nodeType == DOM_TEXT_NODE) {
  275. buf.push(xmlEscapeText(node.nodeValue));
  276. } else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
  277. if (cdata) {
  278. buf.push(node.nodeValue);
  279. } else {
  280. buf.push('<![CDATA[' + node.nodeValue + ']]>');
  281. }
  282. } else if (node.nodeType == DOM_COMMENT_NODE) {
  283. buf.push('<!--' + node.nodeValue + '-->');
  284. } else if (node.nodeType == DOM_ELEMENT_NODE) {
  285. buf.push('<' + xmlFullNodeName(node));
  286. for (var i = 0; i < node.attributes.length; ++i) {
  287. var a = node.attributes[i];
  288. if (a && a.nodeName && a.nodeValue) {
  289. buf.push(' ' + xmlFullNodeName(a) + '="' +
  290. xmlEscapeAttr(a.nodeValue) + '"');
  291. }
  292. }
  293. if (node.childNodes.length == 0) {
  294. buf.push('/>');
  295. } else {
  296. buf.push('>');
  297. for (var i = 0; i < node.childNodes.length; ++i) {
  298. arguments.callee(node.childNodes[i], buf, cdata);
  299. }
  300. buf.push('</' + xmlFullNodeName(node) + '>');
  301. }
  302. } else if (node.nodeType == DOM_DOCUMENT_NODE ||
  303. node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
  304. for (var i = 0; i < node.childNodes.length; ++i) {
  305. arguments.callee(node.childNodes[i], buf, cdata);
  306. }
  307. }
  308. }
  309. function xmlFullNodeName(n) {
  310. if (n.prefix && n.nodeName.indexOf(n.prefix + ':') != 0) {
  311. return n.prefix + ':' + n.nodeName;
  312. } else {
  313. return n.nodeName;
  314. }
  315. }
  316. // Escape XML special markup chracters: tag delimiter < > and entity
  317. // reference start delimiter &. The escaped string can be used in XML
  318. // text portions (i.e. between tags).
  319. function xmlEscapeText(s) {
  320. return ('' + s).replace(/&/g, '&amp;').replace(/</g, '&lt;').
  321. replace(/>/g, '&gt;');
  322. }
  323. // Escape XML special markup characters: tag delimiter < > entity
  324. // reference start delimiter & and quotes ". The escaped string can be
  325. // used in double quoted XML attribute value portions (i.e. in
  326. // attributes within start tags).
  327. function xmlEscapeAttr(s) {
  328. return xmlEscapeText(s).replace(/\"/g, '&quot;');
  329. }
  330. // Escape markup in XML text, but don't touch entity references. The
  331. // escaped string can be used as XML text (i.e. between tags).
  332. function xmlEscapeTags(s) {
  333. return s.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  334. }
  335. /**
  336. * Wrapper function to access the owner document uniformly for document
  337. * and other nodes: for the document node, the owner document is the
  338. * node itself, for all others it's the ownerDocument property.
  339. *
  340. * @param {Node} node
  341. * @return {Document}
  342. */
  343. function xmlOwnerDocument(node) {
  344. if (node.nodeType == DOM_DOCUMENT_NODE) {
  345. return node;
  346. } else {
  347. return node.ownerDocument;
  348. }
  349. }
  350. // Wrapper around DOM methods so we can condense their invocations.
  351. function domGetAttribute(node, name) {
  352. return node.getAttribute(name);
  353. }
  354. function domSetAttribute(node, name, value) {
  355. return node.setAttribute(name, value);
  356. }
  357. function domRemoveAttribute(node, name) {
  358. return node.removeAttribute(name);
  359. }
  360. function domAppendChild(node, child) {
  361. return node.appendChild(child);
  362. }
  363. function domRemoveChild(node, child) {
  364. return node.removeChild(child);
  365. }
  366. function domReplaceChild(node, newChild, oldChild) {
  367. return node.replaceChild(newChild, oldChild);
  368. }
  369. function domInsertBefore(node, newChild, oldChild) {
  370. return node.insertBefore(newChild, oldChild);
  371. }
  372. function domRemoveNode(node) {
  373. return domRemoveChild(node.parentNode, node);
  374. }
  375. function domCreateTextNode(doc, text) {
  376. return doc.createTextNode(text);
  377. }
  378. function domCreateElement(doc, name) {
  379. return doc.createElement(name);
  380. }
  381. function domCreateAttribute(doc, name) {
  382. return doc.createAttribute(name);
  383. }
  384. function domCreateCDATASection(doc, data) {
  385. return doc.createCDATASection(data);
  386. }
  387. function domCreateComment(doc, text) {
  388. return doc.createComment(text);
  389. }
  390. function domCreateDocumentFragment(doc) {
  391. return doc.createDocumentFragment();
  392. }
  393. function domGetElementById(doc, id) {
  394. return doc.getElementById(id);
  395. }
  396. // Same for window methods.
  397. function windowSetInterval(win, fun, time) {
  398. return win.setInterval(fun, time);
  399. }
  400. function windowClearInterval(win, id) {
  401. return win.clearInterval(id);
  402. }
  403. /**
  404. * Escape the special regular expression characters when the regular expression
  405. * is specified as a string.
  406. *
  407. * Based on: http://simonwillison.net/2006/Jan/20/escape/
  408. */
  409. RegExp.escape = (function() {
  410. var specials = [
  411. '/', '.', '*', '+', '?', '|', '^', '$',
  412. '(', ')', '[', ']', '{', '}', '\\'
  413. ];
  414. var sRE = new RegExp(
  415. '(\\' + specials.join('|\\') + ')', 'g'
  416. );
  417. return function(text) {
  418. return text.replace(sRE, '\\$1');
  419. }
  420. })();
  421. /**
  422. * Determines whether a predicate expression contains a "positional selector".
  423. * A positional selector filters nodes from the nodelist input based on their
  424. * position within that list. When such selectors are encountered, the
  425. * evaluation of the predicate cannot be depth-first, because the positional
  426. * selector may be based on the result of evaluating predicates that precede
  427. * it.
  428. */
  429. function predicateExprHasPositionalSelector(expr, isRecursiveCall) {
  430. if (!expr) {
  431. return false;
  432. }
  433. if (!isRecursiveCall && exprReturnsNumberValue(expr)) {
  434. // this is a "proximity position"-based predicate
  435. return true;
  436. }
  437. if (expr instanceof FunctionCallExpr) {
  438. var value = expr.name.value;
  439. return (value == 'last' || value == 'position');
  440. }
  441. if (expr instanceof BinaryExpr) {
  442. return (
  443. predicateExprHasPositionalSelector(expr.expr1, true) ||
  444. predicateExprHasPositionalSelector(expr.expr2, true));
  445. }
  446. return false;
  447. }
  448. function exprReturnsNumberValue(expr) {
  449. if (expr instanceof FunctionCallExpr) {
  450. var isMember = {
  451. last: true
  452. , position: true
  453. , count: true
  454. , 'string-length': true
  455. , number: true
  456. , sum: true
  457. , floor: true
  458. , ceiling: true
  459. , round: true
  460. };
  461. return isMember[expr.name.value];
  462. }
  463. else if (expr instanceof UnaryMinusExpr) {
  464. return true;
  465. }
  466. else if (expr instanceof BinaryExpr) {
  467. var isMember = {
  468. '+': true
  469. , '-': true
  470. , '*': true
  471. , mod: true
  472. , div: true
  473. };
  474. return isMember[expr.op.value];
  475. }
  476. else if (expr instanceof NumberExpr) {
  477. return true;
  478. }
  479. return false;
  480. }