Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing callbacks if {pretty: true} and only one method called on element? #195

Closed
Simran-B opened this issue Jan 25, 2019 · 2 comments
Closed
Labels

Comments

@Simran-B
Copy link

I observed a strange behavior while toying around with my own stringWriter (master branch at 1425618):

  • If I create an element and call .txt() or .raw() on it once, or provide the text for the element directly in the .ele() call, then there is no callback to writer.text / writer.raw
  • If I add another method call to .txt(), .raw() or .dummy(), then there is a callback
  • This only occurs if { pretty: true }

Demo script:

let xml = xmlbuilder.create("root")

xml
  .ele("textDirect", null, "[1]")

xml
  .ele("textSingle")
  .txt("[2]")
  
xml
  .ele("rawSingle")
  .raw("[3]")

xml
  .ele("textDirectDummy", null, "[4]")
  .dummy()

xml
  .ele("textDummy")
  .txt("[5]")
  .dummy()

xml
  .ele("rawDummy")
  .raw("[6]")
  .dummy()

xml
  .ele("twoTextNodes")
  .txt("[7]")
  .txt("[7]")

xml
  .ele("twoRaw")
  .raw("[8]")
  .raw("[8]")

xml
  .ele("rawAndTextNode")
  .raw("[9]")
  .txt("[9]")

xml.end(
    xmlbuilder.stringWriter(
    {
        pretty: true, // missing callbacks only if true!
        writer: {
            raw: function(node, options, level) {
                console.log(`${options.indent.repeat(level)}RAW ${node.value}`)
                return this._raw(node, options, level)
            },
            text: function(node, options, level) {
                console.log(`${options.indent.repeat(level)}TEXT ${node.value}`)
                return this._text(node, options, level)
            },
            element: function(node, options, level) {
                console.log(`${options.indent.repeat(level)}ELEMENT ${node.name}`)
                return this._element(node, options, level)
            }
        }
    }
    )
)

Output:

ELEMENT root
  ELEMENT textDirect
  ELEMENT textSingle
  ELEMENT rawSingle
  ELEMENT textDirectDummy
    TEXT [4]
  ELEMENT textDummy
    TEXT [5]
  ELEMENT rawDummy
    RAW [6]
  ELEMENT twoTextNodes
    TEXT [7]
    TEXT [7]
  ELEMENT twoRaw
    RAW [8]
    RAW [8]
  ELEMENT rawAndTextNode
    RAW [9]
    TEXT [9]

Expected output / { pretty: false }:

ELEMENT root
  ELEMENT textDirect
    TEXT [1]
  ELEMENT textSingle
    TEXT [2]
  ELEMENT rawSingle
    RAW [3]
  ELEMENT textDirectDummy
    TEXT [4]
  ELEMENT textDummy
    TEXT [5]
  ELEMENT rawDummy
    RAW [6]
  ELEMENT twoTextNodes
    TEXT [7]
    TEXT [7]
  ELEMENT twoRaw
    RAW [8]
    RAW [8]
  ELEMENT rawAndTextNode
    RAW [9]
    TEXT [9]

Is this a bug? Maybe a side-effect of an optimization? Or am I using writer overrides in a completely unanticipated and unsupported way?

@oozcitak
Copy link
Owner

oozcitak commented Feb 4, 2019

Thanks for the report. This is a bug.

@oozcitak oozcitak added the Bug label Feb 4, 2019
@Simran-B
Copy link
Author

Simran-B commented Feb 4, 2019

Thanks a lot for fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants