6-restructBlock.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. switch (unit) {
  132. // is not supported until IE11
  133. case 'rem':
  134. // v* units is too buggy across browsers and better
  135. // don't merge values with those units
  136. case 'vw':
  137. case 'vh':
  138. case 'vmin':
  139. case 'vmax':
  140. case 'vm': // IE9 supporting "vm" instead of "vmin".
  141. special[unit] = true;
  142. break;
  143. }
  144. break;
  145. }
  146. });
  147. fingerprint = raw
  148. ? '!' + fingerprintId++
  149. : '!' + Object.keys(special).sort() + '|' + iehack + vendorId;
  150. break;
  151. case 'Raw':
  152. fingerprint = '!' + declaration.value.value;
  153. break;
  154. default:
  155. fingerprint = generate(declaration.value);
  156. }
  157. fingerprints[declarationId] = fingerprint;
  158. }
  159. return propertyName + fingerprint;
  160. }
  161. function needless(props, declaration, fingerprints) {
  162. var property = resolveProperty(declaration.property);
  163. if (NEEDLESS_TABLE.hasOwnProperty(property.basename)) {
  164. var table = NEEDLESS_TABLE[property.basename];
  165. for (var i = 0; i < table.length; i++) {
  166. var ppre = getPropertyFingerprint(property.prefix + table[i], declaration, fingerprints);
  167. var prev = props.hasOwnProperty(ppre) ? props[ppre] : null;
  168. if (prev && (!declaration.important || prev.item.data.important)) {
  169. return prev;
  170. }
  171. }
  172. }
  173. }
  174. function processRule(rule, item, list, props, fingerprints) {
  175. var declarations = rule.block.children;
  176. declarations.eachRight(function(declaration, declarationItem) {
  177. var property = declaration.property;
  178. var fingerprint = getPropertyFingerprint(property, declaration, fingerprints);
  179. var prev = props[fingerprint];
  180. if (prev && !dontRestructure.hasOwnProperty(property)) {
  181. if (declaration.important && !prev.item.data.important) {
  182. props[fingerprint] = {
  183. block: declarations,
  184. item: declarationItem
  185. };
  186. prev.block.remove(prev.item);
  187. // TODO: use it when we can refer to several points in source
  188. // declaration.loc = {
  189. // primary: declaration.loc,
  190. // merged: prev.item.data.loc
  191. // };
  192. } else {
  193. declarations.remove(declarationItem);
  194. // TODO: use it when we can refer to several points in source
  195. // prev.item.data.loc = {
  196. // primary: prev.item.data.loc,
  197. // merged: declaration.loc
  198. // };
  199. }
  200. } else {
  201. var prev = needless(props, declaration, fingerprints);
  202. if (prev) {
  203. declarations.remove(declarationItem);
  204. // TODO: use it when we can refer to several points in source
  205. // prev.item.data.loc = {
  206. // primary: prev.item.data.loc,
  207. // merged: declaration.loc
  208. // };
  209. } else {
  210. declaration.fingerprint = fingerprint;
  211. props[fingerprint] = {
  212. block: declarations,
  213. item: declarationItem
  214. };
  215. }
  216. }
  217. });
  218. if (declarations.isEmpty()) {
  219. list.remove(item);
  220. }
  221. }
  222. module.exports = function restructBlock(ast) {
  223. var stylesheetMap = {};
  224. var fingerprints = Object.create(null);
  225. walk(ast, {
  226. visit: 'Rule',
  227. reverse: true,
  228. enter: function(node, item, list) {
  229. var stylesheet = this.block || this.stylesheet;
  230. var ruleId = (node.pseudoSignature || '') + '|' + node.prelude.children.first().id;
  231. var ruleMap;
  232. var props;
  233. if (!stylesheetMap.hasOwnProperty(stylesheet.id)) {
  234. ruleMap = {};
  235. stylesheetMap[stylesheet.id] = ruleMap;
  236. } else {
  237. ruleMap = stylesheetMap[stylesheet.id];
  238. }
  239. if (ruleMap.hasOwnProperty(ruleId)) {
  240. props = ruleMap[ruleId];
  241. } else {
  242. props = {};
  243. ruleMap[ruleId] = props;
  244. }
  245. processRule.call(this, node, item, list, props, fingerprints);
  246. }
  247. });
  248. };