Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

treebuilder.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function TreeBuilder(element_factory) {
  2. this._data = [];
  3. this._elem = [];
  4. this._last = null;
  5. this._tail = null;
  6. if (!element_factory) {
  7. /* evil circular dep */
  8. element_factory = require('./elementtree').Element;
  9. }
  10. this._factory = element_factory;
  11. }
  12. TreeBuilder.prototype.close = function() {
  13. return this._last;
  14. };
  15. TreeBuilder.prototype._flush = function() {
  16. if (this._data) {
  17. if (this._last !== null) {
  18. var text = this._data.join("");
  19. if (this._tail) {
  20. this._last.tail = text;
  21. }
  22. else {
  23. this._last.text = text;
  24. }
  25. }
  26. this._data = [];
  27. }
  28. };
  29. TreeBuilder.prototype.data = function(data) {
  30. this._data.push(data);
  31. };
  32. TreeBuilder.prototype.start = function(tag, attrs) {
  33. this._flush();
  34. var elem = this._factory(tag, attrs);
  35. this._last = elem;
  36. if (this._elem.length) {
  37. this._elem[this._elem.length - 1].append(elem);
  38. }
  39. this._elem.push(elem);
  40. this._tail = null;
  41. };
  42. TreeBuilder.prototype.end = function(tag) {
  43. this._flush();
  44. this._last = this._elem.pop();
  45. if (this._last.tag !== tag) {
  46. throw new Error("end tag mismatch");
  47. }
  48. this._tail = 1;
  49. return this._last;
  50. };
  51. exports.TreeBuilder = TreeBuilder;