web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.5 KiB

  1. var template = require('lodash.template');
  2. var decls = require('./decls.json');
  3. /*
  4. Rules legend:
  5. - combined - if rule is combined it will be rendered with template
  6. - combined and basic rules are present in basic reset
  7. - combined, basic and inherited rules are present in full reset
  8. */
  9. function _getRulesMap(inputDecls) {
  10. return inputDecls
  11. .filter(function (decl) {
  12. return !decl.combined;
  13. })
  14. .reduce(function (map, decl) {
  15. map[decl.prop.replace(/\-/g, '')] = decl.initial;
  16. return map;
  17. }, {});
  18. }
  19. function _compileDecls(inputDecls) {
  20. var templateVars = _getRulesMap(inputDecls);
  21. return inputDecls.map(function (decl) {
  22. if (decl.combined && decl.initial) {
  23. var t = template(decl.initial.replace(/\-/g, ''));
  24. decl.initial = t(templateVars);
  25. }
  26. return decl;
  27. });
  28. }
  29. function _getRequirements(inputDecls) {
  30. return inputDecls.reduce(function (map, decl) {
  31. if (!decl.contains) return map;
  32. return decl.contains.reduce(function (mapInner, dependensy) {
  33. mapInner[dependensy] = decl;
  34. return mapInner;
  35. }, map);
  36. }, {});
  37. }
  38. function _expandContainments(inputDecls) {
  39. var requiredMap = _getRequirements(inputDecls);
  40. return inputDecls
  41. .filter(function (decl) {
  42. return !decl.contains;
  43. }).map(function (decl) {
  44. var dependensy = requiredMap[decl.prop];
  45. if (dependensy) {
  46. decl.requiredBy = dependensy.prop;
  47. decl.basic = decl.basic || dependensy.basic;
  48. decl.inherited = decl.inherited || dependensy.inherited;
  49. }
  50. return decl;
  51. });
  52. }
  53. var compiledDecls = _expandContainments(_compileDecls(decls));
  54. function _clearDecls(rules, value) {
  55. return rules.map(function (rule) {
  56. return {
  57. prop: rule.prop,
  58. value: value.replace(/initial/g, rule.initial)
  59. };
  60. });
  61. }
  62. function _allDecls(onlyInherited) {
  63. return compiledDecls.filter(function (decl) {
  64. var allowed = decl.combined || decl.basic;
  65. if (onlyInherited) return allowed && decl.inherited;
  66. return allowed;
  67. });
  68. }
  69. function _concreteDecl(declName) {
  70. return compiledDecls.filter(function (decl) {
  71. return declName === decl.prop || declName === decl.requiredBy;
  72. });
  73. }
  74. function makeFallbackFunction(onlyInherited) {
  75. return function (declName, declValue) {
  76. var result;
  77. if (declName === 'all') {
  78. result = _allDecls(onlyInherited);
  79. } else {
  80. result = _concreteDecl(declName);
  81. }
  82. return _clearDecls(result, declValue);
  83. };
  84. }
  85. module.exports = makeFallbackFunction;