CSSPageRule.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
  4. CSSRule: require("./CSSRule").CSSRule,
  5. CSSRuleList: require("./CSSRuleList").CSSRuleList,
  6. CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
  7. };
  8. var regexPatterns = require("./regexPatterns").regexPatterns;
  9. // Use cssstyle if available
  10. try {
  11. CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
  12. } catch (e) {
  13. // ignore
  14. }
  15. ///CommonJS
  16. /**
  17. * @constructor
  18. * @see https://drafts.csswg.org/cssom/#the-csspagerule-interface
  19. */
  20. CSSOM.CSSPageRule = function CSSPageRule() {
  21. CSSOM.CSSGroupingRule.call(this);
  22. this.__style = new CSSOM.CSSStyleDeclaration();
  23. this.__style.parentRule = this;
  24. };
  25. CSSOM.CSSPageRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
  26. CSSOM.CSSPageRule.prototype.constructor = CSSOM.CSSPageRule;
  27. Object.setPrototypeOf(CSSOM.CSSPageRule, CSSOM.CSSGroupingRule);
  28. Object.defineProperty(CSSOM.CSSPageRule.prototype, "type", {
  29. value: 6,
  30. writable: false
  31. });
  32. Object.defineProperty(CSSOM.CSSPageRule.prototype, "selectorText", {
  33. get: function() {
  34. return this.__selectorText;
  35. },
  36. set: function(value) {
  37. if (typeof value === "string") {
  38. var trimmedValue = value.trim();
  39. // Empty selector is valid for @page
  40. if (trimmedValue === '') {
  41. this.__selectorText = '';
  42. return;
  43. }
  44. var atPageRuleSelectorRegExp = regexPatterns.atPageRuleSelectorRegExp;
  45. var cssCustomIdentifierRegExp = regexPatterns.cssCustomIdentifierRegExp;
  46. var match = trimmedValue.match(atPageRuleSelectorRegExp);
  47. if (match) {
  48. var pageName = match[1] || '';
  49. var pseudoPages = match[2] || '';
  50. // Validate page name if present
  51. if (pageName) {
  52. // Page name can be an identifier or a string
  53. if (!cssCustomIdentifierRegExp.test(pageName)) {
  54. return;
  55. }
  56. }
  57. // Validate pseudo-pages if present
  58. if (pseudoPages) {
  59. var pseudos = pseudoPages.split(':').filter(function(p) { return p; });
  60. var validPseudos = ['left', 'right', 'first', 'blank'];
  61. var allValid = true;
  62. for (var j = 0; j < pseudos.length; j++) {
  63. if (validPseudos.indexOf(pseudos[j].toLowerCase()) === -1) {
  64. allValid = false;
  65. break;
  66. }
  67. }
  68. if (!allValid) {
  69. return; // Invalid pseudo-page, do nothing
  70. }
  71. }
  72. this.__selectorText = pageName + pseudoPages.toLowerCase();
  73. }
  74. }
  75. }
  76. });
  77. Object.defineProperty(CSSOM.CSSPageRule.prototype, "style", {
  78. get: function() {
  79. return this.__style;
  80. },
  81. set: function(value) {
  82. if (typeof value === "string") {
  83. this.__style.cssText = value;
  84. } else {
  85. this.__style = value;
  86. }
  87. }
  88. });
  89. Object.defineProperty(CSSOM.CSSPageRule.prototype, "cssText", {
  90. get: function() {
  91. var values = "";
  92. if (this.cssRules.length) {
  93. var valuesArr = [" {"];
  94. this.style.cssText && valuesArr.push(this.style.cssText);
  95. valuesArr.push(this.cssRules.reduce(function(acc, rule){
  96. if (rule.cssText !== "") {
  97. acc.push(rule.cssText);
  98. }
  99. return acc;
  100. }, []).join("\n "));
  101. values = valuesArr.join("\n ") + "\n}";
  102. } else {
  103. values = " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
  104. }
  105. return "@page" + (this.selectorText ? " " + this.selectorText : "") + values;
  106. }
  107. });
  108. //.CommonJS
  109. exports.CSSPageRule = CSSOM.CSSPageRule;
  110. ///CommonJS