extend.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var Node = require("./node"),
  2. Selector = require("./selector");
  3. var Extend = function Extend(selector, option, index, currentFileInfo, visibilityInfo) {
  4. this.selector = selector;
  5. this.option = option;
  6. this.index = index;
  7. this.object_id = Extend.next_id++;
  8. this.parent_ids = [this.object_id];
  9. this.currentFileInfo = currentFileInfo || {};
  10. this.copyVisibilityInfo(visibilityInfo);
  11. this.allowRoot = true;
  12. switch(option) {
  13. case "all":
  14. this.allowBefore = true;
  15. this.allowAfter = true;
  16. break;
  17. default:
  18. this.allowBefore = false;
  19. this.allowAfter = false;
  20. break;
  21. }
  22. };
  23. Extend.next_id = 0;
  24. Extend.prototype = new Node();
  25. Extend.prototype.type = "Extend";
  26. Extend.prototype.accept = function (visitor) {
  27. this.selector = visitor.visit(this.selector);
  28. };
  29. Extend.prototype.eval = function (context) {
  30. return new Extend(this.selector.eval(context), this.option, this.index, this.currentFileInfo, this.visibilityInfo());
  31. };
  32. Extend.prototype.clone = function (context) {
  33. return new Extend(this.selector, this.option, this.index, this.currentFileInfo, this.visibilityInfo());
  34. };
  35. //it concatenates (joins) all selectors in selector array
  36. Extend.prototype.findSelfSelectors = function (selectors) {
  37. var selfElements = [],
  38. i,
  39. selectorElements;
  40. for (i = 0; i < selectors.length; i++) {
  41. selectorElements = selectors[i].elements;
  42. // duplicate the logic in genCSS function inside the selector node.
  43. // future TODO - move both logics into the selector joiner visitor
  44. if (i > 0 && selectorElements.length && selectorElements[0].combinator.value === "") {
  45. selectorElements[0].combinator.value = ' ';
  46. }
  47. selfElements = selfElements.concat(selectors[i].elements);
  48. }
  49. this.selfSelectors = [new Selector(selfElements)];
  50. this.selfSelectors[0].copyVisibilityInfo(this.visibilityInfo());
  51. };
  52. module.exports = Extend;