data.js 3.4 KB

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