rules-matcher.js 8.5 KB

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