rules-matcher.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global CSSRule, cssWhat, parseCss */
  21. this.RulesMatcher = this.RulesMatcher || (() => {
  22. const MEDIA_ALL = "all";
  23. const PRIORITY_IMPORTANT = "important";
  24. return class {
  25. constructor(doc) {
  26. this.doc = doc;
  27. this.mediaAllInfo = createMediaInfo(MEDIA_ALL);
  28. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  29. if (styleElement.sheet) {
  30. if (styleElement.media && styleElement.media != MEDIA_ALL) {
  31. const mediaInfo = createMediaInfo(styleElement.media);
  32. this.mediaAllInfo.medias.set(styleElement.media, mediaInfo);
  33. getMatchedElementsRules(doc, styleElement.sheet.cssRules, mediaInfo, styleIndex);
  34. } else {
  35. getMatchedElementsRules(doc, styleElement.sheet.cssRules, this.mediaAllInfo, styleIndex);
  36. }
  37. }
  38. });
  39. sortRules(this.mediaAllInfo);
  40. computeCascade(this.mediaAllInfo);
  41. }
  42. getAllMatchedRules() {
  43. return this.mediaAllInfo;
  44. }
  45. getMatchedRules(element) {
  46. this.mediaAllInfo.elements.get(element);
  47. }
  48. };
  49. function getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex) {
  50. Array.from(cssRules).forEach((cssRule, ruleIndex) => {
  51. if (cssRule.type == CSSRule.MEDIA_RULE) {
  52. const ruleMediaInfo = createMediaInfo(cssRule.media);
  53. mediaInfo.medias.set(cssRule.media, ruleMediaInfo);
  54. getMatchedElementsRules(doc, cssRule.cssRules, ruleMediaInfo, sheetIndex);
  55. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  56. if (cssRule.selectorText) {
  57. let selectors = cssWhat.parse(cssRule.selectorText);
  58. const selectorsText = selectors.map(selector => cssWhat.stringify([selector]));
  59. selectors.forEach(selector => getMatchedElementsSelector(doc, cssRule, selector, selectorsText, mediaInfo, ruleIndex, sheetIndex));
  60. }
  61. }
  62. });
  63. }
  64. function getMatchedElementsSelector(doc, cssRule, selector, selectorsText, mediaInfo, ruleIndex, sheetIndex) {
  65. const selectorText = cssWhat.stringify([selector]);
  66. const matchedElements = doc.querySelectorAll(selectorText);
  67. if (matchedElements.length) {
  68. matchedElements.forEach(element => {
  69. let elementInfo;
  70. if (mediaInfo.elements.has(element)) {
  71. elementInfo = mediaInfo.elements.get(element);
  72. } else {
  73. elementInfo = [];
  74. const elementStyle = element.getAttribute("style");
  75. if (elementStyle) {
  76. elementInfo.push({ cssStyle: element.style });
  77. }
  78. mediaInfo.elements.set(element, elementInfo);
  79. }
  80. const specificity = computeSpecificity(selector);
  81. specificity.ruleIndex = ruleIndex;
  82. specificity.sheetIndex = sheetIndex;
  83. let ruleInfo = elementInfo.find(ruleInfo => ruleInfo.cssRule == cssRule);
  84. if (ruleInfo) {
  85. if (compareSpecificity(ruleInfo.specificity, specificity)) {
  86. ruleInfo.specificity = specificity;
  87. ruleInfo.selectorText = selectorText;
  88. }
  89. } else {
  90. ruleInfo = { cssRule, specificity, selectorText, selectorsText };
  91. elementInfo.push(ruleInfo);
  92. }
  93. });
  94. }
  95. }
  96. function computeCascade(mediaInfo, parentMediaInfos = []) {
  97. mediaInfo.elements.forEach((elementInfo) => {
  98. getStylesInfo(elementInfo).forEach((elementStyleInfo, styleName) => {
  99. let ruleInfo, ascendantMedia, allMedia;
  100. if (elementStyleInfo.cssRule) {
  101. ascendantMedia = [mediaInfo, ...parentMediaInfos].find(media => media.rules.get(elementStyleInfo.cssRule)) || mediaInfo;
  102. ruleInfo = ascendantMedia.rules.get(elementStyleInfo.cssRule);
  103. }
  104. if (elementStyleInfo.cssStyle) {
  105. allMedia = parentMediaInfos[parentMediaInfos.length - 1] || mediaInfo;
  106. ruleInfo = allMedia.styles.get(elementStyleInfo.cssStyle);
  107. }
  108. if (!ruleInfo) {
  109. ruleInfo = { style: new Map(), matchedSelectors: new Set(), selectorsText: elementStyleInfo.selectorsText };
  110. if (elementStyleInfo.cssRule) {
  111. ascendantMedia.rules.set(elementStyleInfo.cssRule, ruleInfo);
  112. } else {
  113. allMedia.styles.set(elementStyleInfo.cssStyle, ruleInfo);
  114. }
  115. }
  116. ruleInfo.matchedSelectors.add(elementStyleInfo.selectorText);
  117. const styleValue = ruleInfo.style.get(styleName);
  118. if (!styleValue) {
  119. ruleInfo.style.set(styleName, elementStyleInfo.styleValue);
  120. }
  121. });
  122. });
  123. mediaInfo.medias.forEach(childMediaInfo => computeCascade(childMediaInfo, [mediaInfo, ...parentMediaInfos]));
  124. }
  125. function getStylesInfo(elementInfo) {
  126. const elementStylesInfo = new Map();
  127. elementInfo.forEach(ruleInfo => {
  128. if (ruleInfo.cssStyle) {
  129. const cssStyle = ruleInfo.cssStyle;
  130. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  131. stylesInfo.forEach(styleInfo => {
  132. const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
  133. const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? " !" + PRIORITY_IMPORTANT : "");
  134. elementStylesInfo.set(styleInfo.name, { styleValue, cssStyle: ruleInfo.cssStyle });
  135. });
  136. } else {
  137. const cssStyle = ruleInfo.cssRule.style;
  138. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  139. stylesInfo.forEach(styleInfo => {
  140. const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
  141. const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? " !" + PRIORITY_IMPORTANT : "");
  142. let elementStyleInfo = elementStylesInfo.get(styleInfo.name);
  143. if (!elementStyleInfo || (important && !elementStyleInfo.important)) {
  144. elementStylesInfo.set(styleInfo.name, { styleValue, cssRule: ruleInfo.cssRule, selectorText: ruleInfo.selectorText, selectorsText: ruleInfo.selectorsText });
  145. }
  146. });
  147. }
  148. });
  149. return elementStylesInfo;
  150. }
  151. function createMediaInfo(media) {
  152. const mediaInfo = { media: media, elements: new Map(), medias: new Map(), rules: new Map() };
  153. if (media == MEDIA_ALL) {
  154. mediaInfo.styles = new Map();
  155. }
  156. return mediaInfo;
  157. }
  158. function sortRules(media) {
  159. media.elements.forEach(elementRules => elementRules.sort((ruleInfo1, ruleInfo2) =>
  160. ruleInfo1.cssStyle && !ruleInfo2.cssStyle ? -1 :
  161. !ruleInfo1.cssStyle && ruleInfo2.cssStyle ? 1 :
  162. compareSpecificity(ruleInfo1.specificity, ruleInfo2.specificity)));
  163. media.medias.forEach(sortRules);
  164. }
  165. function computeSpecificity(selector, specificity = { a: 0, b: 0, c: 0 }) {
  166. selector.forEach(token => {
  167. if (token.expandedSelector && token.type == "attribute" && token.name === "id" && token.action === "equals") {
  168. specificity.a++;
  169. }
  170. if ((!token.expandedSelector && token.type == "attribute") ||
  171. (token.expandedSelector && token.type == "attribute" && token.name === "class" && token.action === "element") ||
  172. (token.type == "pseudo" && token.name != "not")) {
  173. specificity.b++;
  174. }
  175. if ((token.type == "tag" && token.value != "*") || (token.type == "pseudo-element")) {
  176. specificity.c++;
  177. }
  178. if (token.data) {
  179. if (Array.isArray(token.data)) {
  180. token.data.forEach(selector => computeSpecificity(selector, specificity));
  181. }
  182. }
  183. });
  184. return specificity;
  185. }
  186. function compareSpecificity(specificity1, specificity2) {
  187. if (specificity1.a > specificity2.a) {
  188. return -1;
  189. } else if (specificity1.a < specificity2.a) {
  190. return 1;
  191. } else if (specificity1.b > specificity2.b) {
  192. return -1;
  193. } else if (specificity1.b < specificity2.b) {
  194. return 1;
  195. } else if (specificity1.c > specificity2.c) {
  196. return -1;
  197. } else if (specificity1.c < specificity2.c) {
  198. return 1;
  199. } else if (specificity1.sheetIndex > specificity2.sheetIndex) {
  200. return -1;
  201. } else if (specificity1.sheetIndex < specificity2.sheetIndex) {
  202. return 1;
  203. } else if (specificity1.ruleIndex > specificity2.ruleIndex) {
  204. return -1;
  205. } else if (specificity1.ruleIndex < specificity2.ruleIndex) {
  206. return 1;
  207. } else {
  208. return -1;
  209. }
  210. }
  211. })();