No Description

elementpath.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * Copyright 2011 Rackspace
  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. var sprintf = require('./sprintf').sprintf;
  18. var utils = require('./utils');
  19. var SyntaxError = require('./errors').SyntaxError;
  20. var _cache = {};
  21. var RE = new RegExp(
  22. "(" +
  23. "'[^']*'|\"[^\"]*\"|" +
  24. "::|" +
  25. "//?|" +
  26. "\\.\\.|" +
  27. "\\(\\)|" +
  28. "[/.*:\\[\\]\\(\\)@=])|" +
  29. "((?:\\{[^}]+\\})?[^/\\[\\]\\(\\)@=\\s]+)|" +
  30. "\\s+", 'g'
  31. );
  32. var xpath_tokenizer = utils.findall.bind(null, RE);
  33. function prepare_tag(next, token) {
  34. var tag = token[0];
  35. function select(context, result) {
  36. var i, len, elem, rv = [];
  37. for (i = 0, len = result.length; i < len; i++) {
  38. elem = result[i];
  39. elem._children.forEach(function(e) {
  40. if (e.tag === tag) {
  41. rv.push(e);
  42. }
  43. });
  44. }
  45. return rv;
  46. }
  47. return select;
  48. }
  49. function prepare_star(next, token) {
  50. function select(context, result) {
  51. var i, len, elem, rv = [];
  52. for (i = 0, len = result.length; i < len; i++) {
  53. elem = result[i];
  54. elem._children.forEach(function(e) {
  55. rv.push(e);
  56. });
  57. }
  58. return rv;
  59. }
  60. return select;
  61. }
  62. function prepare_dot(next, token) {
  63. function select(context, result) {
  64. var i, len, elem, rv = [];
  65. for (i = 0, len = result.length; i < len; i++) {
  66. elem = result[i];
  67. rv.push(elem);
  68. }
  69. return rv;
  70. }
  71. return select;
  72. }
  73. function prepare_iter(next, token) {
  74. var tag;
  75. token = next();
  76. if (token[1] === '*') {
  77. tag = '*';
  78. }
  79. else if (!token[1]) {
  80. tag = token[0] || '';
  81. }
  82. else {
  83. throw new SyntaxError(token);
  84. }
  85. function select(context, result) {
  86. var i, len, elem, rv = [];
  87. for (i = 0, len = result.length; i < len; i++) {
  88. elem = result[i];
  89. elem.iter(tag, function(e) {
  90. if (e !== elem) {
  91. rv.push(e);
  92. }
  93. });
  94. }
  95. return rv;
  96. }
  97. return select;
  98. }
  99. function prepare_dot_dot(next, token) {
  100. function select(context, result) {
  101. var i, len, elem, rv = [], parent_map = context.parent_map;
  102. if (!parent_map) {
  103. context.parent_map = parent_map = {};
  104. context.root.iter(null, function(p) {
  105. p._children.forEach(function(e) {
  106. parent_map[e] = p;
  107. });
  108. });
  109. }
  110. for (i = 0, len = result.length; i < len; i++) {
  111. elem = result[i];
  112. if (parent_map.hasOwnProperty(elem)) {
  113. rv.push(parent_map[elem]);
  114. }
  115. }
  116. return rv;
  117. }
  118. return select;
  119. }
  120. function prepare_predicate(next, token) {
  121. var tag, key, value, select;
  122. token = next();
  123. if (token[1] === '@') {
  124. // attribute
  125. token = next();
  126. if (token[1]) {
  127. throw new SyntaxError(token, 'Invalid attribute predicate');
  128. }
  129. key = token[0];
  130. token = next();
  131. if (token[1] === ']') {
  132. select = function(context, result) {
  133. var i, len, elem, rv = [];
  134. for (i = 0, len = result.length; i < len; i++) {
  135. elem = result[i];
  136. if (elem.get(key)) {
  137. rv.push(elem);
  138. }
  139. }
  140. return rv;
  141. };
  142. }
  143. else if (token[1] === '=') {
  144. value = next()[1];
  145. if (value[0] === '"' || value[value.length - 1] === '\'') {
  146. value = value.slice(1, value.length - 1);
  147. }
  148. else {
  149. throw new SyntaxError(token, 'Ivalid comparison target');
  150. }
  151. token = next();
  152. select = function(context, result) {
  153. var i, len, elem, rv = [];
  154. for (i = 0, len = result.length; i < len; i++) {
  155. elem = result[i];
  156. if (elem.get(key) === value) {
  157. rv.push(elem);
  158. }
  159. }
  160. return rv;
  161. };
  162. }
  163. if (token[1] !== ']') {
  164. throw new SyntaxError(token, 'Invalid attribute predicate');
  165. }
  166. }
  167. else if (!token[1]) {
  168. tag = token[0] || '';
  169. token = next();
  170. if (token[1] !== ']') {
  171. throw new SyntaxError(token, 'Invalid node predicate');
  172. }
  173. select = function(context, result) {
  174. var i, len, elem, rv = [];
  175. for (i = 0, len = result.length; i < len; i++) {
  176. elem = result[i];
  177. if (elem.find(tag)) {
  178. rv.push(elem);
  179. }
  180. }
  181. return rv;
  182. };
  183. }
  184. else {
  185. throw new SyntaxError(null, 'Invalid predicate');
  186. }
  187. return select;
  188. }
  189. var ops = {
  190. "": prepare_tag,
  191. "*": prepare_star,
  192. ".": prepare_dot,
  193. "..": prepare_dot_dot,
  194. "//": prepare_iter,
  195. "[": prepare_predicate,
  196. };
  197. function _SelectorContext(root) {
  198. this.parent_map = null;
  199. this.root = root;
  200. }
  201. function findall(elem, path) {
  202. var selector, result, i, len, token, value, select, context;
  203. if (_cache.hasOwnProperty(path)) {
  204. selector = _cache[path];
  205. }
  206. else {
  207. // TODO: Use smarter cache purging approach
  208. if (Object.keys(_cache).length > 100) {
  209. _cache = {};
  210. }
  211. if (path.charAt(0) === '/') {
  212. throw new SyntaxError(null, 'Cannot use absolute path on element');
  213. }
  214. result = xpath_tokenizer(path);
  215. selector = [];
  216. function getToken() {
  217. return result.shift();
  218. }
  219. token = getToken();
  220. while (true) {
  221. var c = token[1] || '';
  222. value = ops[c](getToken, token);
  223. if (!value) {
  224. throw new SyntaxError(null, sprintf('Invalid path: %s', path));
  225. }
  226. selector.push(value);
  227. token = getToken();
  228. if (!token) {
  229. break;
  230. }
  231. else if (token[1] === '/') {
  232. token = getToken();
  233. }
  234. if (!token) {
  235. break;
  236. }
  237. }
  238. _cache[path] = selector;
  239. }
  240. // Execute slector pattern
  241. result = [elem];
  242. context = new _SelectorContext(elem);
  243. for (i = 0, len = selector.length; i < len; i++) {
  244. select = selector[i];
  245. result = select(context, result);
  246. }
  247. return result || [];
  248. }
  249. function find(element, path) {
  250. var resultElements = findall(element, path);
  251. if (resultElements && resultElements.length > 0) {
  252. return resultElements[0];
  253. }
  254. return null;
  255. }
  256. function findtext(element, path, defvalue) {
  257. var resultElements = findall(element, path);
  258. if (resultElements && resultElements.length > 0) {
  259. return resultElements[0].text;
  260. }
  261. return defvalue;
  262. }
  263. exports.find = find;
  264. exports.findall = findall;
  265. exports.findtext = findtext;