oozcitak / xmlbuilder2

An XML builder for node.js
362 stars 35 forks source link

callback API reference/position error #140

Closed b7kich closed 1 year ago

b7kich commented 2 years ago

Describe the bug new nodes aren't added to the element you refer to but to wherever the pointer currently is in the node tree, i.e. the latest element or it's parent(s) depending on the number of up() calls

To Reproduce See https://codesandbox.io/s/serverless-morning-gj0qou

function testXml(xml) {
  let root=xml.ele("root")
  root.ele("a")
  root.ele("b")
}

export default function App() {
  let xml = create();
  testXml(xml);
  let regular = xml.end();
  let buffer = "";
  let xmlCB = createCB({ data: (chunk) => (buffer = buffer + chunk) });
  testXml(xmlCB);
  xmlCB.end();
  console.log("Regular xmlbuilder2: "+regular)
  console.log("Callback xmlbuilder2: "+buffer)

...

Regular xmlbuilder2: <root><a/><b/></root> 
Callback xmlbuilder2: <root><a><b/></a></root> 

Expected behavior regular and callback xml should be the same

Version:

Additional context Add any other context about the problem here.

oozcitak commented 1 year ago

Current implementation of the callback API is similar to a stream. It is not possible to keep references to arbitrary nodes, as what information is kept about individual nodes is lost when they are serialized. This is emphasized clearly in the docs. Please see: https://oozcitak.github.io/xmlbuilder2/builder-functions-with-callbacks.html#createcb