Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  5. const isStandardSyntaxSelector = require('../../utils/isStandardSyntaxSelector');
  6. const keywordSets = require('../../reference/keywordSets');
  7. const optionsMatches = require('../../utils/optionsMatches');
  8. const parseSelector = require('../../utils/parseSelector');
  9. const report = require('../../utils/report');
  10. const resolvedNestedSelector = require('postcss-resolve-nested-selector');
  11. const ruleMessages = require('../../utils/ruleMessages');
  12. const specificity = require('specificity');
  13. const validateOptions = require('../../utils/validateOptions');
  14. const ruleName = 'selector-max-specificity';
  15. const messages = ruleMessages(ruleName, {
  16. expected: (selector, max) => `Expected "${selector}" to have a specificity no more than "${max}"`,
  17. });
  18. // Return an array representation of zero specificity. We need a new array each time so that it can mutated
  19. const zeroSpecificity = () => [0, 0, 0, 0];
  20. // Calculate the sum of given array of specificity arrays
  21. const specificitySum = (specificities) => {
  22. const sum = zeroSpecificity();
  23. specificities.forEach((specificityArray) => {
  24. specificityArray.forEach((value, i) => {
  25. sum[i] += value;
  26. });
  27. });
  28. return sum;
  29. };
  30. function rule(max, options) {
  31. return (root, result) => {
  32. const validOptions = validateOptions(
  33. result,
  34. ruleName,
  35. {
  36. actual: max,
  37. possible: [
  38. // Check that the max specificity is in the form "a,b,c"
  39. (spec) => /^\d+,\d+,\d+$/.test(spec),
  40. ],
  41. },
  42. {
  43. actual: options,
  44. possible: {
  45. ignoreSelectors: [_.isString, _.isRegExp],
  46. },
  47. optional: true,
  48. },
  49. );
  50. if (!validOptions) {
  51. return;
  52. }
  53. // Calculate the specificity of a simple selector (type, attribute, class, ID, or pseudos's own value)
  54. const simpleSpecificity = (selector) => {
  55. if (optionsMatches(options, 'ignoreSelectors', selector)) {
  56. return zeroSpecificity();
  57. }
  58. return specificity.calculate(selector)[0].specificityArray;
  59. };
  60. // Calculate the the specificity of the most specific direct child
  61. const maxChildSpecificity = (node) =>
  62. node.reduce((maxSpec, child) => {
  63. const childSpecificity = nodeSpecificity(child); // eslint-disable-line no-use-before-define
  64. return specificity.compare(childSpecificity, maxSpec) === 1 ? childSpecificity : maxSpec;
  65. }, zeroSpecificity());
  66. // Calculate the specificity of a pseudo selector including own value and children
  67. const pseudoSpecificity = (node) => {
  68. // `node.toString()` includes children which should be processed separately,
  69. // so use `node.value` instead
  70. const ownValue = node.value;
  71. const ownSpecificity =
  72. ownValue === ':not' || ownValue === ':matches'
  73. ? // :not and :matches don't add specificity themselves, but their children do
  74. zeroSpecificity()
  75. : simpleSpecificity(ownValue);
  76. return specificitySum([ownSpecificity, maxChildSpecificity(node)]);
  77. };
  78. const shouldSkipPseudoClassArgument = (node) => {
  79. // postcss-selector-parser includes the arguments to nth-child() functions
  80. // as "tags", so we need to ignore them ourselves.
  81. // The fake-tag's "parent" is actually a selector node, whose parent
  82. // should be the :nth-child pseudo node.
  83. const parentNode = node.parent.parent;
  84. if (parentNode && parentNode.value) {
  85. const parentNodeValue = parentNode.value;
  86. const normalisedParentNode = parentNodeValue.toLowerCase().replace(/:+/, '');
  87. return (
  88. parentNode.type === 'pseudo' &&
  89. (keywordSets.aNPlusBNotationPseudoClasses.has(normalisedParentNode) ||
  90. keywordSets.linguisticPseudoClasses.has(normalisedParentNode))
  91. );
  92. }
  93. return false;
  94. };
  95. // Calculate the specificity of a node parsed by `postcss-selector-parser`
  96. const nodeSpecificity = (node) => {
  97. if (shouldSkipPseudoClassArgument(node)) {
  98. return zeroSpecificity();
  99. }
  100. switch (node.type) {
  101. case 'attribute':
  102. case 'class':
  103. case 'id':
  104. case 'tag':
  105. return simpleSpecificity(node.toString());
  106. case 'pseudo':
  107. return pseudoSpecificity(node);
  108. case 'selector':
  109. // Calculate the sum of all the direct children
  110. return specificitySum(node.map(nodeSpecificity));
  111. default:
  112. return zeroSpecificity();
  113. }
  114. };
  115. const maxSpecificityArray = `0,${max}`.split(',').map(parseFloat);
  116. root.walkRules((ruleNode) => {
  117. if (!isStandardSyntaxRule(ruleNode)) {
  118. return;
  119. }
  120. // Using `.selectors` gets us each selector in the eventuality we have a comma separated set
  121. ruleNode.selectors.forEach((selector) => {
  122. resolvedNestedSelector(selector, ruleNode).forEach((resolvedSelector) => {
  123. try {
  124. // Skip non-standard syntax selectors
  125. if (!isStandardSyntaxSelector(resolvedSelector)) {
  126. return;
  127. }
  128. parseSelector(resolvedSelector, result, ruleNode, (selectorTree) => {
  129. // Check if the selector specificity exceeds the allowed maximum
  130. if (
  131. specificity.compare(maxChildSpecificity(selectorTree), maxSpecificityArray) === 1
  132. ) {
  133. report({
  134. ruleName,
  135. result,
  136. node: ruleNode,
  137. message: messages.expected(resolvedSelector, max),
  138. word: selector,
  139. });
  140. }
  141. });
  142. } catch {
  143. result.warn('Cannot parse selector', {
  144. node: ruleNode,
  145. stylelintType: 'parseError',
  146. });
  147. }
  148. });
  149. });
  150. });
  151. };
  152. }
  153. rule.ruleName = ruleName;
  154. rule.messages = messages;
  155. module.exports = rule;