Open hugowschneider opened 9 years ago
Has there been any progress on this? I am outputting some embedded HTML in an XML doc and would like to override the rendering behavior to output <br/>
rather than 
.
Looks like it's the xmlbuilder-js lib that does the escaping but just wondering if you have any pre/post-processing hooks I could leverage?
In your opinion, @jameshowe, what would the hook provide for functionality?
How would you interact with said hooks?
@tflanagan in my head I just see it as a way to get in between the processing to manipulate the output e.g.
new xml2js.Builder({
renderOpts: {
preRenderText: function(el, val) {
if (...) {
// el is descendant of html tag
return val.replace(/\r/g, '<br/>');
}
}
}
});
preRenderText
would be called just before we call render (I don't know my way around the lib well enough to determine exactly where that would be). Passing in the actual element along with the value gives us the ability to do fine-grained overrides.
Alternatively, rather than having it set globally, we could set it at element level? e.g.
xml2js.parseString(defaultXml, function (err, result) {
...
result["span"].renderText = function(val) {
return val.replace(/\r/g, '<br/>');
}
});
The builder would just have to check for the existence of the handler.
xml2js.parseString()
already has the ability to hook into it via attrNameProcessors
, tagNameProcessors
, and valueProcessors
. This is because the underlying SAX parser is event based, making hooks extremely easy.
Looking at the xmlbuilder-js module, it looks like this might take some work before getting it 100%.
@tflanagan if I'm reading the xmlbuilder-js code correctly (which I may not be as I don't use CoffeeScript!), it seems like they provide the ability to override the XMLStringifier
methods in the constructor?
The blocker from xml2js is the fact we only pass specific options down. If, for arguments sake, we changed this code to:
rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype, {
headless: this.options.headless,
stringify: this.options.stringify
});
We could pass stringify-specific options which should allow us to override the behaviour e.g.
new xml2js.Builder({
stringify: {
eleText: function(val) {
...
}
}
});
The downside here though is we can't be specific about which elements we want to manipulate as we don't have access to the element.
Exactly, before this can be implemented in the fashion you are requesting, a PR will need to be made for xmlbuilder-js. Otherwise, anything done in here is a workaround hack.
+1
Is there any progress on this? I am hitting the same issue.
I am not working on pushing such a PR to xmlbuilder-js, but I encourrage you to do so.
Hi,
Every time I have a new line, a tab or some other special characters inside an attribute value, the xml builder creates a xml string with html entities like and . How can I disable it and get new lines and tabs in my attribute value?
My original xml block:
The result after parsing and building: