/testing/selenium-core/scripts/htmlutils.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 1551 lines · 1118 code · 132 blank · 301 comment · 226 complexity · bb8a530900c371528227e50d5f6c9989 MD5 · raw file

  1. /*
  2. * Copyright 2004 ThoughtWorks, Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. // This script contains a badly-organised collection of miscellaneous
  18. // functions that really better homes.
  19. function classCreate() {
  20. return function() {
  21. this.initialize.apply(this, arguments);
  22. }
  23. }
  24. function objectExtend(destination, source) {
  25. for (var property in source) {
  26. destination[property] = source[property];
  27. }
  28. return destination;
  29. }
  30. function sel$() {
  31. var results = [], element;
  32. for (var i = 0; i < arguments.length; i++) {
  33. element = arguments[i];
  34. if (typeof element == 'string')
  35. element = document.getElementById(element);
  36. results[results.length] = element;
  37. }
  38. return results.length < 2 ? results[0] : results;
  39. }
  40. function sel$A(iterable) {
  41. if (!iterable) return [];
  42. if (iterable.toArray) {
  43. return iterable.toArray();
  44. } else {
  45. var results = [];
  46. for (var i = 0; i < iterable.length; i++)
  47. results.push(iterable[i]);
  48. return results;
  49. }
  50. }
  51. function fnBind() {
  52. var args = sel$A(arguments), __method = args.shift(), object = args.shift();
  53. var retval = function() {
  54. return __method.apply(object, args.concat(sel$A(arguments)));
  55. }
  56. retval.__method = __method;
  57. return retval;
  58. }
  59. function fnBindAsEventListener(fn, object) {
  60. var __method = fn;
  61. return function(event) {
  62. return __method.call(object, event || window.event);
  63. }
  64. }
  65. function removeClassName(element, name) {
  66. var re = new RegExp("\\b" + name + "\\b", "g");
  67. element.className = element.className.replace(re, "");
  68. }
  69. function addClassName(element, name) {
  70. element.className = element.className + ' ' + name;
  71. }
  72. function elementSetStyle(element, style) {
  73. for (var name in style) {
  74. var value = style[name];
  75. if (value == null) value = "";
  76. element.style[name] = value;
  77. }
  78. }
  79. function elementGetStyle(element, style) {
  80. var value = element.style[style];
  81. if (!value) {
  82. if (document.defaultView && document.defaultView.getComputedStyle) {
  83. var css = document.defaultView.getComputedStyle(element, null);
  84. value = css ? css.getPropertyValue(style) : null;
  85. } else if (element.currentStyle) {
  86. value = element.currentStyle[style];
  87. }
  88. }
  89. /** DGF necessary?
  90. if (window.opera && ['left', 'top', 'right', 'bottom'].include(style))
  91. if (Element.getStyle(element, 'position') == 'static') value = 'auto'; */
  92. return value == 'auto' ? null : value;
  93. }
  94. String.prototype.trim = function() {
  95. var result = this.replace(/^\s+/g, "");
  96. // strip leading
  97. return result.replace(/\s+$/g, "");
  98. // strip trailing
  99. };
  100. String.prototype.lcfirst = function() {
  101. return this.charAt(0).toLowerCase() + this.substr(1);
  102. };
  103. String.prototype.ucfirst = function() {
  104. return this.charAt(0).toUpperCase() + this.substr(1);
  105. };
  106. String.prototype.startsWith = function(str) {
  107. return this.indexOf(str) == 0;
  108. };
  109. /**
  110. * Given a string literal that would appear in an XPath, puts it in quotes and
  111. * returns it. Special consideration is given to literals who themselves
  112. * contain quotes. It's possible for a concat() expression to be returned.
  113. */
  114. String.prototype.quoteForXPath = function()
  115. {
  116. if (/\'/.test(this)) {
  117. if (/\"/.test(this)) {
  118. // concat scenario
  119. var pieces = [];
  120. var a = "'", b = '"', c;
  121. for (var i = 0, j = 0; i < this.length;) {
  122. if (this.charAt(i) == a) {
  123. // encountered a quote that cannot be contained in current
  124. // quote, so need to flip-flop quoting scheme
  125. if (j < i) {
  126. pieces.push(a + this.substring(j, i) + a);
  127. j = i;
  128. }
  129. c = a;
  130. a = b;
  131. b = c;
  132. }
  133. else {
  134. ++i;
  135. }
  136. }
  137. pieces.push(a + this.substring(j) + a);
  138. return 'concat(' + pieces.join(', ') + ')';
  139. }
  140. else {
  141. // quote with doubles
  142. return '"' + this + '"';
  143. }
  144. }
  145. // quote with singles
  146. return "'" + this + "'";
  147. };
  148. // Returns the text in this element
  149. function getText(element) {
  150. var text = "";
  151. var isRecentFirefox = (browserVersion.isFirefox && browserVersion.firefoxVersion >= "1.5");
  152. if (isRecentFirefox || browserVersion.isKonqueror || browserVersion.isSafari || browserVersion.isOpera) {
  153. text = getTextContent(element);
  154. } else if (element.textContent) {
  155. text = element.textContent;
  156. } else if (element.innerText) {
  157. text = element.innerText;
  158. }
  159. text = normalizeNewlines(text);
  160. text = normalizeSpaces(text);
  161. return text.trim();
  162. }
  163. function getTextContent(element, preformatted) {
  164. if (element.nodeType == 3 /*Node.TEXT_NODE*/) {
  165. var text = element.data;
  166. if (!preformatted) {
  167. text = text.replace(/\n|\r|\t/g, " ");
  168. }
  169. return text;
  170. }
  171. if (element.nodeType == 1 /*Node.ELEMENT_NODE*/) {
  172. var childrenPreformatted = preformatted || (element.tagName == "PRE");
  173. var text = "";
  174. for (var i = 0; i < element.childNodes.length; i++) {
  175. var child = element.childNodes.item(i);
  176. text += getTextContent(child, childrenPreformatted);
  177. }
  178. // Handle block elements that introduce newlines
  179. // -- From HTML spec:
  180. //<!ENTITY % block
  181. // "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
  182. // BLOCKQUOTE | F:wORM | HR | TABLE | FIELDSET | ADDRESS">
  183. //
  184. // TODO: should potentially introduce multiple newlines to separate blocks
  185. if (element.tagName == "P" || element.tagName == "BR" || element.tagName == "HR" || element.tagName == "DIV") {
  186. text += "\n";
  187. }
  188. return text;
  189. }
  190. return '';
  191. }
  192. /**
  193. * Convert all newlines to \n
  194. */
  195. function normalizeNewlines(text)
  196. {
  197. return text.replace(/\r\n|\r/g, "\n");
  198. }
  199. /**
  200. * Replace multiple sequential spaces with a single space, and then convert &nbsp; to space.
  201. */
  202. function normalizeSpaces(text)
  203. {
  204. // IE has already done this conversion, so doing it again will remove multiple nbsp
  205. if (browserVersion.isIE)
  206. {
  207. return text;
  208. }
  209. // Replace multiple spaces with a single space
  210. // TODO - this shouldn't occur inside PRE elements
  211. text = text.replace(/\ +/g, " ");
  212. // Replace &nbsp; with a space
  213. var nbspPattern = new RegExp(String.fromCharCode(160), "g");
  214. if (browserVersion.isSafari) {
  215. return replaceAll(text, String.fromCharCode(160), " ");
  216. } else {
  217. return text.replace(nbspPattern, " ");
  218. }
  219. }
  220. function replaceAll(text, oldText, newText) {
  221. while (text.indexOf(oldText) != -1) {
  222. text = text.replace(oldText, newText);
  223. }
  224. return text;
  225. }
  226. function xmlDecode(text) {
  227. text = text.replace(/&quot;/g, '"');
  228. text = text.replace(/&apos;/g, "'");
  229. text = text.replace(/&lt;/g, "<");
  230. text = text.replace(/&gt;/g, ">");
  231. text = text.replace(/&amp;/g, "&");
  232. return text;
  233. }
  234. // Sets the text in this element
  235. function setText(element, text) {
  236. if (element.textContent != null) {
  237. element.textContent = text;
  238. } else if (element.innerText != null) {
  239. element.innerText = text;
  240. }
  241. }
  242. // Get the value of an <input> element
  243. function getInputValue(inputElement) {
  244. if (inputElement.type) {
  245. if (inputElement.type.toUpperCase() == 'CHECKBOX' ||
  246. inputElement.type.toUpperCase() == 'RADIO')
  247. {
  248. return (inputElement.checked ? 'on' : 'off');
  249. }
  250. }
  251. if (inputElement.value == null) {
  252. throw new SeleniumError("This element has no value; is it really a form field?");
  253. }
  254. return inputElement.value;
  255. }
  256. /* Fire an event in a browser-compatible manner */
  257. function triggerEvent(element, eventType, canBubble, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) {
  258. canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
  259. if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { // IE
  260. var evt = createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown);
  261. element.fireEvent('on' + eventType, evt);
  262. }
  263. else {
  264. var evt = document.createEvent('HTMLEvents');
  265. try {
  266. evt.shiftKey = shiftKeyDown;
  267. evt.metaKey = metaKeyDown;
  268. evt.altKey = altKeyDown;
  269. evt.ctrlKey = controlKeyDown;
  270. } catch (e) {
  271. // On Firefox 1.0, you can only set these during initMouseEvent or initKeyEvent
  272. // we'll have to ignore them here
  273. LOG.exception(e);
  274. }
  275. evt.initEvent(eventType, canBubble, true);
  276. element.dispatchEvent(evt);
  277. }
  278. }
  279. function getKeyCodeFromKeySequence(keySequence) {
  280. var match = /^\\(\d{1,3})$/.exec(keySequence);
  281. if (match != null) {
  282. return match[1];
  283. }
  284. match = /^.$/.exec(keySequence);
  285. if (match != null) {
  286. return match[0].charCodeAt(0);
  287. }
  288. // this is for backward compatibility with existing tests
  289. // 1 digit ascii codes will break however because they are used for the digit chars
  290. match = /^\d{2,3}$/.exec(keySequence);
  291. if (match != null) {
  292. return match[0];
  293. }
  294. throw new SeleniumError("invalid keySequence");
  295. }
  296. function createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) {
  297. var evt = element.ownerDocument.createEventObject();
  298. evt.shiftKey = shiftKeyDown;
  299. evt.metaKey = metaKeyDown;
  300. evt.altKey = altKeyDown;
  301. evt.ctrlKey = controlKeyDown;
  302. return evt;
  303. }
  304. function triggerKeyEvent(element, eventType, keySequence, canBubble, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) {
  305. var keycode = getKeyCodeFromKeySequence(keySequence);
  306. canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
  307. if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { // IE
  308. var keyEvent = createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown);
  309. keyEvent.keyCode = keycode;
  310. element.fireEvent('on' + eventType, keyEvent);
  311. }
  312. else {
  313. var evt;
  314. if (window.KeyEvent) {
  315. evt = document.createEvent('KeyEvents');
  316. evt.initKeyEvent(eventType, true, true, window, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown, keycode, keycode);
  317. } else {
  318. evt = document.createEvent('UIEvents');
  319. evt.shiftKey = shiftKeyDown;
  320. evt.metaKey = metaKeyDown;
  321. evt.altKey = altKeyDown;
  322. evt.ctrlKey = controlKeyDown;
  323. evt.initUIEvent(eventType, true, true, window, 1);
  324. evt.keyCode = keycode;
  325. evt.which = keycode;
  326. }
  327. element.dispatchEvent(evt);
  328. }
  329. }
  330. function removeLoadListener(element, command) {
  331. LOG.debug('Removing loadListenter for ' + element + ', ' + command);
  332. if (window.removeEventListener)
  333. element.removeEventListener("load", command, true);
  334. else if (window.detachEvent)
  335. element.detachEvent("onload", command);
  336. }
  337. function addLoadListener(element, command) {
  338. LOG.debug('Adding loadListenter for ' + element + ', ' + command);
  339. var augmentedCommand = function() {
  340. command.call(this, element);
  341. }
  342. if (window.addEventListener && !browserVersion.isOpera)
  343. element.addEventListener("load", augmentedCommand, true);
  344. else if (window.attachEvent)
  345. element.attachEvent("onload", augmentedCommand);
  346. }
  347. /**
  348. * Override the broken getFunctionName() method from JsUnit
  349. * This file must be loaded _after_ the jsunitCore.js
  350. */
  351. function getFunctionName(aFunction) {
  352. var regexpResult = aFunction.toString().match(/function (\w*)/);
  353. if (regexpResult && regexpResult[1]) {
  354. return regexpResult[1];
  355. }
  356. return 'anonymous';
  357. }
  358. function getDocumentBase(doc) {
  359. var bases = document.getElementsByTagName("base");
  360. if (bases && bases.length && bases[0].href) {
  361. return bases[0].href;
  362. }
  363. return "";
  364. }
  365. function getTagName(element) {
  366. var tagName;
  367. if (element && element.tagName && element.tagName.toLowerCase) {
  368. tagName = element.tagName.toLowerCase();
  369. }
  370. return tagName;
  371. }
  372. function selArrayToString(a) {
  373. if (isArray(a)) {
  374. // DGF copying the array, because the array-like object may be a non-modifiable nodelist
  375. var retval = [];
  376. for (var i = 0; i < a.length; i++) {
  377. var item = a[i];
  378. var replaced = new String(item).replace(/([,\\])/g, '\\$1');
  379. retval[i] = replaced;
  380. }
  381. return retval;
  382. }
  383. return new String(a);
  384. }
  385. function isArray(x) {
  386. return ((typeof x) == "object") && (x["length"] != null);
  387. }
  388. function absolutify(url, baseUrl) {
  389. /** returns a relative url in its absolute form, given by baseUrl.
  390. *
  391. * This function is a little odd, because it can take baseUrls that
  392. * aren't necessarily directories. It uses the same rules as the HTML
  393. * &lt;base&gt; tag; if the baseUrl doesn't end with "/", we'll assume
  394. * that it points to a file, and strip the filename off to find its
  395. * base directory.
  396. *
  397. * So absolutify("foo", "http://x/bar") will return "http://x/foo" (stripping off bar),
  398. * whereas absolutify("foo", "http://x/bar/") will return "http://x/bar/foo" (preserving bar).
  399. * Naturally absolutify("foo", "http://x") will return "http://x/foo", appropriately.
  400. *
  401. * @param url the url to make absolute; if this url is already absolute, we'll just return that, unchanged
  402. * @param baseUrl the baseUrl from which we'll absolutify, following the rules above.
  403. * @return 'url' if it was already absolute, or the absolutized version of url if it was not absolute.
  404. */
  405. // DGF isn't there some library we could use for this?
  406. if (/^\w+:/.test(url)) {
  407. // it's already absolute
  408. return url;
  409. }
  410. var loc;
  411. try {
  412. loc = parseUrl(baseUrl);
  413. } catch (e) {
  414. // is it an absolute windows file path? let's play the hero in that case
  415. if (/^\w:\\/.test(baseUrl)) {
  416. baseUrl = "file:///" + baseUrl.replace(/\\/g, "/");
  417. loc = parseUrl(baseUrl);
  418. } else {
  419. throw new SeleniumError("baseUrl wasn't absolute: " + baseUrl);
  420. }
  421. }
  422. loc.search = null;
  423. loc.hash = null;
  424. // if url begins with /, then that's the whole pathname
  425. if (/^\//.test(url)) {
  426. loc.pathname = url;
  427. var result = reassembleLocation(loc);
  428. return result;
  429. }
  430. // if pathname is null, then we'll just append "/" + the url
  431. if (!loc.pathname) {
  432. loc.pathname = "/" + url;
  433. var result = reassembleLocation(loc);
  434. return result;
  435. }
  436. // if pathname ends with /, just append url
  437. if (/\/$/.test(loc.pathname)) {
  438. loc.pathname += url;
  439. var result = reassembleLocation(loc);
  440. return result;
  441. }
  442. // if we're here, then the baseUrl has a pathname, but it doesn't end with /
  443. // in that case, we replace everything after the final / with the relative url
  444. loc.pathname = loc.pathname.replace(/[^\/\\]+$/, url);
  445. var result = reassembleLocation(loc);
  446. return result;
  447. }
  448. var URL_REGEX = /^((\w+):\/\/)(([^:]+):?([^@]+)?@)?([^\/\?:]*):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(.+)?/;
  449. function parseUrl(url) {
  450. var fields = ['url', null, 'protocol', null, 'username', 'password', 'host', 'port', 'pathname', 'search', 'hash'];
  451. var result = URL_REGEX.exec(url);
  452. if (!result) {
  453. throw new SeleniumError("Invalid URL: " + url);
  454. }
  455. var loc = new Object();
  456. for (var i = 0; i < fields.length; i++) {
  457. var field = fields[i];
  458. if (field == null) {
  459. continue;
  460. }
  461. loc[field] = result[i];
  462. }
  463. return loc;
  464. }
  465. function reassembleLocation(loc) {
  466. if (!loc.protocol) {
  467. throw new Error("Not a valid location object: " + o2s(loc));
  468. }
  469. var protocol = loc.protocol;
  470. protocol = protocol.replace(/:$/, "");
  471. var url = protocol + "://";
  472. if (loc.username) {
  473. url += loc.username;
  474. if (loc.password) {
  475. url += ":" + loc.password;
  476. }
  477. url += "@";
  478. }
  479. if (loc.host) {
  480. url += loc.host;
  481. }
  482. if (loc.port) {
  483. url += ":" + loc.port;
  484. }
  485. if (loc.pathname) {
  486. url += loc.pathname;
  487. }
  488. if (loc.search) {
  489. url += "?" + loc.search;
  490. }
  491. if (loc.hash) {
  492. var hash = loc.hash;
  493. hash = loc.hash.replace(/^#/, "");
  494. url += "#" + hash;
  495. }
  496. return url;
  497. }
  498. function canonicalize(url) {
  499. var tempLink = window.document.createElement("link");
  500. tempLink.href = url; // this will canonicalize the href on most browsers
  501. var loc = parseUrl(tempLink.href)
  502. if (!/\/\.\.\//.test(loc.pathname)) {
  503. return tempLink.href;
  504. }
  505. // didn't work... let's try it the hard way
  506. var originalParts = loc.pathname.split("/");
  507. var newParts = [];
  508. newParts.push(originalParts.shift());
  509. for (var i = 0; i < originalParts.length; i++) {
  510. var part = originalParts[i];
  511. if (".." == part) {
  512. newParts.pop();
  513. continue;
  514. }
  515. newParts.push(part);
  516. }
  517. loc.pathname = newParts.join("/");
  518. return reassembleLocation(loc);
  519. }
  520. function extractExceptionMessage(ex) {
  521. if (ex == null) return "null exception";
  522. if (ex.message != null) return ex.message;
  523. if (ex.toString && ex.toString() != null) return ex.toString();
  524. }
  525. function describe(object, delimiter) {
  526. var props = new Array();
  527. for (var prop in object) {
  528. try {
  529. props.push(prop + " -> " + object[prop]);
  530. } catch (e) {
  531. props.push(prop + " -> [htmlutils: ack! couldn't read this property! (Permission Denied?)]");
  532. }
  533. }
  534. return props.join(delimiter || '\n');
  535. }
  536. var PatternMatcher = function(pattern) {
  537. this.selectStrategy(pattern);
  538. };
  539. PatternMatcher.prototype = {
  540. selectStrategy: function(pattern) {
  541. this.pattern = pattern;
  542. var strategyName = 'glob';
  543. // by default
  544. if (/^([a-z-]+):(.*)/.test(pattern)) {
  545. var possibleNewStrategyName = RegExp.$1;
  546. var possibleNewPattern = RegExp.$2;
  547. if (PatternMatcher.strategies[possibleNewStrategyName]) {
  548. strategyName = possibleNewStrategyName;
  549. pattern = possibleNewPattern;
  550. }
  551. }
  552. var matchStrategy = PatternMatcher.strategies[strategyName];
  553. if (!matchStrategy) {
  554. throw new SeleniumError("cannot find PatternMatcher.strategies." + strategyName);
  555. }
  556. this.strategy = matchStrategy;
  557. this.matcher = new matchStrategy(pattern);
  558. },
  559. matches: function(actual) {
  560. return this.matcher.matches(actual + '');
  561. // Note: appending an empty string avoids a Konqueror bug
  562. }
  563. };
  564. /**
  565. * A "static" convenience method for easy matching
  566. */
  567. PatternMatcher.matches = function(pattern, actual) {
  568. return new PatternMatcher(pattern).matches(actual);
  569. };
  570. PatternMatcher.strategies = {
  571. /**
  572. * Exact matching, e.g. "exact:***"
  573. */
  574. exact: function(expected) {
  575. this.expected = expected;
  576. this.matches = function(actual) {
  577. return actual == this.expected;
  578. };
  579. },
  580. /**
  581. * Match by regular expression, e.g. "regexp:^[0-9]+$"
  582. */
  583. regexp: function(regexpString) {
  584. this.regexp = new RegExp(regexpString);
  585. this.matches = function(actual) {
  586. return this.regexp.test(actual);
  587. };
  588. },
  589. regex: function(regexpString) {
  590. this.regexp = new RegExp(regexpString);
  591. this.matches = function(actual) {
  592. return this.regexp.test(actual);
  593. };
  594. },
  595. regexpi: function(regexpString) {
  596. this.regexp = new RegExp(regexpString, "i");
  597. this.matches = function(actual) {
  598. return this.regexp.test(actual);
  599. };
  600. },
  601. regexi: function(regexpString) {
  602. this.regexp = new RegExp(regexpString, "i");
  603. this.matches = function(actual) {
  604. return this.regexp.test(actual);
  605. };
  606. },
  607. /**
  608. * "globContains" (aka "wildmat") patterns, e.g. "glob:one,two,*",
  609. * but don't require a perfect match; instead succeed if actual
  610. * contains something that matches globString.
  611. * Making this distinction is motivated by a bug in IE6 which
  612. * leads to the browser hanging if we implement *TextPresent tests
  613. * by just matching against a regular expression beginning and
  614. * ending with ".*". The globcontains strategy allows us to satisfy
  615. * the functional needs of the *TextPresent ops more efficiently
  616. * and so avoid running into this IE6 freeze.
  617. */
  618. globContains: function(globString) {
  619. this.regexp = new RegExp(PatternMatcher.regexpFromGlobContains(globString));
  620. this.matches = function(actual) {
  621. return this.regexp.test(actual);
  622. };
  623. },
  624. /**
  625. * "glob" (aka "wildmat") patterns, e.g. "glob:one,two,*"
  626. */
  627. glob: function(globString) {
  628. this.regexp = new RegExp(PatternMatcher.regexpFromGlob(globString));
  629. this.matches = function(actual) {
  630. return this.regexp.test(actual);
  631. };
  632. }
  633. };
  634. PatternMatcher.convertGlobMetaCharsToRegexpMetaChars = function(glob) {
  635. var re = glob;
  636. re = re.replace(/([.^$+(){}\[\]\\|])/g, "\\$1");
  637. re = re.replace(/\?/g, "(.|[\r\n])");
  638. re = re.replace(/\*/g, "(.|[\r\n])*");
  639. return re;
  640. };
  641. PatternMatcher.regexpFromGlobContains = function(globContains) {
  642. return PatternMatcher.convertGlobMetaCharsToRegexpMetaChars(globContains);
  643. };
  644. PatternMatcher.regexpFromGlob = function(glob) {
  645. return "^" + PatternMatcher.convertGlobMetaCharsToRegexpMetaChars(glob) + "$";
  646. };
  647. if (!this["Assert"]) Assert = {};
  648. Assert.fail = function(message) {
  649. throw new AssertionFailedError(message);
  650. };
  651. /*
  652. * Assert.equals(comment?, expected, actual)
  653. */
  654. Assert.equals = function() {
  655. var args = new AssertionArguments(arguments);
  656. if (args.expected === args.actual) {
  657. return;
  658. }
  659. Assert.fail(args.comment +
  660. "Expected '" + args.expected +
  661. "' but was '" + args.actual + "'");
  662. };
  663. Assert.assertEquals = Assert.equals;
  664. /*
  665. * Assert.matches(comment?, pattern, actual)
  666. */
  667. Assert.matches = function() {
  668. var args = new AssertionArguments(arguments);
  669. if (PatternMatcher.matches(args.expected, args.actual)) {
  670. return;
  671. }
  672. Assert.fail(args.comment +
  673. "Actual value '" + args.actual +
  674. "' did not match '" + args.expected + "'");
  675. }
  676. /*
  677. * Assert.notMtches(comment?, pattern, actual)
  678. */
  679. Assert.notMatches = function() {
  680. var args = new AssertionArguments(arguments);
  681. if (!PatternMatcher.matches(args.expected, args.actual)) {
  682. return;
  683. }
  684. Assert.fail(args.comment +
  685. "Actual value '" + args.actual +
  686. "' did match '" + args.expected + "'");
  687. }
  688. // Preprocess the arguments to allow for an optional comment.
  689. function AssertionArguments(args) {
  690. if (args.length == 2) {
  691. this.comment = "";
  692. this.expected = args[0];
  693. this.actual = args[1];
  694. } else {
  695. this.comment = args[0] + "; ";
  696. this.expected = args[1];
  697. this.actual = args[2];
  698. }
  699. }
  700. function AssertionFailedError(message) {
  701. this.isAssertionFailedError = true;
  702. this.isSeleniumError = true;
  703. this.message = message;
  704. this.failureMessage = message;
  705. }
  706. function SeleniumError(message) {
  707. var error = new Error(message);
  708. if (typeof(arguments.caller) != 'undefined') { // IE, not ECMA
  709. var result = '';
  710. for (var a = arguments.caller; a != null; a = a.caller) {
  711. result += '> ' + a.callee.toString() + '\n';
  712. if (a.caller == a) {
  713. result += '*';
  714. break;
  715. }
  716. }
  717. error.stack = result;
  718. }
  719. error.isSeleniumError = true;
  720. return error;
  721. }
  722. function highlight(element) {
  723. var highLightColor = "yellow";
  724. if (element.originalColor == undefined) { // avoid picking up highlight
  725. element.originalColor = elementGetStyle(element, "background-color");
  726. }
  727. elementSetStyle(element, {"backgroundColor" : highLightColor});
  728. window.setTimeout(function() {
  729. try {
  730. //if element is orphan, probably page of it has already gone, so ignore
  731. if (!element.parentNode) {
  732. return;
  733. }
  734. elementSetStyle(element, {"backgroundColor" : element.originalColor});
  735. } catch (e) {} // DGF unhighlighting is very dangerous and low priority
  736. }, 200);
  737. }
  738. // for use from vs.2003 debugger
  739. function o2s(obj) {
  740. var s = "";
  741. for (key in obj) {
  742. var line = key + "->" + obj[key];
  743. line.replace("\n", " ");
  744. s += line + "\n";
  745. }
  746. return s;
  747. }
  748. var seenReadyStateWarning = false;
  749. function openSeparateApplicationWindow(url, suppressMozillaWarning) {
  750. // resize the Selenium window itself
  751. window.resizeTo(1200, 500);
  752. window.moveTo(window.screenX, 0);
  753. var appWindow = window.open(url + '?start=true', 'main');
  754. if (appWindow == null) {
  755. var errorMessage = "Couldn't open app window; is the pop-up blocker enabled?"
  756. LOG.error(errorMessage);
  757. throw new Error("Couldn't open app window; is the pop-up blocker enabled?");
  758. }
  759. try {
  760. var windowHeight = 500;
  761. if (window.outerHeight) {
  762. windowHeight = window.outerHeight;
  763. } else if (document.documentElement && document.documentElement.offsetHeight) {
  764. windowHeight = document.documentElement.offsetHeight;
  765. }
  766. if (window.screenLeft && !window.screenX) window.screenX = window.screenLeft;
  767. if (window.screenTop && !window.screenY) window.screenY = window.screenTop;
  768. appWindow.resizeTo(1200, screen.availHeight - windowHeight - 60);
  769. appWindow.moveTo(window.screenX, window.screenY + windowHeight + 25);
  770. } catch (e) {
  771. LOG.error("Couldn't resize app window");
  772. LOG.exception(e);
  773. }
  774. if (!suppressMozillaWarning && window.document.readyState == null && !seenReadyStateWarning) {
  775. alert("Beware! Mozilla bug 300992 means that we can't always reliably detect when a new page has loaded. Install the Selenium IDE extension or the readyState extension available from selenium.openqa.org to make page load detection more reliable.");
  776. seenReadyStateWarning = true;
  777. }
  778. return appWindow;
  779. }
  780. var URLConfiguration = classCreate();
  781. objectExtend(URLConfiguration.prototype, {
  782. initialize: function() {
  783. },
  784. _isQueryParameterTrue: function (name) {
  785. var parameterValue = this._getQueryParameter(name);
  786. if (parameterValue == null) return false;
  787. if (parameterValue.toLowerCase() == "true") return true;
  788. if (parameterValue.toLowerCase() == "on") return true;
  789. return false;
  790. },
  791. _getQueryParameter: function(searchKey) {
  792. var str = this.queryString
  793. if (str == null) return null;
  794. var clauses = str.split('&');
  795. for (var i = 0; i < clauses.length; i++) {
  796. var keyValuePair = clauses[i].split('=', 2);
  797. var key = unescape(keyValuePair[0]);
  798. if (key == searchKey) {
  799. return unescape(keyValuePair[1]);
  800. }
  801. }
  802. return null;
  803. },
  804. _extractArgs: function() {
  805. var str = SeleniumHTARunner.commandLine;
  806. if (str == null || str == "") return new Array();
  807. var matches = str.match(/(?:\"([^\"]+)\"|(?!\"([^\"]+)\")(\S+))/g);
  808. // We either want non quote stuff ([^"]+) surrounded by quotes
  809. // or we want to look-ahead, see that the next character isn't
  810. // a quoted argument, and then grab all the non-space stuff
  811. // this will return for the line: "foo" bar
  812. // the results "\"foo\"" and "bar"
  813. // So, let's unquote the quoted arguments:
  814. var args = new Array;
  815. for (var i = 0; i < matches.length; i++) {
  816. args[i] = matches[i];
  817. args[i] = args[i].replace(/^"(.*)"$/, "$1");
  818. }
  819. return args;
  820. },
  821. isMultiWindowMode:function() {
  822. return this._isQueryParameterTrue('multiWindow');
  823. },
  824. getBaseUrl:function() {
  825. return this._getQueryParameter('baseUrl');
  826. }
  827. });
  828. function safeScrollIntoView(element) {
  829. if (element.scrollIntoView) {
  830. element.scrollIntoView(false);
  831. return;
  832. }
  833. // TODO: work out how to scroll browsers that don't support
  834. // scrollIntoView (like Konqueror)
  835. }
  836. /**
  837. * Returns true iff the current environment is the IDE.
  838. */
  839. function is_IDE()
  840. {
  841. return (typeof(SeleniumIDE) != 'undefined');
  842. }
  843. /**
  844. * Logs a message if the Logger exists, and does nothing if it doesn't exist.
  845. *
  846. * @param level the level to log at
  847. * @param msg the message to log
  848. */
  849. function safe_log(level, msg)
  850. {
  851. try {
  852. LOG[level](msg);
  853. }
  854. catch (e) {
  855. // couldn't log!
  856. }
  857. }
  858. /**
  859. * Displays a warning message to the user appropriate to the context under
  860. * which the issue is encountered. This is primarily used to avoid popping up
  861. * alert dialogs that might pause an automated test suite.
  862. *
  863. * @param msg the warning message to display
  864. */
  865. function safe_alert(msg)
  866. {
  867. if (is_IDE()) {
  868. alert(msg);
  869. }
  870. }
  871. //******************************************************************************
  872. // Locator evaluation support
  873. /**
  874. * Parses a Selenium locator, returning its type and the unprefixed locator
  875. * string as an object.
  876. *
  877. * @param locator the locator to parse
  878. */
  879. function parse_locator(locator)
  880. {
  881. var result = locator.match(/^([A-Za-z]+)=(.+)/);
  882. if (result) {
  883. return { type: result[1].toLowerCase(), string: result[2] };
  884. }
  885. return { type: 'implicit', string: locator };
  886. }
  887. /**
  888. * Evaluates an xpath on a document, and returns a list containing nodes in the
  889. * resulting nodeset. The browserbot xpath methods are now backed by this
  890. * function. A context node may optionally be provided, and the xpath will be
  891. * evaluated from that context.
  892. *
  893. * @param xpath the xpath to evaluate
  894. * @param inDocument the document in which to evaluate the xpath.
  895. * @param opts (optional) An object containing various flags that can
  896. * modify how the xpath is evaluated. Here's a listing of
  897. * the meaningful keys:
  898. *
  899. * contextNode:
  900. * the context node from which to evaluate the xpath. If
  901. * unspecified, the context will be the root document
  902. * element.
  903. *
  904. * namespaceResolver:
  905. * the namespace resolver function. Defaults to null.
  906. *
  907. * xpathLibrary:
  908. * the javascript library to use for XPath. "ajaxslt" is
  909. * the default. "javascript-xpath" is newer and faster,
  910. * but needs more testing.
  911. *
  912. * allowNativeXpath:
  913. * whether to allow native evaluate(). Defaults to true.
  914. *
  915. * ignoreAttributesWithoutValue:
  916. * whether it's ok to ignore attributes without value
  917. * when evaluating the xpath. This can greatly improve
  918. * performance in IE; however, if your xpaths depend on
  919. * such attributes, you can't ignore them! Defaults to
  920. * true.
  921. *
  922. * returnOnFirstMatch:
  923. * whether to optimize the XPath evaluation to only
  924. * return the first match. The match, if any, will still
  925. * be returned in a list. Defaults to false.
  926. */
  927. function eval_xpath(xpath, inDocument, opts)
  928. {
  929. if (!opts) {
  930. var opts = {};
  931. }
  932. var contextNode = opts.contextNode
  933. ? opts.contextNode : inDocument;
  934. var namespaceResolver = opts.namespaceResolver
  935. ? opts.namespaceResolver : null;
  936. var xpathLibrary = opts.xpathLibrary
  937. ? opts.xpathLibrary : null;
  938. var allowNativeXpath = (opts.allowNativeXpath != undefined)
  939. ? opts.allowNativeXpath : true;
  940. var ignoreAttributesWithoutValue = (opts.ignoreAttributesWithoutValue != undefined)
  941. ? opts.ignoreAttributesWithoutValue : true;
  942. var returnOnFirstMatch = (opts.returnOnFirstMatch != undefined)
  943. ? opts.returnOnFirstMatch : false;
  944. // Trim any trailing "/": not valid xpath, and remains from attribute
  945. // locator.
  946. if (xpath.charAt(xpath.length - 1) == '/') {
  947. xpath = xpath.slice(0, -1);
  948. }
  949. // HUGE hack - remove namespace from xpath for IE
  950. if (browserVersion && browserVersion.isIE) {
  951. xpath = xpath.replace(/x:/g, '')
  952. }
  953. // When using the new and faster javascript-xpath library,
  954. // we'll use the TestRunner's document object, not the App-Under-Test's document.
  955. // The new library only modifies the TestRunner document with the new
  956. // functionality.
  957. if (xpathLibrary == 'javascript-xpath') {
  958. documentForXpath = document;
  959. } else {
  960. documentForXpath = inDocument;
  961. }
  962. var results = [];
  963. // Use document.evaluate() if it's available
  964. if (allowNativeXpath && documentForXpath.evaluate) {
  965. try {
  966. // Regarding use of the second argument to document.evaluate():
  967. // http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/a59ce20639c74ba1/a9d9f53e88e5ebb5
  968. var xpathResult = documentForXpath
  969. .evaluate((contextNode == inDocument ? xpath : '.' + xpath),
  970. contextNode, namespaceResolver, 0, null);
  971. }
  972. catch (e) {
  973. throw new SeleniumError("Invalid xpath: " + extractExceptionMessage(e));
  974. }
  975. finally{
  976. if (xpathResult == null) {
  977. // If the result is null, we should still throw an Error.
  978. throw new SeleniumError("Invalid xpath: " + xpath);
  979. }
  980. }
  981. var result = xpathResult.iterateNext();
  982. while (result) {
  983. results.push(result);
  984. result = xpathResult.iterateNext();
  985. }
  986. return results;
  987. }
  988. // If not, fall back to slower JavaScript implementation
  989. // DGF set xpathdebug = true (using getEval, if you like) to turn on JS XPath debugging
  990. //xpathdebug = true;
  991. var context;
  992. if (contextNode == inDocument) {
  993. context = new ExprContext(inDocument);
  994. }
  995. else {
  996. // provide false values to get the default constructor values
  997. context = new ExprContext(contextNode, false, false,
  998. contextNode.parentNode);
  999. }
  1000. context.setCaseInsensitive(true);
  1001. context.setIgnoreAttributesWithoutValue(ignoreAttributesWithoutValue);
  1002. context.setReturnOnFirstMatch(returnOnFirstMatch);
  1003. var xpathObj;
  1004. try {
  1005. xpathObj = xpathParse(xpath);
  1006. }
  1007. catch (e) {
  1008. throw new SeleniumError("Invalid xpath: " + extractExceptionMessage(e));
  1009. }
  1010. var xpathResult = xpathObj.evaluate(context);
  1011. if (xpathResult && xpathResult.value) {
  1012. for (var i = 0; i < xpathResult.value.length; ++i) {
  1013. results.push(xpathResult.value[i]);
  1014. }
  1015. }
  1016. return results;
  1017. }
  1018. /**
  1019. * Returns the full resultset of a CSS selector evaluation.
  1020. */
  1021. function eval_css(locator, inDocument)
  1022. {
  1023. return cssQuery(locator, inDocument);
  1024. }
  1025. /**
  1026. * This function duplicates part of BrowserBot.findElement() to open up locator
  1027. * evaluation on arbitrary documents. It returns a plain old array of located
  1028. * elements found by using a Selenium locator.
  1029. *
  1030. * Multiple results may be generated for xpath and CSS locators. Even though a
  1031. * list could potentially be generated for other locator types, such as link,
  1032. * we don't try for them, because they aren't very expressive location
  1033. * strategies; if you want a list, use xpath or CSS. Furthermore, strategies
  1034. * for these locators have been optimized to only return the first result. For
  1035. * these types of locators, performance is more important than ideal behavior.
  1036. *
  1037. * @param locator a locator string
  1038. * @param inDocument the document in which to apply the locator
  1039. * @param opt_contextNode the context within which to evaluate the locator
  1040. *
  1041. * @return a list of result elements
  1042. */
  1043. function eval_locator(locator, inDocument, opt_contextNode)
  1044. {
  1045. locator = parse_locator(locator);
  1046. var pageBot;
  1047. if (typeof(selenium) != 'undefined' && selenium != undefined) {
  1048. if (typeof(editor) == 'undefined' || editor.state == 'playing') {
  1049. safe_log('info', 'Trying [' + locator.type + ']: '
  1050. + locator.string);
  1051. }
  1052. pageBot = selenium.browserbot;
  1053. }
  1054. else {
  1055. if (!UI_GLOBAL.mozillaBrowserBot) {
  1056. // create a browser bot to evaluate the locator. Hand it the IDE
  1057. // window as a dummy window, and cache it for future use.
  1058. UI_GLOBAL.mozillaBrowserBot = new MozillaBrowserBot(window)
  1059. }
  1060. pageBot = UI_GLOBAL.mozillaBrowserBot;
  1061. }
  1062. var results = [];
  1063. if (locator.type == 'xpath' || (locator.string.charAt(0) == '/' &&
  1064. locator.type == 'implicit')) {
  1065. results = eval_xpath(locator.string, inDocument,
  1066. { contextNode: opt_contextNode });
  1067. }
  1068. else if (locator.type == 'css') {
  1069. results = eval_css(locator.string, inDocument);
  1070. }
  1071. else {
  1072. var element = pageBot
  1073. .findElementBy(locator.type, locator.string, inDocument);
  1074. if (element != null) {
  1075. results.push(element);
  1076. }
  1077. }
  1078. return results;
  1079. }
  1080. //******************************************************************************
  1081. // UI-Element
  1082. /**
  1083. * Escapes the special regular expression characters in a string intended to be
  1084. * used as a regular expression.
  1085. *
  1086. * Based on: http://simonwillison.net/2006/Jan/20/escape/
  1087. */
  1088. RegExp.escape = (function() {
  1089. var specials = [
  1090. '/', '.', '*', '+', '?', '|', '^', '$',
  1091. '(', ')', '[', ']', '{', '}', '\\'
  1092. ];
  1093. var sRE = new RegExp(
  1094. '(\\' + specials.join('|\\') + ')', 'g'
  1095. );
  1096. return function(text) {
  1097. return text.replace(sRE, '\\$1');
  1098. }
  1099. })();
  1100. /**
  1101. * Returns true if two arrays are identical, and false otherwise.
  1102. *
  1103. * @param a1 the first array, may only contain simple values (strings or
  1104. * numbers)
  1105. * @param a2 the second array, same restricts on data as for a1
  1106. * @return true if the arrays are equivalent, false otherwise.
  1107. */
  1108. function are_equal(a1, a2)
  1109. {
  1110. if (typeof(a1) != typeof(a2))
  1111. return false;
  1112. switch(typeof(a1)) {
  1113. case 'object':
  1114. // arrays
  1115. if (a1.length) {
  1116. if (a1.length != a2.length)
  1117. return false;
  1118. for (var i = 0; i < a1.length; ++i) {
  1119. if (!are_equal(a1[i], a2[i]))
  1120. return false
  1121. }
  1122. }
  1123. // associative arrays
  1124. else {
  1125. var keys = {};
  1126. for (var key in a1) {
  1127. keys[key] = true;
  1128. }
  1129. for (var key in a2) {
  1130. keys[key] = true;
  1131. }
  1132. for (var key in keys) {
  1133. if (!are_equal(a1[key], a2[key]))
  1134. return false;
  1135. }
  1136. }
  1137. return true;
  1138. default:
  1139. return a1 == a2;
  1140. }
  1141. }
  1142. /**
  1143. * Create a clone of an object and return it. This is a deep copy of everything
  1144. * but functions, whose references are copied. You shouldn't expect a deep copy
  1145. * of functions anyway.
  1146. *
  1147. * @param orig the original object to copy
  1148. * @return a deep copy of the original object. Any functions attached,
  1149. * however, will have their references copied only.
  1150. */
  1151. function clone(orig) {
  1152. var copy;
  1153. switch(typeof(orig)) {
  1154. case 'object':
  1155. copy = (orig.length) ? [] : {};
  1156. for (var attr in orig) {
  1157. copy[attr] = clone(orig[attr]);
  1158. }
  1159. break;
  1160. default:
  1161. copy = orig;
  1162. break;
  1163. }
  1164. return copy;
  1165. }
  1166. /**
  1167. * Emulates php's print_r() functionality. Returns a nicely formatted string
  1168. * representation of an object. Very useful for debugging.
  1169. *
  1170. * @param object the object to dump
  1171. * @param maxDepth the maximum depth to recurse into the object. Ellipses will
  1172. * be shown for objects whose depth exceeds the maximum.
  1173. * @param indent the string to use for indenting progressively deeper levels
  1174. * of the dump.
  1175. * @return a string representing a dump of the object
  1176. */
  1177. function print_r(object, maxDepth, indent)
  1178. {
  1179. var parentIndent, attr, str = "";
  1180. if (arguments.length == 1) {
  1181. var maxDepth = Number.MAX_VALUE;
  1182. } else {
  1183. maxDepth--;
  1184. }
  1185. if (arguments.length < 3) {
  1186. parentIndent = ''
  1187. var indent = ' ';
  1188. } else {
  1189. parentIndent = indent;
  1190. indent += ' ';
  1191. }
  1192. switch(typeof(object)) {
  1193. case 'object':
  1194. if (object.length != undefined) {
  1195. if (object.length == 0) {
  1196. str += "Array ()\r\n";
  1197. }
  1198. else {
  1199. str += "Array (\r\n";
  1200. for (var i = 0; i < object.length; ++i) {
  1201. str += indent + '[' + i + '] => ';
  1202. if (maxDepth == 0)
  1203. str += "...\r\n";
  1204. else
  1205. str += print_r(object[i], maxDepth, indent);
  1206. }
  1207. str += parentIndent + ")\r\n";
  1208. }
  1209. }
  1210. else {
  1211. str += "Object (\r\n";
  1212. for (attr in object) {
  1213. str += indent + "[" + attr + "] => ";
  1214. if (maxDepth == 0)
  1215. str += "...\r\n";
  1216. else
  1217. str += print_r(object[attr], maxDepth, indent);
  1218. }
  1219. str += parentIndent + ")\r\n";
  1220. }
  1221. break;
  1222. case 'boolean':
  1223. str += (object ? 'true' : 'false') + "\r\n";
  1224. break;
  1225. case 'function':
  1226. str += "Function\r\n";
  1227. break;
  1228. default:
  1229. str += object + "\r\n";
  1230. break;
  1231. }
  1232. return str;
  1233. }
  1234. /**
  1235. * Return an array containing all properties of an object. Perl-style.
  1236. *
  1237. * @param object the object whose keys to return
  1238. * @return array of object keys, as strings
  1239. */
  1240. function keys(object)
  1241. {
  1242. var keys = [];
  1243. for (var k in object) {
  1244. keys.push(k);
  1245. }
  1246. return keys;
  1247. }
  1248. /**
  1249. * Emulates python's range() built-in. Returns an array of integers, counting
  1250. * up (or down) from start to end. Note that the range returned is up to, but
  1251. * NOT INCLUDING, end.
  1252. *.
  1253. * @param start integer from which to start counting. If the end parameter is
  1254. * not provided, this value is considered the end and start will
  1255. * be zero.
  1256. * @param end integer to which to count. If omitted, the function will count
  1257. * up from zero to the value of the start parameter. Note that
  1258. * the array returned will count up to but will not include this
  1259. * value.
  1260. * @return an array of consecutive integers.
  1261. */
  1262. function range(start, end)
  1263. {
  1264. if (arguments.length == 1) {
  1265. var end = start;
  1266. start = 0;
  1267. }
  1268. var r = [];
  1269. if (start < end) {
  1270. while (start != end)
  1271. r.push(start++);
  1272. }
  1273. else {
  1274. while (start != end)
  1275. r.push(start--);
  1276. }
  1277. return r;
  1278. }
  1279. /**
  1280. * Parses a python-style keyword arguments string and returns the pairs in a
  1281. * new object.
  1282. *
  1283. * @param kwargs a string representing a set of keyword arguments. It should
  1284. * look like <tt>keyword1=value1, keyword2=value2, ...</tt>
  1285. * @return an object mapping strings to strings
  1286. */
  1287. function parse_kwargs(kwargs)
  1288. {
  1289. var args = new Object();
  1290. var pairs = kwargs.split(/,/);
  1291. for (var i = 0; i < pairs.length;) {
  1292. if (i > 0 && pairs[i].indexOf('=') == -1) {
  1293. // the value string contained a comma. Glue the parts back together.
  1294. pairs[i-1] += ',' + pairs.splice(i, 1)[0];
  1295. }
  1296. else {
  1297. ++i;
  1298. }
  1299. }
  1300. for (var i = 0; i < pairs.length; ++i) {
  1301. var splits = pairs[i].split(/=/);
  1302. if (splits.length == 1) {
  1303. continue;
  1304. }
  1305. var key = splits.shift();
  1306. var value = splits.join('=');
  1307. args[key.trim()] = value.trim();
  1308. }
  1309. return args;
  1310. }
  1311. /**
  1312. * Creates a python-style keyword arguments string from an object.
  1313. *
  1314. * @param args an associative array mapping strings to strings
  1315. * @param sortedKeys (optional) a list of keys of the args parameter that
  1316. * specifies the order in which the arguments will appear in
  1317. * the returned kwargs string
  1318. *
  1319. * @return a kwarg string representation of args
  1320. */
  1321. function to_kwargs(args, sortedKeys)
  1322. {
  1323. var s = '';
  1324. if (!sortedKeys) {
  1325. var sortedKeys = keys(args).sort();
  1326. }
  1327. for (var i = 0; i < sortedKeys.length; ++i) {
  1328. var k = sortedKeys[i];
  1329. if (args[k] != undefined) {
  1330. if (s) {
  1331. s += ', ';
  1332. }
  1333. s += k + '=' + args[k];
  1334. }
  1335. }
  1336. return s;
  1337. }
  1338. /**
  1339. * Returns true if a node is an ancestor node of a target node, and false
  1340. * otherwise.
  1341. *
  1342. * @param node the node being compared to the target node
  1343. * @param target the target node
  1344. * @return true if node is an ancestor node of target, false otherwise.
  1345. */
  1346. function is_ancestor(node, target)
  1347. {
  1348. while (target.parentNode) {
  1349. target = target.parentNode;
  1350. if (node == target)
  1351. return true;
  1352. }
  1353. return false;
  1354. }
  1355. //******************************************************************************
  1356. // parseUri 1.2.1
  1357. // MIT License
  1358. /*
  1359. Copyright (c) 2007 Steven Levithan <stevenlevithan.com>
  1360. Permission is hereby granted, free of charge, to any person obtaining a copy
  1361. of this software and associated documentation files (the "Software"), to deal
  1362. in the Software without restriction, including without limitation the rights
  1363. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1364. copies of the Software, and to permit persons to whom the Software is
  1365. furnished to do so, subject to the following conditions:
  1366. The above copyright notice and this permission notice shall be included in
  1367. all copies or substantial portions of the Software.
  1368. */
  1369. function parseUri (str) {
  1370. var o = parseUri.options,
  1371. m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  1372. uri = {},
  1373. i = 14;
  1374. while (i--) uri[o.key[i]] = m[i] || "";
  1375. uri[o.q.name] = {};
  1376. uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  1377. if ($1) uri[o.q.name][$1] = $2;
  1378. });
  1379. return uri;
  1380. };
  1381. parseUri.options = {
  1382. strictMode: false,
  1383. key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  1384. q: {
  1385. name: "queryKey",
  1386. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  1387. },
  1388. parser: {
  1389. strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  1390. loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  1391. }
  1392. };