data.cjs 3.4 KB

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