Permalink
Browse files

#6 - only 10 xhtmlns can go through

  • Loading branch information...
1 parent 885b224 commit 30d8e224644a076b950ecd3673a3e27fd7d33201 @chris-twiner committed Sep 26, 2012
@@ -295,6 +295,25 @@ class XmlMarshallingTest extends junit.framework.TestCase {
str.toString.indexOf("<Child xmlns=\"\" />") > -1)
}
+ // tests #6
+ def testXHTMLNonEmpty = {
+ implicit val defaultSerializerFactory : SerializerFactory = serializers.LSSerializerFactoryXHTML
+ val str = new java.io.StringWriter()
+
+ val ns = Namespace(LSSerializerFactoryXHTML.xhtmlNS)
+
+ val builder = ns("Root") /( ns("Child"), ns("img") )
+ val r = writeTo(builder, str)
+
+// println(str.toString)
+ assertFalse("should not have an error", r.isDefined)
+ assertTrue("should not have had gaps on Child",
+ str.toString.indexOf("<Child></Child>") > -1)
+ assertTrue("but img should have",
+ str.toString.indexOf("<img />") > -1)
+
+ }
+
def testLSSerializerNoCacheFactory = {
implicit val defaultSerializerFactory : SerializerFactory = serializers.LSSerializerNoCacheFactory
val str = new java.io.StringWriter()
@@ -67,6 +67,13 @@ trait LSSerializerFactoryBase extends SerializerFactory {
* XHTML serialization extra touches
*/
object LSSerializerFactoryXHTML extends LSSerializerConcurrentCacheFactoryXHTML {
+ val xhtmlNS = "http://www.w3.org/1999/xhtml"
+
+ /**
+ * Local names of all 10 xhtml elements that can be empty, issue #6
+ */
+ val canBeEmpty = Set( "area", "base", "br", "col", "hr",
+ "img", "input", "link", "meta", "param" )
}
trait LSSerializerConcurrentCacheFactoryXHTML extends LSSerializerConcurrentCacheFactory {
@@ -171,8 +178,23 @@ trait XHTMLLSSerializer extends LSSerializer {
override def emptyElement(qName: QName, attributes: Traversable[Attribute], namespaces: Map[String, String], declareDefaultNS: Option[String], path: List[QName]): Option[Throwable] =
doElem(qName, attributes, namespaces, declareDefaultNS) orElse {
- out.append(" />")
- None
+ // #6
+ val canBeEmpty =
+ if (qName.namespace.uri == LSSerializerFactoryXHTML.xhtmlNS)
+ if (LSSerializerFactoryXHTML.canBeEmpty(qName.local))
+ true
+ else
+ false // all others must be <></>
+ else
+ true
+
+ if (canBeEmpty) {
+ out.append(" />")
+ None
+ } else {
+ out.append(">")
+ endElement(qName, path)
+ }
}
}

0 comments on commit 30d8e22

Please sign in to comment.