extract-properties.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This extractor is used in level 2 optimizations
  2. // IMPORTANT: Mind Token class and this code is not related!
  3. // Properties will be tokenized in one step, see #429
  4. var Token = require('../../tokenizer/token');
  5. var serializeRules = require('../../writer/one-time').rules;
  6. var serializeValue = require('../../writer/one-time').value;
  7. function extractProperties(token) {
  8. var properties = [];
  9. var inSpecificSelector;
  10. var property;
  11. var name;
  12. var value;
  13. var i, l;
  14. if (token[0] == Token.RULE) {
  15. inSpecificSelector = !/[\.\+>~]/.test(serializeRules(token[1]));
  16. for (i = 0, l = token[2].length; i < l; i++) {
  17. property = token[2][i];
  18. if (property[0] != Token.PROPERTY)
  19. continue;
  20. name = property[1][1];
  21. if (name.length === 0)
  22. continue;
  23. if (name.indexOf('--') === 0)
  24. continue;
  25. value = serializeValue(property, i);
  26. properties.push([
  27. name,
  28. value,
  29. findNameRoot(name),
  30. token[2][i],
  31. name + ':' + value,
  32. token[1],
  33. inSpecificSelector
  34. ]);
  35. }
  36. } else if (token[0] == Token.NESTED_BLOCK) {
  37. for (i = 0, l = token[2].length; i < l; i++) {
  38. properties = properties.concat(extractProperties(token[2][i]));
  39. }
  40. }
  41. return properties;
  42. }
  43. function findNameRoot(name) {
  44. if (name == 'list-style')
  45. return name;
  46. if (name.indexOf('-radius') > 0)
  47. return 'border-radius';
  48. if (name == 'border-collapse' || name == 'border-spacing' || name == 'border-image')
  49. return name;
  50. if (name.indexOf('border-') === 0 && /^border\-\w+\-\w+$/.test(name))
  51. return name.match(/border\-\w+/)[0];
  52. if (name.indexOf('border-') === 0 && /^border\-\w+$/.test(name))
  53. return 'border';
  54. if (name.indexOf('text-') === 0)
  55. return name;
  56. if (name == '-chrome-')
  57. return name;
  58. return name.replace(/^\-\w+\-/, '').match(/([a-zA-Z]+)/)[0].toLowerCase();
  59. }
  60. module.exports = extractProperties;