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.

93 lines
3.7 KiB

4 years ago
  1. // TODO: it'd be great to merge it with the other canReorder functionality
  2. var rulesOverlap = require('./rules-overlap');
  3. var specificitiesOverlap = require('./specificities-overlap');
  4. var FLEX_PROPERTIES = /align\-items|box\-align|box\-pack|flex|justify/;
  5. var BORDER_PROPERTIES = /^border\-(top|right|bottom|left|color|style|width|radius)/;
  6. function canReorder(left, right, cache) {
  7. for (var i = right.length - 1; i >= 0; i--) {
  8. for (var j = left.length - 1; j >= 0; j--) {
  9. if (!canReorderSingle(left[j], right[i], cache))
  10. return false;
  11. }
  12. }
  13. return true;
  14. }
  15. function canReorderSingle(left, right, cache) {
  16. var leftName = left[0];
  17. var leftValue = left[1];
  18. var leftNameRoot = left[2];
  19. var leftSelector = left[5];
  20. var leftInSpecificSelector = left[6];
  21. var rightName = right[0];
  22. var rightValue = right[1];
  23. var rightNameRoot = right[2];
  24. var rightSelector = right[5];
  25. var rightInSpecificSelector = right[6];
  26. if (leftName == 'font' && rightName == 'line-height' || rightName == 'font' && leftName == 'line-height')
  27. return false;
  28. if (FLEX_PROPERTIES.test(leftName) && FLEX_PROPERTIES.test(rightName))
  29. return false;
  30. if (leftNameRoot == rightNameRoot && unprefixed(leftName) == unprefixed(rightName) && (vendorPrefixed(leftName) ^ vendorPrefixed(rightName)))
  31. return false;
  32. if (leftNameRoot == 'border' && BORDER_PROPERTIES.test(rightNameRoot) && (leftName == 'border' || leftName == rightNameRoot || (leftValue != rightValue && sameBorderComponent(leftName, rightName))))
  33. return false;
  34. if (rightNameRoot == 'border' && BORDER_PROPERTIES.test(leftNameRoot) && (rightName == 'border' || rightName == leftNameRoot || (leftValue != rightValue && sameBorderComponent(leftName, rightName))))
  35. return false;
  36. if (leftNameRoot == 'border' && rightNameRoot == 'border' && leftName != rightName && (isSideBorder(leftName) && isStyleBorder(rightName) || isStyleBorder(leftName) && isSideBorder(rightName)))
  37. return false;
  38. if (leftNameRoot != rightNameRoot)
  39. return true;
  40. if (leftName == rightName && leftNameRoot == rightNameRoot && (leftValue == rightValue || withDifferentVendorPrefix(leftValue, rightValue)))
  41. return true;
  42. if (leftName != rightName && leftNameRoot == rightNameRoot && leftName != leftNameRoot && rightName != rightNameRoot)
  43. return true;
  44. if (leftName != rightName && leftNameRoot == rightNameRoot && leftValue == rightValue)
  45. return true;
  46. if (rightInSpecificSelector && leftInSpecificSelector && !inheritable(leftNameRoot) && !inheritable(rightNameRoot) && !rulesOverlap(rightSelector, leftSelector, false))
  47. return true;
  48. if (!specificitiesOverlap(leftSelector, rightSelector, cache))
  49. return true;
  50. return false;
  51. }
  52. function vendorPrefixed(name) {
  53. return /^\-(?:moz|webkit|ms|o)\-/.test(name);
  54. }
  55. function unprefixed(name) {
  56. return name.replace(/^\-(?:moz|webkit|ms|o)\-/, '');
  57. }
  58. function sameBorderComponent(name1, name2) {
  59. return name1.split('-').pop() == name2.split('-').pop();
  60. }
  61. function isSideBorder(name) {
  62. return name == 'border-top' || name == 'border-right' || name == 'border-bottom' || name == 'border-left';
  63. }
  64. function isStyleBorder(name) {
  65. return name == 'border-color' || name == 'border-style' || name == 'border-width';
  66. }
  67. function withDifferentVendorPrefix(value1, value2) {
  68. return vendorPrefixed(value1) && vendorPrefixed(value2) && value1.split('-')[1] != value2.split('-')[2];
  69. }
  70. function inheritable(name) {
  71. // According to http://www.w3.org/TR/CSS21/propidx.html
  72. // Others will be catched by other, preceeding rules
  73. return name == 'font' || name == 'line-height' || name == 'list-style';
  74. }
  75. module.exports = {
  76. canReorder: canReorder,
  77. canReorderSingle: canReorderSingle
  78. };