PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/www/touch/src/dom/Query.js

https://gitlab.com/cruxrebels/laundromat-bot
JavaScript | 181 lines | 66 code | 16 blank | 99 comment | 19 complexity | 0e880a00118f1602cdabd7c07243db58 MD5 | raw file
  1. //@tag dom,core
  2. //@define Ext.DomQuery
  3. //@define Ext.core.DomQuery
  4. //@require Ext.env.Feature
  5. /**
  6. * @class Ext.DomQuery
  7. * @alternateClassName Ext.core.DomQuery
  8. * @extend Ext.dom.Query
  9. * @singleton
  10. *
  11. * Provides functionality to select elements on the page based on a CSS selector. Delegates to
  12. * document.querySelectorAll. More information can be found at
  13. * [http://www.w3.org/TR/css3-selectors/](http://www.w3.org/TR/css3-selectors/)
  14. *
  15. * All selectors, attribute filters and pseudos below can be combined infinitely in any order. For example
  16. * `div.foo:nth-child(odd)[@foo=bar].bar:first` would be a perfectly valid selector.
  17. *
  18. * ## Element Selectors:
  19. *
  20. * * \* any element
  21. * * E an element with the tag E
  22. * * E F All descendant elements of E that have the tag F
  23. * * E > F or E/F all direct children elements of E that have the tag F
  24. * * E + F all elements with the tag F that are immediately preceded by an element with the tag E
  25. * * E ~ F all elements with the tag F that are preceded by a sibling element with the tag E
  26. *
  27. * ## Attribute Selectors:
  28. *
  29. * The use of @ and quotes are optional. For example, div[@foo='bar'] is also a valid attribute selector.
  30. *
  31. * * E[foo] has an attribute "foo"
  32. * * E[foo=bar] has an attribute "foo" that equals "bar"
  33. * * E[foo^=bar] has an attribute "foo" that starts with "bar"
  34. * * E[foo$=bar] has an attribute "foo" that ends with "bar"
  35. * * E[foo*=bar] has an attribute "foo" that contains the substring "bar"
  36. * * E[foo%=2] has an attribute "foo" that is evenly divisible by 2
  37. * * E[foo!=bar] has an attribute "foo" that does not equal "bar"
  38. *
  39. * ## Pseudo Classes:
  40. *
  41. * * E:first-child E is the first child of its parent
  42. * * E:last-child E is the last child of its parent
  43. * * E:nth-child(n) E is the nth child of its parent (1 based as per the spec)
  44. * * E:nth-child(odd) E is an odd child of its parent
  45. * * E:nth-child(even) E is an even child of its parent
  46. * * E:only-child E is the only child of its parent
  47. * * E:checked E is an element that is has a checked attribute that is true (e.g. a radio or checkbox)
  48. * * E:first the first E in the resultset
  49. * * E:last the last E in the resultset
  50. * * E:nth(n) the nth E in the resultset (1 based)
  51. * * E:odd shortcut for :nth-child(odd)
  52. * * E:even shortcut for :nth-child(even)
  53. * * E:not(S) an E element that does not match simple selector S
  54. * * E:has(S) an E element that has a descendant that matches simple selector S
  55. * * E:next(S) an E element whose next sibling matches simple selector S
  56. * * E:prev(S) an E element whose previous sibling matches simple selector S
  57. * * E:any(S1|S2|S2) an E element which matches any of the simple selectors S1, S2 or S3//\\
  58. *
  59. * ## CSS Value Selectors:
  60. *
  61. * * E{display=none} CSS value "display" that equals "none"
  62. * * E{display^=none} CSS value "display" that starts with "none"
  63. * * E{display$=none} CSS value "display" that ends with "none"
  64. * * E{display*=none} CSS value "display" that contains the substring "none"
  65. * * E{display%=2} CSS value "display" that is evenly divisible by 2
  66. * * E{display!=none} CSS value "display" that does not equal "none"
  67. */
  68. /**
  69. * See {@link Ext.DomQuery} which is the singleton instance of this
  70. * class. You most likely don't need to instantiate Ext.dom.Query by
  71. * yourself.
  72. * @private
  73. */
  74. Ext.define('Ext.dom.Query', {
  75. /**
  76. * Selects a group of elements.
  77. * @param {String} selector The selector/xpath query (can be a comma separated list of selectors)
  78. * @param {HTMLElement/String} [root] The start of the query (defaults to document).
  79. * @return {HTMLElement[]} An Array of DOM elements which match the selector. If there are
  80. * no matches, and empty Array is returned.
  81. */
  82. select: function(q, root) {
  83. var results = [],
  84. nodes, i, j, qlen, nlen;
  85. root = root || document;
  86. if (typeof root == 'string') {
  87. root = document.getElementById(root);
  88. }
  89. q = q.split(",");
  90. for (i = 0,qlen = q.length; i < qlen; i++) {
  91. if (typeof q[i] == 'string') {
  92. //support for node attribute selection
  93. if (q[i][0] == '@') {
  94. nodes = root.getAttributeNode(q[i].substring(1));
  95. results.push(nodes);
  96. }
  97. else {
  98. nodes = root.querySelectorAll(q[i]);
  99. for (j = 0,nlen = nodes.length; j < nlen; j++) {
  100. results.push(nodes[j]);
  101. }
  102. }
  103. }
  104. }
  105. return results;
  106. },
  107. /**
  108. * Selects a single element.
  109. * @param {String} selector The selector/xpath query
  110. * @param {HTMLElement/String} [root] The start of the query (defaults to document).
  111. * @return {HTMLElement} The DOM element which matched the selector.
  112. */
  113. selectNode: function(q, root) {
  114. return this.select(q, root)[0];
  115. },
  116. /**
  117. * Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)
  118. * @param {String/HTMLElement/Array} el An element id, element or array of elements
  119. * @param {String} selector The simple selector to test
  120. * @return {Boolean}
  121. */
  122. is: function (el, q) {
  123. var root, is, i, ln;
  124. if (typeof el == "string") {
  125. el = document.getElementById(el);
  126. }
  127. if (Ext.isArray(el)) {
  128. is = true;
  129. ln = el.length;
  130. for (i = 0; i < ln; i++) {
  131. if (!this.is(el[i], q)) {
  132. is = false;
  133. break;
  134. }
  135. }
  136. }
  137. else {
  138. root = el.parentNode;
  139. if (!root) {
  140. root = document.createDocumentFragment();
  141. root.appendChild(el);
  142. is = this.select(q, root).indexOf(el) !== -1;
  143. root.removeChild(el);
  144. root = null;
  145. } else {
  146. is = this.select(q, root).indexOf(el) !== -1;
  147. }
  148. }
  149. return is;
  150. },
  151. isXml: function(el) {
  152. var docEl = (el ? el.ownerDocument || el : 0).documentElement;
  153. return docEl ? docEl.nodeName !== "HTML" : false;
  154. }
  155. }, function() {
  156. Ext.ns('Ext.core');
  157. Ext.core.DomQuery = Ext.DomQuery = new this();
  158. /**
  159. * Shorthand of {@link Ext.dom.Query#select}
  160. * @member Ext
  161. * @method query
  162. * @inheritdoc Ext.dom.Query#select
  163. */
  164. Ext.query = Ext.Function.alias(Ext.DomQuery, 'select');
  165. });