Giter Site home page Giter Site logo

Comments (4)

cayhorstmann avatar cayhorstmann commented on June 15, 2024

The problem is that predefined entities are turned into Atom(c), where c is the character represented by the entity. For example, < turns into an Atom('<'). This happens in MarkupParser:

case '&' => // EntityRef or CharRef
      nextch(); ch match {
        case '#' => // CharacterRef
          nextch()
          val theChar = handle.text(tmppos, xCharRef(() => ch, () => nextch()))
          xToken(';')
         ts &+ theChar

The &+ operator is defined in NodeBuffer as follows:

def &+(o: Any): NodeBuffer = {
  o match {
    case null | _: Unit | Text("") => // ignore
    case it: Iterator[_]           => it foreach &+
    case n: Node                   => super.+=(n)
    case ns: Iterable[_]           => this &+ ns.iterator
    case ns: Array[_]              => this &+ ns.iterator
    case d                         => super.+=(new Atom(d))
  }
  ...
}

That sounds innocuous, but there is no good way to save such a thing. Calling XML.write or PrettyPrinter.formatNodes ultimately goes through Utilities.sequenceToXML and this branch:

else if (children forall isAtomAndNotText) { // add space
    val it = children.iterator
    val f = it.next()
    serialize(f, pscope, sb, stripComments, decodeEntities, preserveWhitespace, minimizeTags)
    while (it.hasNext) {
      val x = it.next()
      sb.append(' ')
      serialize(x, pscope, sb, stripComments, decodeEntities, preserveWhitespace, minimizeTags)
    }
}

When saving an element that contains only non-text atoms, they are separated by spaces.
The simplest remedy would be to change

ts &+ theChar 

to

ts &+ Text(theChar.toString)

in MarkupParser.

from scala-xml.

ashawley avatar ashawley commented on June 15, 2024

Good analysis of the problem. Not sure if that change to MarkupParser will fix it though.

The parser will need some work to be smart enough to preserve white space between entities. Obviously, white space shouldn't be preserved, when it's not desired, between other types of XML "nodes". However, that feature seems to be the source of the problem. An Atom is considered a Node, but entities are indeed a special case. They should be treated like text, so any adjacent whitespace should be treated as text.

The MarkupParser doesn't have a method to consume white space characters in to a Text object. The appendText method would be useful here. Unfortunately, the preserveWS affects the behavior of appendText.

from scala-xml.

ashawley avatar ashawley commented on June 15, 2024

I've taken a first pass at writing unit tests should anyone want to take this one on.

from scala-xml.

piyush-jaiswal avatar piyush-jaiswal commented on June 15, 2024

Hi @ashawley, I have tried to fix this issue and also added the tests you provided. It would be great if you could take a look at it. Thanks!

from scala-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.