string-xform.js 966 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const BaseXform = require('../base-xform');
  2. class StringXform extends BaseXform {
  3. constructor(options) {
  4. super();
  5. this.tag = options.tag;
  6. this.attr = options.attr;
  7. this.attrs = options.attrs;
  8. }
  9. render(xmlStream, model) {
  10. if (model !== undefined) {
  11. xmlStream.openNode(this.tag);
  12. if (this.attrs) {
  13. xmlStream.addAttributes(this.attrs);
  14. }
  15. if (this.attr) {
  16. xmlStream.addAttribute(this.attr, model);
  17. } else {
  18. xmlStream.writeText(model);
  19. }
  20. xmlStream.closeNode();
  21. }
  22. }
  23. parseOpen(node) {
  24. if (node.name === this.tag) {
  25. if (this.attr) {
  26. this.model = node.attributes[this.attr];
  27. } else {
  28. this.text = [];
  29. }
  30. }
  31. }
  32. parseText(text) {
  33. if (!this.attr) {
  34. this.text.push(text);
  35. }
  36. }
  37. parseClose() {
  38. if (!this.attr) {
  39. this.model = this.text.join('');
  40. }
  41. return false;
  42. }
  43. }
  44. module.exports = StringXform;