LeafMatchExpression.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var MatchExpression = require('./MatchExpression'),
  3. ElementPath = require('./ElementPath');
  4. var LeafMatchExpression = module.exports = function LeafMatchExpression(type) {
  5. base.call(this, type);
  6. this._elementPath = new ElementPath();
  7. }, klass = LeafMatchExpression, base = MatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
  8. proto._elementPath = undefined;
  9. proto._path = undefined;
  10. /**
  11. *
  12. * Checks whether the document matches against what was searched.
  13. *
  14. * @param doc
  15. * @param details
  16. * @returns {*}
  17. */
  18. proto.matches = function matches(doc, details) {
  19. var self = this,
  20. checker = function(element) {
  21. if (!self.matchesSingleElement(element)) {
  22. return false;
  23. }
  24. return true;
  25. };
  26. return this._elementPath._matches(doc, details, checker);
  27. };
  28. /**
  29. *
  30. * Overridable method for matching against a single element.
  31. *
  32. * @param e
  33. * @returns {boolean}
  34. */
  35. proto.matchesSingleElement = function matchesSingleElement(e) { return false; }; // The child class defines this method.
  36. /**
  37. *
  38. * Return the internal path.
  39. *
  40. * @returns {undefined|*|klass._path}
  41. */
  42. proto.path = function path() {
  43. return this._path;
  44. };
  45. /**
  46. *
  47. * Initialize the ElementPath to the input path.
  48. *
  49. * @param path
  50. * @returns {*}
  51. */
  52. proto.initPath = function initPath(path) {
  53. this._path = path;
  54. if (this._elementPath === undefined) {
  55. this._elementPath = new ElementPath();
  56. }
  57. return this._elementPath.init(this._path);
  58. };