Skip to content

Use toVector for XML literal sequences #23221

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ object MarkupParsers {
content_LT(ts)
charComingAfter(xSpaceOpt()) == '<'
} do ()
handle.makeXMLseq(Span(start, curOffset, start), ts)
handle.makeXMLseq(Span(start, curOffset, start), ts, toVector = false)
}
else {
assert(ts.length == 1, "Require one tree")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ class SymbolicXMLBuilder(parser: Parser, preserveWS: Boolean)(using Context) {
val _plus: TermName = "&+"
val _tmpscope: TermName = "$tmpscope"
val _xml: TermName = "xml"
val _toVector: TermName = "toVector"
}

import xmltypes.{_Comment, _Elem, _EntityRef, _Group, _MetaData, _NamespaceBinding, _NodeBuffer,
_PrefixedAttribute, _ProcInstr, _Text, _Unparsed, _UnprefixedAttribute}

import xmlterms.{_Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml}
import xmlterms.{_Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml, _toVector}

// convenience methods
private def LL[A](x: A*): List[List[A]] = List(x.toList)
Expand Down Expand Up @@ -103,7 +104,7 @@ class SymbolicXMLBuilder(parser: Parser, preserveWS: Boolean)(using Context) {
{
def starArgs =
if (children.isEmpty) Nil
else List(Typed(makeXMLseq(span, children), wildStar))
else List(Typed(makeXMLseq(span, children, toVector = true), wildStar))

def pat = Apply(_scala_xml__Elem, List(pre, label, wild, wild) ::: convertToTextPat(children))
def nonpat = New(_scala_xml_Elem, List(List(pre, label, attrs, scope, if (empty) Literal(Constant(true)) else Literal(Constant(false))) ::: starArgs))
Expand Down Expand Up @@ -152,7 +153,7 @@ class SymbolicXMLBuilder(parser: Parser, preserveWS: Boolean)(using Context) {
ts match {
case Nil => TypedSplice(tpd.ref(defn.NilModule).withSpan(span))
case t :: Nil => t
case _ => makeXMLseq(span, ts)
case _ => makeXMLseq(span, ts, toVector = true)
}
}

Expand All @@ -162,11 +163,12 @@ class SymbolicXMLBuilder(parser: Parser, preserveWS: Boolean)(using Context) {
}

/** could optimize if args.length == 0, args.length == 1 AND args(0) is <: Node. */
def makeXMLseq(span: Span, args: collection.Seq[Tree]): Block = {
def makeXMLseq(span: Span, args: collection.Seq[Tree], toVector: Boolean): Block = {
val buffer = ValDef(_buf, TypeTree(), New(_scala_xml_NodeBuffer, ListOfNil))
val applies = args filterNot isEmptyText map (t => Apply(Select(Ident(_buf), _plus), List(t)))

atSpan(span)(new XMLBlock(buffer :: applies.toList, Ident(_buf)) )
val res = if (toVector) Select(Ident(_buf), _toVector) else Ident(_buf)
atSpan(span)(new XMLBlock(buffer :: applies.toList, res))
}

/** Returns (Some(prefix) | None, rest) based on position of ':' */
Expand All @@ -177,7 +179,7 @@ class SymbolicXMLBuilder(parser: Parser, preserveWS: Boolean)(using Context) {

/** Various node constructions. */
def group(span: Span, args: collection.Seq[Tree]): Tree =
atSpan(span)( New(_scala_xml_Group, LL(makeXMLseq(span, args))) )
atSpan(span)( New(_scala_xml_Group, LL(makeXMLseq(span, args, toVector = true))) )

def unparsed(span: Span, str: String): Tree =
atSpan(span)( New(_scala_xml_Unparsed, LL(const(str))) )
Expand Down
25 changes: 21 additions & 4 deletions tests/run/xml.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@

object Test {
import scala.xml.NodeBuffer
import scala.xml._

def main(args: Array[String]): Unit = {
val xml = <hello>world</hello>
assert(xml.toString == "helloworld")
val nodeBuffer: NodeBuffer = <hello/><world/>
assert(nodeBuffer.mkString == "helloworld")
val sq: NodeBuffer = <hello/><world/>
assert(sq.mkString == "helloworld")

val subSq: Node = <a><b/><c/></a>
assert(subSq.child.toString == "Vector(b, c)") // implementation detail

val attrSeq: Elem = <a foo="txt&entityref;txt"/>
assert(attrSeq.attributes.asInstanceOf[UnprefixedAttribute].value.toString == "Vector(txt, &entityref;, txt)")

val g: Group = <xml:group><a/><b/><c/></xml:group>
assert(g.nodes.toString == "Vector(a, b, c)")
}
}
package scala.xml {
Expand All @@ -20,7 +29,7 @@ package scala.xml {
def child: Seq[Node]
override def toString = label + child.mkString
}
class Elem(prefix: String, val label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
class Elem(prefix: String, val label: String, val attributes: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
class NodeBuffer extends Seq[Node] {
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
def &+(o: Any): NodeBuffer =
Expand All @@ -39,4 +48,12 @@ package scala.xml {
def label = t.text
def child = Nil
}

case class UnprefixedAttribute(key: String, value: Seq[Node], next: MetaData)
case class EntityRef(entityName: String) extends Node {
def label = s"&$entityName;"
def child = Nil
}

case class Group(nodes: Seq[Node])
}
Loading