index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const mdnAtrules = require('mdn-data/css/at-rules.json');
  2. const mdnProperties = require('mdn-data/css/properties.json');
  3. const mdnSyntaxes = require('mdn-data/css/syntaxes.json');
  4. const patch = require('./patch.json');
  5. const extendSyntax = /^\s*\|\s*/;
  6. function preprocessAtrules(dict) {
  7. const result = Object.create(null);
  8. for (const atruleName in dict) {
  9. const atrule = dict[atruleName];
  10. let descriptors = null;
  11. if (atrule.descriptors) {
  12. descriptors = Object.create(null);
  13. for (const descriptor in atrule.descriptors) {
  14. descriptors[descriptor] = atrule.descriptors[descriptor].syntax;
  15. }
  16. }
  17. result[atruleName.substr(1)] = {
  18. prelude: atrule.syntax.trim().match(/^@\S+\s+([^;\{]*)/)[1].trim() || null,
  19. descriptors
  20. };
  21. }
  22. return result;
  23. }
  24. function patchDictionary(dict, patchDict) {
  25. const result = {};
  26. // copy all syntaxes for an original dict
  27. for (const key in dict) {
  28. result[key] = dict[key].syntax || dict[key];
  29. }
  30. // apply a patch
  31. for (const key in patchDict) {
  32. if (key in dict) {
  33. if (patchDict[key].syntax) {
  34. result[key] = extendSyntax.test(patchDict[key].syntax)
  35. ? result[key] + ' ' + patchDict[key].syntax.trim()
  36. : patchDict[key].syntax;
  37. } else {
  38. delete result[key];
  39. }
  40. } else {
  41. if (patchDict[key].syntax) {
  42. result[key] = patchDict[key].syntax.replace(extendSyntax, '');
  43. }
  44. }
  45. }
  46. return result;
  47. }
  48. function unpackSyntaxes(dict) {
  49. const result = {};
  50. for (const key in dict) {
  51. result[key] = dict[key].syntax;
  52. }
  53. return result;
  54. }
  55. function patchAtrules(dict, patchDict) {
  56. const result = {};
  57. // copy all syntaxes for an original dict
  58. for (const key in dict) {
  59. const patchDescriptors = (patchDict[key] && patchDict[key].descriptors) || null;
  60. result[key] = {
  61. prelude: key in patchDict && 'prelude' in patchDict[key]
  62. ? patchDict[key].prelude
  63. : dict[key].prelude || null,
  64. descriptors: dict[key].descriptors
  65. ? patchDictionary(dict[key].descriptors, patchDescriptors || {})
  66. : patchDescriptors && unpackSyntaxes(patchDescriptors)
  67. };
  68. }
  69. // apply a patch
  70. for (const key in patchDict) {
  71. if (!hasOwnProperty.call(dict, key)) {
  72. result[key] = {
  73. prelude: patchDict[key].prelude || null,
  74. descriptors: patchDict[key].descriptors && unpackSyntaxes(patchDict[key].descriptors)
  75. };
  76. }
  77. }
  78. return result;
  79. }
  80. module.exports = {
  81. types: patchDictionary(mdnSyntaxes, patch.syntaxes),
  82. atrules: patchAtrules(preprocessAtrules(mdnAtrules), patch.atrules),
  83. properties: patchDictionary(mdnProperties, patch.properties)
  84. };