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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // @ts-nocheck
  2. 'use strict';
  3. const valueParser = require('postcss-value-parser');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const getDeclarationValue = require('../../utils/getDeclarationValue');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const setDeclarationValue = require('../../utils/setDeclarationValue');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const ruleName = 'color-hex-length';
  11. const messages = ruleMessages(ruleName, {
  12. expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
  13. });
  14. const HEX = /^#[0-9A-Za-z]+/;
  15. const IGNORED_FUNCTIONS = new Set(['url']);
  16. function rule(expectation, _, context) {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, {
  19. actual: expectation,
  20. possible: ['short', 'long'],
  21. });
  22. if (!validOptions) {
  23. return;
  24. }
  25. root.walkDecls((decl) => {
  26. const parsedValue = valueParser(getDeclarationValue(decl));
  27. let needsFix = false;
  28. parsedValue.walk((node) => {
  29. const { value: hexValue } = node;
  30. if (isIgnoredFunction(node)) return false;
  31. if (!isHexColor(node)) return;
  32. if (expectation === 'long' && hexValue.length !== 4 && hexValue.length !== 5) {
  33. return;
  34. }
  35. if (expectation === 'short' && (hexValue.length < 6 || !canShrink(hexValue))) {
  36. return;
  37. }
  38. const variant = expectation === 'long' ? longer : shorter;
  39. const expectedHex = variant(hexValue);
  40. if (context.fix) {
  41. node.value = expectedHex;
  42. needsFix = true;
  43. return;
  44. }
  45. report({
  46. message: messages.expected(hexValue, expectedHex),
  47. node: decl,
  48. index: declarationValueIndex(decl) + node.sourceIndex,
  49. result,
  50. ruleName,
  51. });
  52. });
  53. if (needsFix) {
  54. setDeclarationValue(decl, parsedValue.toString());
  55. }
  56. });
  57. };
  58. }
  59. function canShrink(hex) {
  60. hex = hex.toLowerCase();
  61. return (
  62. hex[1] === hex[2] &&
  63. hex[3] === hex[4] &&
  64. hex[5] === hex[6] &&
  65. (hex.length === 7 || (hex.length === 9 && hex[7] === hex[8]))
  66. );
  67. }
  68. function shorter(hex) {
  69. let hexVariant = '#';
  70. for (let i = 1; i < hex.length; i += 2) {
  71. hexVariant += hex[i];
  72. }
  73. return hexVariant;
  74. }
  75. function longer(hex) {
  76. let hexVariant = '#';
  77. for (let i = 1; i < hex.length; i++) {
  78. hexVariant += hex[i] + hex[i];
  79. }
  80. return hexVariant;
  81. }
  82. function isIgnoredFunction({ type, value }) {
  83. return type === 'function' && IGNORED_FUNCTIONS.has(value.toLowerCase());
  84. }
  85. function isHexColor({ type, value }) {
  86. return type === 'word' && HEX.test(value);
  87. }
  88. rule.ruleName = ruleName;
  89. rule.messages = messages;
  90. module.exports = rule;