names.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. var hasOwnProperty = Object.prototype.hasOwnProperty;
  3. var keywords = Object.create(null);
  4. var properties = Object.create(null);
  5. var HYPHENMINUS = 45; // '-'.charCodeAt()
  6. function isCustomProperty(str, offset) {
  7. offset = offset || 0;
  8. return str.length - offset >= 2 &&
  9. str.charCodeAt(offset) === HYPHENMINUS &&
  10. str.charCodeAt(offset + 1) === HYPHENMINUS;
  11. }
  12. function getVendorPrefix(str, offset) {
  13. offset = offset || 0;
  14. // verdor prefix should be at least 3 chars length
  15. if (str.length - offset >= 3) {
  16. // vendor prefix starts with hyper minus following non-hyper minus
  17. if (str.charCodeAt(offset) === HYPHENMINUS &&
  18. str.charCodeAt(offset + 1) !== HYPHENMINUS) {
  19. // vendor prefix should contain a hyper minus at the ending
  20. var secondDashIndex = str.indexOf('-', offset + 2);
  21. if (secondDashIndex !== -1) {
  22. return str.substring(offset, secondDashIndex + 1);
  23. }
  24. }
  25. }
  26. return '';
  27. }
  28. function getKeywordDescriptor(keyword) {
  29. if (hasOwnProperty.call(keywords, keyword)) {
  30. return keywords[keyword];
  31. }
  32. var name = keyword.toLowerCase();
  33. if (hasOwnProperty.call(keywords, name)) {
  34. return keywords[keyword] = keywords[name];
  35. }
  36. var custom = isCustomProperty(name, 0);
  37. var vendor = !custom ? getVendorPrefix(name, 0) : '';
  38. return keywords[keyword] = Object.freeze({
  39. basename: name.substr(vendor.length),
  40. name: name,
  41. vendor: vendor,
  42. prefix: vendor,
  43. custom: custom
  44. });
  45. }
  46. function getPropertyDescriptor(property) {
  47. if (hasOwnProperty.call(properties, property)) {
  48. return properties[property];
  49. }
  50. var name = property;
  51. var hack = property[0];
  52. if (hack === '/') {
  53. hack = property[1] === '/' ? '//' : '/';
  54. } else if (hack !== '_' &&
  55. hack !== '*' &&
  56. hack !== '$' &&
  57. hack !== '#' &&
  58. hack !== '+') {
  59. hack = '';
  60. }
  61. var custom = isCustomProperty(name, hack.length);
  62. // re-use result when possible (the same as for lower case)
  63. if (!custom) {
  64. name = name.toLowerCase();
  65. if (hasOwnProperty.call(properties, name)) {
  66. return properties[property] = properties[name];
  67. }
  68. }
  69. var vendor = !custom ? getVendorPrefix(name, hack.length) : '';
  70. var prefix = name.substr(0, hack.length + vendor.length);
  71. return properties[property] = Object.freeze({
  72. basename: name.substr(prefix.length),
  73. name: name.substr(hack.length),
  74. hack: hack,
  75. vendor: vendor,
  76. prefix: prefix,
  77. custom: custom
  78. });
  79. }
  80. module.exports = {
  81. keyword: getKeywordDescriptor,
  82. property: getPropertyDescriptor,
  83. isCustomProperty: isCustomProperty,
  84. vendorPrefix: getVendorPrefix
  85. };