Skip to content

XML attributes parsed in reverse order #172

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

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions jvm/src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package scala.xml

import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals

class AttributeTestJVM {

@Test
def attributeOrder: Unit = {
val x = <x y="1" z="2"/>
assertEquals("""<x y="1" z="2"/>""", x.toString)
}

@Test
def attributesFromString: Unit = {
val str = """<x y="1" z="2"/>"""
val doc = XML.loadString(str)
assertEquals(str, doc.toString)
}

@Test
def attributesAndNamespaceFromString: Unit = {
val str = """<x xmlns:w="w" y="1" z="2"/>"""
val doc = XML.loadString(str)
assertNotEquals(str, doc.toString)
val str2 = """<x y="1" z="2" xmlns:w="w"/>"""
val doc2 = XML.loadString(str2)
assertEquals(str2, doc2.toString)
}

@Test(expected=classOf[SAXParseException])
def attributesFromStringWithDuplicate: Unit = {
val str = """<elem one="test" one="test1" two="test2" three="test3"></elem>"""
XML.loadString(str)
}
}
10 changes: 10 additions & 0 deletions jvm/src/test/scala/scala/xml/parsing/ConstructingParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ class ConstructingParserTest {

ConstructingParser.fromSource(source, true).content(TopScope)
}

@Test
def SI6341issue65: Unit = {
val str = """<elem one="test" two="test2" three="test3"/>"""
val cpa = ConstructingParser.fromSource(io.Source.fromString(str), preserveWS = true)
val cpadoc = cpa.document()
val ppr = new PrettyPrinter(80,5)
val out = ppr.format(cpadoc.docElem)
assertEquals(str, out)
}
}
5 changes: 5 additions & 0 deletions shared/src/main/scala/scala/xml/MetaData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ abstract class MetaData
if (f(this)) copy(next filter f)
else next filter f

def reverse: MetaData =
foldLeft(Null: MetaData) { (x, xs) =>
xs.copy(x)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the manner that the recursive data structure is reversed.

/** returns key of this MetaData item */
def key: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ abstract class FactoryAdapter extends DefaultHandler with factory.XMLLoader[Node
if (scopeStack.isEmpty) TopScope
else scopeStack.head

for (i <- 0 until attributes.getLength()) {
for (i <- (0 until attributes.getLength).reverse) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where it is added to the SAX parser infrastructure

val qname = attributes getQName i
val value = attributes getValue i
val (pre, key) = splitName(qname)
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/scala/scala/xml/parsing/MarkupParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
if (!aMap.wellformed(scope))
reportSyntaxError("double attribute")

(aMap, scope)
(aMap.reverse, scope)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where it is added to the constructing parser infrastructure

}

/**
Expand Down
5 changes: 5 additions & 0 deletions shared/src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class AttributeTest {
}

@Test
def attributeOrder: Unit = {
val x = <x y="1" z="2"/>
assertEquals("""<x y="1" z="2"/>""", x.toString)
}

def attributeToString: Unit = {
val expected: String = """<b x="&amp;"/>"""
assertEquals(expected, (<b x="&amp;"/>).toString)
Expand Down
7 changes: 7 additions & 0 deletions shared/src/test/scala/scala/xml/MetaDataTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ class MetaDataTest {
assertEquals(new Atom(3), domatch(z2))
}

@Test
def reverseTest: Unit = {
assertEquals("", Null.reverse.toString)
assertEquals(""" b="c"""", <a b="c"/>.attributes.reverse.toString)
assertEquals(""" d="e" b="c"""", <a b="c" d="e"/>.attributes.reverse.toString)
}

}
2 changes: 2 additions & 0 deletions shared/src/test/scala/scala/xml/UtilityTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class UtilityTest {

@Test
def sort: Unit = {
assertEquals("", xml.Utility.sort(<a/>.attributes).toString)
assertEquals(""" b="c"""", xml.Utility.sort(<a b="c"/>.attributes).toString)
val q = xml.Utility.sort(<a g='3' j='2' oo='2' a='2'/>)
assertEquals(" a=\"2\" g=\"3\" j=\"2\" oo=\"2\"", xml.Utility.sort(q.attributes).toString)
val pp = new xml.PrettyPrinter(80,5)
Expand Down