summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/Xhtml.scala
blob: 1fe16c71b33239313fae307a6ca732254e598526 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// $Id$

package scala.xml

import parsing.XhtmlEntities

/* (c) David Pollak  2007 WorldWide Conferencing, LLC */

object Xhtml
{
  /**
   * Convenience function: same as toXhtml(node, false, false)
   *
   * @param node      the node
   */
  def toXhtml(node: Node): String = toXhtml(node, false, false)

  /**
   * Convenience function: amounts to calling toXhtml(node) on each
   * node in the sequence.
   *
   * @param nodeSeq   the node sequence
   */
  def toXhtml(nodeSeq: NodeSeq): String = {
    val sb = new StringBuilder
    sequenceToXML(nodeSeq, TopScope, sb, false, false)
    sb.toString
  }

  /**
   * Convenience function: amounts to calling toXhtml(node, TopScope, ...)
   * with the supplied parameters.
   *
   * @param nodeSeq   the node sequence
   */
  def toXhtml(n: Node, stripComment: Boolean, convertAmp: Boolean): String = {
    val sb = new StringBuilder()
    toXhtml(n, TopScope, sb, stripComment, convertAmp)
    sb.toString()
  }

  /**
   * Appends a tree to the given stringbuffer within given namespace scope.
   *
   * @param n            the node
   * @param pscope       the parent scope
   * @param sb           stringbuffer to append to
   * @param stripComment if true, strip comments
   * @param convertAmp   if true, decode entity references
   */
  def toXhtml(
    x: Node,
    pscope: NamespaceBinding,
    sb: StringBuilder,
    stripComment: Boolean,
    convertAmp: Boolean): Unit =
  {
    def decode(er: EntityRef) = XhtmlEntities.entMap.get(er.entityName) match {
      case Some(chr) if chr.toInt >= 128  => sb.append(chr)
      case _                              => er.buildString(sb)
    }
    def shortForm =
      (x.child == null || x.child.length == 0) &&
      !(List("div", "script", "textarea") contains x.label)

    x match {
      case c: Comment if !stripComment    => c.buildString(sb)
      case er: EntityRef if convertAmp    => decode(er)
      case x: SpecialNode                 => x.buildString(sb)
      case g: Group                       =>
        g.nodes foreach { toXhtml(_, x.scope, sb, stripComment, convertAmp) }

      case _  =>
        sb.append('<')
        x.nameToString(sb)
        if (x.attributes ne null) x.attributes.buildString(sb)
        x.scope.buildString(sb, pscope)

        if (shortForm) sb.append(" />")
        else {
          sb.append('>')
          sequenceToXML(x.child, x.scope, sb, stripComment, convertAmp)
          sb.append("</")
          x.nameToString(sb)
          sb.append('>')
        }
    }
  }

  /**
   * Amounts to calling toXhtml(node, ...) with the given parameters on each node.
   */
  def sequenceToXML(
    children: Seq[Node],
    pscope: NamespaceBinding,
    sb: StringBuilder,
    stripComment: Boolean,
    convertAmp: Boolean): Unit =
  {
    def isAtomAndNotText(x: Node) = x.isInstanceOf[Atom[_]] && !x.isInstanceOf[Text]
    val doSpaces = children forall isAtomAndNotText // interleave spaces

    for (c <- children.take(children.length - 1)) {
      toXhtml(c, pscope, sb, stripComment, convertAmp)
      if (doSpaces) sb append ' '
    }
    toXhtml(children.last, pscope, sb, stripComment, convertAmp)
  }
}