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