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.

300 lines
11 KiB

4 years ago
  1. var resolveProperty = require('css-tree').property;
  2. var resolveKeyword = require('css-tree').keyword;
  3. var walk = require('css-tree').walk;
  4. var generate = require('css-tree').generate;
  5. var fingerprintId = 1;
  6. var dontRestructure = {
  7. 'src': 1 // https://github.com/afelix/csso/issues/50
  8. };
  9. var DONT_MIX_VALUE = {
  10. // https://developer.mozilla.org/en-US/docs/Web/CSS/display#Browser_compatibility
  11. 'display': /table|ruby|flex|-(flex)?box$|grid|contents|run-in/i,
  12. // https://developer.mozilla.org/en/docs/Web/CSS/text-align
  13. 'text-align': /^(start|end|match-parent|justify-all)$/i
  14. };
  15. var CURSOR_SAFE_VALUE = [
  16. 'auto', 'crosshair', 'default', 'move', 'text', 'wait', 'help',
  17. 'n-resize', 'e-resize', 's-resize', 'w-resize',
  18. 'ne-resize', 'nw-resize', 'se-resize', 'sw-resize',
  19. 'pointer', 'progress', 'not-allowed', 'no-drop', 'vertical-text', 'all-scroll',
  20. 'col-resize', 'row-resize'
  21. ];
  22. var POSITION_SAFE_VALUE = [
  23. 'static', 'relative', 'absolute', 'fixed'
  24. ];
  25. var NEEDLESS_TABLE = {
  26. 'border-width': ['border'],
  27. 'border-style': ['border'],
  28. 'border-color': ['border'],
  29. 'border-top': ['border'],
  30. 'border-right': ['border'],
  31. 'border-bottom': ['border'],
  32. 'border-left': ['border'],
  33. 'border-top-width': ['border-top', 'border-width', 'border'],
  34. 'border-right-width': ['border-right', 'border-width', 'border'],
  35. 'border-bottom-width': ['border-bottom', 'border-width', 'border'],
  36. 'border-left-width': ['border-left', 'border-width', 'border'],
  37. 'border-top-style': ['border-top', 'border-style', 'border'],
  38. 'border-right-style': ['border-right', 'border-style', 'border'],
  39. 'border-bottom-style': ['border-bottom', 'border-style', 'border'],
  40. 'border-left-style': ['border-left', 'border-style', 'border'],
  41. 'border-top-color': ['border-top', 'border-color', 'border'],
  42. 'border-right-color': ['border-right', 'border-color', 'border'],
  43. 'border-bottom-color': ['border-bottom', 'border-color', 'border'],
  44. 'border-left-color': ['border-left', 'border-color', 'border'],
  45. 'margin-top': ['margin'],
  46. 'margin-right': ['margin'],
  47. 'margin-bottom': ['margin'],
  48. 'margin-left': ['margin'],
  49. 'padding-top': ['padding'],
  50. 'padding-right': ['padding'],
  51. 'padding-bottom': ['padding'],
  52. 'padding-left': ['padding'],
  53. 'font-style': ['font'],
  54. 'font-variant': ['font'],
  55. 'font-weight': ['font'],
  56. 'font-size': ['font'],
  57. 'font-family': ['font'],
  58. 'list-style-type': ['list-style'],
  59. 'list-style-position': ['list-style'],
  60. 'list-style-image': ['list-style']
  61. };
  62. function getPropertyFingerprint(propertyName, declaration, fingerprints) {
  63. var realName = resolveProperty(propertyName).basename;
  64. if (realName === 'background') {
  65. return propertyName + ':' + generate(declaration.value);
  66. }
  67. var declarationId = declaration.id;
  68. var fingerprint = fingerprints[declarationId];
  69. if (!fingerprint) {
  70. switch (declaration.value.type) {
  71. case 'Value':
  72. var vendorId = '';
  73. var iehack = '';
  74. var special = {};
  75. var raw = false;
  76. declaration.value.children.each(function walk(node) {
  77. switch (node.type) {
  78. case 'Value':
  79. case 'Brackets':
  80. case 'Parentheses':
  81. node.children.each(walk);
  82. break;
  83. case 'Raw':
  84. raw = true;
  85. break;
  86. case 'Identifier':
  87. var name = node.name;
  88. if (!vendorId) {
  89. vendorId = resolveKeyword(name).vendor;
  90. }
  91. if (/\\[09]/.test(name)) {
  92. iehack = RegExp.lastMatch;
  93. }
  94. if (realName === 'cursor') {
  95. if (CURSOR_SAFE_VALUE.indexOf(name) === -1) {
  96. special[name] = true;
  97. }
  98. } else if (realName === 'position') {
  99. if (POSITION_SAFE_VALUE.indexOf(name) === -1) {
  100. special[name] = true;
  101. }
  102. } else if (DONT_MIX_VALUE.hasOwnProperty(realName)) {
  103. if (DONT_MIX_VALUE[realName].test(name)) {
  104. special[name] = true;
  105. }
  106. }
  107. break;
  108. case 'Function':
  109. var name = node.name;
  110. if (!vendorId) {
  111. vendorId = resolveKeyword(name).vendor;
  112. }
  113. if (name === 'rect') {
  114. // there are 2 forms of rect:
  115. // rect(<top>, <right>, <bottom>, <left>) - standart
  116. // rect(<top> <right> <bottom> <left>) – backwards compatible syntax
  117. // only the same form values can be merged
  118. var hasComma = node.children.some(function(node) {
  119. return node.type === 'Operator' && node.value === ',';
  120. });
  121. if (!hasComma) {
  122. name = 'rect-backward';
  123. }
  124. }
  125. special[name + '()'] = true;
  126. // check nested tokens too
  127. node.children.each(walk);
  128. break;
  129. case 'Dimension':
  130. var unit = node.unit;
  131. if (/\\[09]/.test(unit)) {
  132. iehack = RegExp.lastMatch;
  133. }
  134. switch (unit) {
  135. // is not supported until IE11
  136. case 'rem':
  137. // v* units is too buggy across browsers and better
  138. // don't merge values with those units
  139. case 'vw':
  140. case 'vh':
  141. case 'vmin':
  142. case 'vmax':
  143. case 'vm': // IE9 supporting "vm" instead of "vmin".
  144. special[unit] = true;
  145. break;
  146. }
  147. break;
  148. }
  149. });
  150. fingerprint = raw
  151. ? '!' + fingerprintId++
  152. : '!' + Object.keys(special).sort() + '|' + iehack + vendorId;
  153. break;
  154. case 'Raw':
  155. fingerprint = '!' + declaration.value.value;
  156. break;
  157. default:
  158. fingerprint = generate(declaration.value);
  159. }
  160. fingerprints[declarationId] = fingerprint;
  161. }
  162. return propertyName + fingerprint;
  163. }
  164. function needless(props, declaration, fingerprints) {
  165. var property = resolveProperty(declaration.property);
  166. if (NEEDLESS_TABLE.hasOwnProperty(property.basename)) {
  167. var table = NEEDLESS_TABLE[property.basename];
  168. for (var i = 0; i < table.length; i++) {
  169. var ppre = getPropertyFingerprint(property.prefix + table[i], declaration, fingerprints);
  170. var prev = props.hasOwnProperty(ppre) ? props[ppre] : null;
  171. if (prev && (!declaration.important || prev.item.data.important)) {
  172. return prev;
  173. }
  174. }
  175. }
  176. }
  177. function processRule(rule, item, list, props, fingerprints) {
  178. var declarations = rule.block.children;
  179. declarations.eachRight(function(declaration, declarationItem) {
  180. var property = declaration.property;
  181. var fingerprint = getPropertyFingerprint(property, declaration, fingerprints);
  182. var prev = props[fingerprint];
  183. if (prev && !dontRestructure.hasOwnProperty(property)) {
  184. if (declaration.important && !prev.item.data.important) {
  185. props[fingerprint] = {
  186. block: declarations,
  187. item: declarationItem
  188. };
  189. prev.block.remove(prev.item);
  190. // TODO: use it when we can refer to several points in source
  191. // declaration.loc = {
  192. // primary: declaration.loc,
  193. // merged: prev.item.data.loc
  194. // };
  195. } else {
  196. declarations.remove(declarationItem);
  197. // TODO: use it when we can refer to several points in source
  198. // prev.item.data.loc = {
  199. // primary: prev.item.data.loc,
  200. // merged: declaration.loc
  201. // };
  202. }
  203. } else {
  204. var prev = needless(props, declaration, fingerprints);
  205. if (prev) {
  206. declarations.remove(declarationItem);
  207. // TODO: use it when we can refer to several points in source
  208. // prev.item.data.loc = {
  209. // primary: prev.item.data.loc,
  210. // merged: declaration.loc
  211. // };
  212. } else {
  213. declaration.fingerprint = fingerprint;
  214. props[fingerprint] = {
  215. block: declarations,
  216. item: declarationItem
  217. };
  218. }
  219. }
  220. });
  221. if (declarations.isEmpty()) {
  222. list.remove(item);
  223. }
  224. }
  225. module.exports = function restructBlock(ast) {
  226. var stylesheetMap = {};
  227. var fingerprints = Object.create(null);
  228. walk(ast, {
  229. visit: 'Rule',
  230. reverse: true,
  231. enter: function(node, item, list) {
  232. var stylesheet = this.block || this.stylesheet;
  233. var ruleId = (node.pseudoSignature || '') + '|' + node.prelude.children.first().id;
  234. var ruleMap;
  235. var props;
  236. if (!stylesheetMap.hasOwnProperty(stylesheet.id)) {
  237. ruleMap = {};
  238. stylesheetMap[stylesheet.id] = ruleMap;
  239. } else {
  240. ruleMap = stylesheetMap[stylesheet.id];
  241. }
  242. if (ruleMap.hasOwnProperty(ruleId)) {
  243. props = ruleMap[ruleId];
  244. } else {
  245. props = {};
  246. ruleMap[ruleId] = props;
  247. }
  248. processRule.call(this, node, item, list, props, fingerprints);
  249. }
  250. });
  251. };