composite-xform.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. const BaseXform = require('./base-xform');
  3. /* 'virtual' methods used as a form of documentation */
  4. /* eslint-disable class-methods-use-this */
  5. // base class for xforms that are composed of other xforms
  6. // offers some default implementations
  7. class CompositeXform extends BaseXform {
  8. createNewModel(node) {
  9. return {};
  10. }
  11. parseOpen(node) {
  12. // Typical pattern for composite xform
  13. this.parser = this.parser || this.map[node.name];
  14. if (this.parser) {
  15. this.parser.parseOpen(node);
  16. return true;
  17. }
  18. if (node.name === this.tag) {
  19. this.model = this.createNewModel(node);
  20. return true;
  21. }
  22. return false;
  23. }
  24. parseText(text) {
  25. // Default implementation. Send text to child parser
  26. if (this.parser) {
  27. this.parser.parseText(text);
  28. }
  29. }
  30. onParserClose(name, parser) {
  31. // parseClose has seen a child parser close
  32. // now need to incorporate into this.model somehow
  33. this.model[name] = parser.model;
  34. }
  35. parseClose(name) {
  36. // Default implementation
  37. if (this.parser) {
  38. if (!this.parser.parseClose(name)) {
  39. this.onParserClose(name, this.parser);
  40. this.parser = undefined;
  41. }
  42. return true;
  43. }
  44. return name !== this.tag;
  45. }
  46. }
  47. module.exports = CompositeXform;
  48. //# sourceMappingURL=composite-xform.js.map