rules-matcher.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. class RulesMatcher {
  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. return {
  50. create(doc) {
  51. return new RulesMatcher(doc);
  52. }
  53. };
  54. function getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex) {
  55. Array.from(cssRules).forEach((cssRule, ruleIndex) => {
  56. if (cssRule.type == CSSRule.MEDIA_RULE) {
  57. const ruleMediaInfo = createMediaInfo(cssRule.media);
  58. mediaInfo.medias.set(cssRule.media, ruleMediaInfo);
  59. getMatchedElementsRules(doc, cssRule.cssRules, ruleMediaInfo, sheetIndex);
  60. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  61. if (cssRule.selectorText) {
  62. let selectors = cssWhat.parse(cssRule.selectorText);
  63. const selectorsText = selectors.map(selector => cssWhat.stringify([selector]));
  64. selectors.forEach(selector => getMatchedElementsSelector(doc, cssRule, selector, selectorsText, mediaInfo, ruleIndex, sheetIndex));
  65. }
  66. }
  67. });
  68. }
  69. function getMatchedElementsSelector(doc, cssRule, selector, selectorsText, mediaInfo, ruleIndex, sheetIndex) {
  70. const selectorText = cssWhat.stringify([selector]);
  71. const matchedElements = doc.querySelectorAll(selectorText);
  72. if (matchedElements.length) {
  73. matchedElements.forEach(element => {
  74. let elementInfo;
  75. if (mediaInfo.elements.has(element)) {
  76. elementInfo = mediaInfo.elements.get(element);
  77. } else {
  78. elementInfo = [];
  79. const elementStyle = element.getAttribute("style");
  80. if (elementStyle) {
  81. elementInfo.push({ cssStyle: element.style });
  82. }
  83. mediaInfo.elements.set(element, elementInfo);
  84. }
  85. const specificity = computeSpecificity(selector);
  86. specificity.ruleIndex = ruleIndex;
  87. specificity.sheetIndex = sheetIndex;
  88. let ruleInfo = elementInfo.find(ruleInfo => ruleInfo.cssRule == cssRule);
  89. if (ruleInfo) {
  90. if (compareSpecificity(ruleInfo.specificity, specificity)) {
  91. ruleInfo.specificity = specificity;
  92. ruleInfo.selectorText = selectorText;
  93. }
  94. } else {
  95. ruleInfo = { cssRule, specificity, selectorText, selectorsText };
  96. elementInfo.push(ruleInfo);
  97. }
  98. });
  99. }
  100. }
  101. function computeCascade(mediaInfo, parentMediaInfos = []) {
  102. mediaInfo.elements.forEach(elementInfo => {
  103. getStylesInfo(elementInfo).forEach((elementStyleInfo, styleName) => {
  104. let ruleInfo, ascendantMedia, allMedia;
  105. if (elementStyleInfo.cssRule) {
  106. ascendantMedia = [mediaInfo, ...parentMediaInfos].find(media => media.rules.get(elementStyleInfo.cssRule)) || mediaInfo;
  107. ruleInfo = ascendantMedia.rules.get(elementStyleInfo.cssRule);
  108. }
  109. if (elementStyleInfo.cssStyle) {
  110. allMedia = parentMediaInfos[parentMediaInfos.length - 1] || mediaInfo;
  111. ruleInfo = allMedia.styles.get(elementStyleInfo.cssStyle);
  112. }
  113. if (!ruleInfo) {
  114. ruleInfo = { style: new Map(), matchedSelectors: new Set(), selectorsText: elementStyleInfo.selectorsText };
  115. if (elementStyleInfo.cssRule) {
  116. ascendantMedia.rules.set(elementStyleInfo.cssRule, ruleInfo);
  117. } else {
  118. allMedia.styles.set(elementStyleInfo.cssStyle, ruleInfo);
  119. }
  120. }
  121. ruleInfo.matchedSelectors.add(elementStyleInfo.selectorText);
  122. const styleValue = ruleInfo.style.get(styleName);
  123. if (!styleValue) {
  124. ruleInfo.style.set(styleName, elementStyleInfo.styleValue);
  125. }
  126. });
  127. });
  128. mediaInfo.medias.forEach(childMediaInfo => computeCascade(childMediaInfo, [mediaInfo, ...parentMediaInfos]));
  129. }
  130. function getStylesInfo(elementInfo) {
  131. const elementStylesInfo = new Map();
  132. elementInfo.forEach(ruleInfo => {
  133. if (ruleInfo.cssStyle) {
  134. const cssStyle = ruleInfo.cssStyle;
  135. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  136. stylesInfo.forEach(styleInfo => {
  137. const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
  138. const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? "!" + PRIORITY_IMPORTANT : "");
  139. elementStylesInfo.set(styleInfo.name, { styleValue, cssStyle: ruleInfo.cssStyle, important });
  140. });
  141. } else {
  142. const cssStyle = ruleInfo.cssRule.style;
  143. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  144. stylesInfo.forEach(styleInfo => {
  145. const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
  146. const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? "!" + PRIORITY_IMPORTANT : "");
  147. let elementStyleInfo = elementStylesInfo.get(styleInfo.name);
  148. if (!elementStyleInfo || (important && !elementStyleInfo.important)) {
  149. elementStylesInfo.set(styleInfo.name, { styleValue, cssRule: ruleInfo.cssRule, selectorText: ruleInfo.selectorText, selectorsText: ruleInfo.selectorsText, important });
  150. }
  151. });
  152. }
  153. });
  154. return elementStylesInfo;
  155. }
  156. function createMediaInfo(media) {
  157. const mediaInfo = { media: media, elements: new Map(), medias: new Map(), rules: new Map() };
  158. if (media == MEDIA_ALL) {
  159. mediaInfo.styles = new Map();
  160. }
  161. return mediaInfo;
  162. }
  163. function sortRules(media) {
  164. media.elements.forEach(elementRules => elementRules.sort((ruleInfo1, ruleInfo2) =>
  165. ruleInfo1.cssStyle && !ruleInfo2.cssStyle ? -1 :
  166. !ruleInfo1.cssStyle && ruleInfo2.cssStyle ? 1 :
  167. compareSpecificity(ruleInfo1.specificity, ruleInfo2.specificity)));
  168. media.medias.forEach(sortRules);
  169. }
  170. function computeSpecificity(selector, specificity = { a: 0, b: 0, c: 0 }) {
  171. selector.forEach(token => {
  172. if (token.expandedSelector && token.type == "attribute" && token.name === "id" && token.action === "equals") {
  173. specificity.a++;
  174. }
  175. if ((!token.expandedSelector && token.type == "attribute") ||
  176. (token.expandedSelector && token.type == "attribute" && token.name === "class" && token.action === "element") ||
  177. (token.type == "pseudo" && token.name != "not")) {
  178. specificity.b++;
  179. }
  180. if ((token.type == "tag" && token.value != "*") || (token.type == "pseudo-element")) {
  181. specificity.c++;
  182. }
  183. if (token.data) {
  184. if (Array.isArray(token.data)) {
  185. token.data.forEach(selector => computeSpecificity(selector, specificity));
  186. }
  187. }
  188. });
  189. return specificity;
  190. }
  191. function compareSpecificity(specificity1, specificity2) {
  192. if (specificity1.a > specificity2.a) {
  193. return -1;
  194. } else if (specificity1.a < specificity2.a) {
  195. return 1;
  196. } else if (specificity1.b > specificity2.b) {
  197. return -1;
  198. } else if (specificity1.b < specificity2.b) {
  199. return 1;
  200. } else if (specificity1.c > specificity2.c) {
  201. return -1;
  202. } else if (specificity1.c < specificity2.c) {
  203. return 1;
  204. } else if (specificity1.sheetIndex > specificity2.sheetIndex) {
  205. return -1;
  206. } else if (specificity1.sheetIndex < specificity2.sheetIndex) {
  207. return 1;
  208. } else if (specificity1.ruleIndex > specificity2.ruleIndex) {
  209. return -1;
  210. } else if (specificity1.ruleIndex < specificity2.ruleIndex) {
  211. return 1;
  212. } else {
  213. return -1;
  214. }
  215. }
  216. })();