仲裁视频会议H5

main.mjs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { shorthandData } from './shorthand-data.mjs';
  2. import { bubbleSort } from './bubble-sort.mjs';
  3. const builtInOrders = [
  4. 'alphabetical',
  5. 'concentric-css',
  6. 'smacss',
  7. ];
  8. export const cssDeclarationSorter = ({ order = 'alphabetical', keepOverrides = false } = {}) => ({
  9. postcssPlugin: 'css-declaration-sorter',
  10. OnceExit (css) {
  11. let withKeepOverrides = comparator => comparator;
  12. if (keepOverrides) {
  13. withKeepOverrides = withOverridesComparator(shorthandData);
  14. }
  15. if (typeof order === 'function') {
  16. return processCss({ css, comparator: withKeepOverrides(order) });
  17. }
  18. if (!builtInOrders.includes(order))
  19. return Promise.reject(
  20. Error([
  21. `Invalid built-in order '${order}' provided.`,
  22. `Available built-in orders are: ${builtInOrders}`,
  23. ].join('\n'))
  24. );
  25. return import(`../orders/${order}.mjs`)
  26. .then(({ properties }) => processCss({
  27. css,
  28. comparator: withKeepOverrides(orderComparator(properties)),
  29. }));
  30. },
  31. });
  32. cssDeclarationSorter.postcss = true;
  33. // Kept for backward compatibility
  34. export default cssDeclarationSorter;
  35. function processCss ({ css, comparator }) {
  36. const comments = [];
  37. const rulesCache = [];
  38. css.walk(node => {
  39. const nodes = node.nodes;
  40. const type = node.type;
  41. if (type === 'comment') {
  42. // Don't do anything to root comments or the last newline comment
  43. const isNewlineNode = node.raws.before && node.raws.before.includes('\n');
  44. const lastNewlineNode = isNewlineNode && !node.next();
  45. const onlyNode = !node.prev() && !node.next() || !node.parent;
  46. if (lastNewlineNode || onlyNode || node.parent.type === 'root') {
  47. return;
  48. }
  49. if (isNewlineNode) {
  50. const pairedNode = node.next() || node.prev();
  51. if (pairedNode) {
  52. comments.unshift({
  53. 'comment': node,
  54. 'pairedNode': pairedNode,
  55. 'insertPosition': node.next() ? 'Before' : 'After',
  56. });
  57. node.remove();
  58. }
  59. } else {
  60. const pairedNode = node.prev() || node.next();
  61. if (pairedNode) {
  62. comments.push({
  63. 'comment': node,
  64. 'pairedNode': pairedNode,
  65. 'insertPosition': 'After',
  66. });
  67. node.remove();
  68. }
  69. }
  70. return;
  71. }
  72. // Add rule-like nodes to a cache so that we can remove all
  73. // comment nodes before we start sorting.
  74. const isRule = type === 'rule' || type === 'atrule';
  75. if (isRule && nodes && nodes.length > 1) {
  76. rulesCache.push(nodes);
  77. }
  78. });
  79. // Perform a sort once all comment nodes are removed
  80. rulesCache.forEach(nodes => {
  81. sortCssDeclarations({ nodes, comparator });
  82. });
  83. // Add comments back to the nodes they are paired with
  84. comments.forEach(node => {
  85. const pairedNode = node.pairedNode;
  86. node.comment.remove();
  87. pairedNode.parent && pairedNode.parent['insert' + node.insertPosition](pairedNode, node.comment);
  88. });
  89. }
  90. function sortCssDeclarations ({ nodes, comparator }) {
  91. bubbleSort(nodes, (a, b) => {
  92. if (a.type === 'decl' && b.type === 'decl') {
  93. return comparator(a.prop, b.prop);
  94. } else {
  95. return compareDifferentType(a, b);
  96. }
  97. });
  98. }
  99. function withOverridesComparator (shorthandData) {
  100. return function (comparator) {
  101. return function (a, b) {
  102. a = removeVendorPrefix(a);
  103. b = removeVendorPrefix(b);
  104. if (shorthandData[a] && shorthandData[a].includes(b)) return 0;
  105. if (shorthandData[b] && shorthandData[b].includes(a)) return 0;
  106. return comparator(a, b);
  107. };
  108. };
  109. }
  110. function orderComparator (order) {
  111. return function (a, b) {
  112. return order.indexOf(a) - order.indexOf(b);
  113. };
  114. }
  115. function compareDifferentType (a, b) {
  116. if (b.type === 'atrule' || a.type === 'atrule') {
  117. return 0;
  118. }
  119. return a.type === 'decl' ? -1 : b.type === 'decl' ? 1 : 0;
  120. }
  121. function removeVendorPrefix (property) {
  122. return property.replace(/^-\w+-/, '');
  123. }