Skip to content

Commit 00fa469

Browse files
authored
Merge pull request scala#109 from OlivierBlanvillain/scala.js
Fix scala#49: Add Scala.js support
2 parents 4da6dfb + 834bfab commit 00fa469

File tree

108 files changed

+788
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+788
-33
lines changed

build.sbt

+29-33
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}
22

3-
scalaModuleSettings
4-
5-
name := "scala-xml"
6-
7-
version := "1.0.6-SNAPSHOT"
8-
9-
scalaVersion := crossScalaVersions.value.head
10-
11-
crossScalaVersions := {
3+
scalaVersion in ThisBuild := crossScalaVersions.value.head
4+
crossScalaVersions in ThisBuild := {
125
val java = System.getProperty("java.version")
136
if (java.startsWith("1.6.") || java.startsWith("1.7."))
147
Seq("2.11.8")
@@ -18,27 +11,30 @@ crossScalaVersions := {
1811
sys.error(s"don't know what Scala versions to build on $java")
1912
}
2013

21-
//reenable -Xfatal-warnings?
22-
scalacOptions ++= "-deprecation:false -feature -Xlint:-stars-align,-nullary-unit,_".split("\\s+").to[Seq]
23-
24-
scalacOptions in Test += "-Xxml:coalescing"
25-
26-
// important!! must come here (why?)
27-
scalaModuleOsgiSettings
28-
29-
OsgiKeys.exportPackage := Seq(s"scala.xml.*;version=${version.value}")
30-
31-
libraryDependencies += "junit" % "junit" % "4.11" % "test"
32-
33-
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"
34-
35-
//// testing:
36-
// used in CompilerErrors test
37-
libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml*")
38-
39-
mimaPreviousVersion := Some("1.0.5")
40-
41-
// You cannot disable JVM test forking when working on scala modules
42-
// that are distributed with the compiler because of an SBT
43-
// classloader leaking issue (scala/scala-xml#20 and #112).
44-
fork in Test := true
14+
lazy val root = project.in(file("."))
15+
.aggregate(xmlJS, xmlJVM)
16+
.settings(publish := {}, publishLocal := {})
17+
18+
lazy val xml = crossProject.in(file("."))
19+
.settings(
20+
name := "scala-xml",
21+
version := "1.0.6-SNAPSHOT",
22+
scalacOptions ++= "-deprecation:false -feature -Xlint:-stars-align,-nullary-unit,_".split("\\s+").to[Seq],
23+
scalacOptions in Test += "-Xxml:coalescing")
24+
.jvmSettings(
25+
scalaModuleSettings ++
26+
scalaModuleOsgiSettings ++
27+
List(
28+
OsgiKeys.exportPackage := Seq(s"scala.xml.*;version=${version.value}"),
29+
libraryDependencies += "junit" % "junit" % "4.11" % "test",
30+
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test",
31+
libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml*"),
32+
mimaPreviousVersion := Some("1.0.5"),
33+
// You cannot disable JVM test forking when working on scala modules
34+
// that are distributed with the compiler because of an SBT
35+
// classloader leaking issue (scala/scala-xml#20 and #112).
36+
fork in Test := true): _*)
37+
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
38+
39+
lazy val xmlJVM = xml.jvm
40+
lazy val xmlJS = xml.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package scala.xml
2+
3+
import scala.xml.transform._
4+
import org.junit.Test
5+
import org.junit.Assert.assertTrue
6+
import org.junit.Assert.assertEquals
7+
import org.junit.runner.RunWith
8+
/**
9+
* This test verify that after the tranform, the resultant xml node
10+
* uses as many old nodes as possible.
11+
*
12+
* Three transformers class for case -
13+
* One for orginal, one for modified, and one proposed which shows
14+
* all are equivalent when it comes to reusing as many nodes as possible
15+
*/
16+
object ReuseNodesTest {
17+
18+
class OriginalTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
19+
override def transform(ns: Seq[Node]): Seq[Node] = {
20+
val xs = ns.toStream map transform
21+
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) }
22+
23+
if (xs2.isEmpty) ns
24+
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1))
25+
}
26+
override def transform(n:Node): Seq[Node] = super.transform(n)
27+
}
28+
29+
class ModifiedTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
30+
override def transform(ns: Seq[Node]): Seq[Node] = {
31+
val changed = ns flatMap transform
32+
33+
if (changed.length != ns.length || (changed, ns).zipped.exists(_ != _)) changed
34+
else ns
35+
}
36+
override def transform(n:Node): Seq[Node] = super.transform(n)
37+
}
38+
39+
class AlternateTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
40+
override def transform(ns: Seq[Node]): Seq[Node] = {
41+
val xs = ns.toStream map transform
42+
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) }
43+
44+
if (xs2.isEmpty) ns
45+
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1))
46+
}
47+
override def transform(n:Node): Seq[Node] = super.transform(n)
48+
}
49+
50+
def rewriteRule = new RewriteRule {
51+
override def transform(n: Node): NodeSeq = n match {
52+
case n if n.label == "change" => Elem(
53+
n.prefix, "changed", n.attributes, n.scope, n.child.isEmpty, n.child : _*)
54+
case _ => n
55+
}
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Assert.assertFalse
9+
import org.junit.Assert.assertEquals
10+
11+
class XMLSyntaxTest {
12+
13+
private def handle[A](x: Node): A = {
14+
x.child(0).asInstanceOf[Atom[A]].data
15+
}
16+
17+
@Test
18+
def test1(): Unit = {
19+
val xNull = <hello>{null}</hello> // these used to be Atom(unit), changed to empty children
20+
assertTrue(xNull.child sameElements Nil)
21+
22+
val x0 = <hello>{}</hello> // these used to be Atom(unit), changed to empty children
23+
val x00 = <hello>{ }</hello> // dto.
24+
val xa = <hello>{ "world" }</hello>
25+
26+
assertTrue(x0.child sameElements Nil)
27+
assertTrue(x00.child sameElements Nil)
28+
assertEquals("world", handle[String](xa))
29+
30+
val xb = <hello>{ 1.5 }</hello>
31+
assertEquals(1.5, handle[Double](xb), 0.0)
32+
33+
val xc = <hello>{ 5 }</hello>
34+
assertEquals(5, handle[Int](xc))
35+
36+
val xd = <hello>{ true }</hello>
37+
assertEquals(true, handle[Boolean](xd))
38+
39+
val xe = <hello>{ 5:Short }</hello>
40+
assertEquals((5:Short), handle[Short](xe))
41+
42+
val xf = <hello>{ val x = 27; x }</hello>
43+
assertEquals(27, handle[Int](xf))
44+
45+
val xg = <hello>{ List(1,2,3,4) }</hello>
46+
assertEquals("<hello>1 2 3 4</hello>", xg.toString)
47+
assertFalse(xg.child.map(_.isInstanceOf[Text]).exists(identity))
48+
49+
val xh = <hello>{ for(x <- List(1,2,3,4) if x % 2 == 0) yield x }</hello>
50+
assertEquals("<hello>2 4</hello>", xh.toString)
51+
assertFalse(xh.child.map(_.isInstanceOf[Text]).exists(identity))
52+
}
53+
54+
/** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />,
55+
* so that Options can be used for optional attributes.
56+
*/
57+
@Test
58+
def test2(): Unit = {
59+
val x1: Option[Seq[Node]] = Some(<b>hello</b>)
60+
val n1 = <elem key={x1} />;
61+
assertEquals(x1, n1.attribute("key"))
62+
63+
val x2: Option[Seq[Node]] = None
64+
val n2 = <elem key={x2} />;
65+
assertEquals(x2, n2.attribute("key"))
66+
}
67+
68+
}

0 commit comments

Comments
 (0)