-
Notifications
You must be signed in to change notification settings - Fork 21
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
XML parser reverses attribute order #6341
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6341?orig=1
|
@som-snytt said: def main(args: Array[String]): Unit = {
val input = """<elem one="test" two="test2" three="test3"></elem>"""
val doc = XML.loadString(input)
println(doc)
val cpa = ConstructingParser.fromSource(io.Source.fromString(input), preserveWS = true)
val cpadoc = cpa.document()
val ppr = new PrettyPrinter(80,5)
println(ppr.format(cpadoc.docElem))
} I remember this issue from the first Scala code I ever wrote, but I haven't used XML since then, so bear with me. My solution was a hook in PrettyPrinter for formatting attributes: // in leafTag and startTag; but also Utility.toXML/serialize
//n.attributes buildString sb
formatAttributes(n, sb) I realize now this was my first fold ever: override protected def formatAttributes(n: Node, sb: StringBuilder) {
//n.attributes.toList.reverse.foldLeft(sb) { (a,b) => a.append(' '); b.toString1(a); a }
(sb /: n.attributes.toList.reverse) { (r,v) => r.append(' '); v.toString1(r); r }
} And maybe the last time I used /: except as an emoticon. I see toString1 is protected now, so cut/paste that, too. I don't know if such a formatAttributes hook is worth it? |
robinst said: The above is merely a workaround, but thanks anyway. It should really be fixed so that round-tripping does not change things around needlessly. |
@SethTisue said: Interested community members: if you consider this issue significant, feel free to open a new issue for it on GitHub, with links in both directions. |
Graeme Phillipson (cogpp) said: |
When parsing XML and then writing it out again, the order of attributes is reversed.
This prevents certain use cases where one wants to modify an XML file and have a minimal amount of changes. E.g. adding a new element to a file causes all existing attributes to get reversed.
The cause is here:
https://github.com/scala/scala/blob/81e3121eb09010375783d13e8c4c853686a34ffd/src/library/scala/xml/parsing/MarkupParser.scala#L302
aMap: MetaData is built by prepending new attributes to the linked list, causing the reversal.
The attached patch adds a failing test case. (It failed when I wrote it originally, now I can't test it because the build fails).
The text was updated successfully, but these errors were encountered: