Skip to content

Instantly share code, notes, and snippets.

@terjesb
Created November 3, 2011 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terjesb/1337238 to your computer and use it in GitHub Desktop.
Save terjesb/1337238 to your computer and use it in GitHub Desktop.
nu.validator.htmlparser and comments in Lift HTML5 template
<!DOCTYPE html>
<!-- test --><!--[if lt IE 7]> <html class="no-js ie6" lang="en"> <![endif]--><!--[if IE 7]> <html class="no-js ie7" lang="en"> <![endif]--><!--[if IE 8]> <html class="no-js ie8" lang="en"> <![endif]--><!--[if IE 9]> <html class="no-js ie9" lang="en"> <![endif]--><!--[if gt IE 9]><!--><html class="no-js lift:H5BP.lang" lang="en"><!--<![endif]--><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="">
<meta name="keywords" content="">
<title class="lift:Menu.title">App: </title>
<script class="lift:Html5Shiv"></script>
<style class="lift:CSS.blueprint"></style>
<style class="lift:CSS.fancyType"></style>
<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>
<script id="json" src="/classpath/json.js" type="text/javascript"></script>
<style type="text/css">
/* <![CDATA[ */
.edit_error_class {
display: block;
color: red;
}
.sidebar ul {
margin:0;
padding:0;
border-bottom:1px solid #ccc;
}
.sidebar ul li {
margin:0;
padding:0;
list-style:none;
border:1px solid #ccc;
border-bottom:none;
}
.sidebar ul li a {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li span {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li a:hover {
background-color: #eee;
}
/* ]]> */
</style>
</head>
<body>
<!-- test -->
<div class="container">
<!-- test -->
<div class="column span-12 last" style="text-align: right">
<h1 class="alt">app<img alt="" id="ajax-loader" style="display:none; margin-bottom: 0px; margin-left: 5px" src="/images/ajax-loader.gif"></h1>
</div>
<hr>
<div class="column span-6 colborder sidebar">
<hr class="space">
<span class="lift:Menu.builder"></span>
<div class="lift:Msgs?showAll=true"></div>
<hr class="space">
</div>
<div class="column span-17 last">
<div id="content">The main content will get bound here</div>
</div>
<hr>
<div class="column span-23 last" style="text-align: center">
<h4 class="alt">
<a href="http://www.liftweb.net"><i>Lift</i></a>
is Copyright 2007-2011 WorldWide Conferencing, LLC.
Distributed under an Apache 2.0 License.</h4>
</div>
</div>
</body></html>
So templates-hidden/h5bp-b.html is my template, and 2html.html the output from nu.validator.htmlparser with boilerplate and body comments intact.
java -cp .:htmlparser-1.3.1.jar nu.validator.htmlparser.tools.HTML2HTML h5bp-b.html
diff --git a/core/util/src/main/scala/net/liftweb/util/HtmlParser.scala b/core/util/src/main/scala/net/liftweb/util/HtmlParser.scala
index e9c8551..156aaab 100644
--- a/core/util/src/main/scala/net/liftweb/util/HtmlParser.scala
+++ b/core/util/src/main/scala/net/liftweb/util/HtmlParser.scala
@@ -321,6 +321,15 @@ object Html5Constants {
(t equalsIgnoreCase "style")
}
+trait CommentHandler extends org.xml.sax.ext.LexicalHandler {
+ def comment(ch: Array[Char], start: Int, length: Int) = Unit
+ def endCDATA() = Unit
+ def endDTD() = Unit
+ def endEntity(name: String) = Unit
+ def startCDATA() = Unit
+ def startDTD(name: String, publicId: String, systemId: String) = Unit
+ def startEntity(name: String) = Unit
+}
/**
* A utility that supports parsing of HTML5 file.
@@ -340,7 +349,7 @@ trait Html5Parser {
hp.setContentNonXmlCharPolicy(common.XmlViolationPolicy.ALLOW)
hp.setContentSpacePolicy(common.XmlViolationPolicy.FATAL)
hp.setNamePolicy(common.XmlViolationPolicy.ALLOW)
- val saxer = new NoBindingFactoryAdapter {
+ val saxer = new NoBindingFactoryAdapter with CommentHandler {
/*
override def createNode (pre: String, label: String, attrs: MetaData, scope: NamespaceBinding, children: List[Node]) : Elem = {
if (pre == "lift" && label == "head") {
@@ -350,6 +359,10 @@ trait Html5Parser {
}
}*/
+ override def comment(ch: Array[Char], start: Int, length: Int): Unit = {
+ hStack.push(scala.xml.Comment(ch.mkString))
+ }
+
override def captureText(): Unit = {
if (capture) {
val text = buffer.toString()
@@ -363,6 +376,7 @@ trait Html5Parser {
saxer.scopeStack.push(TopScope)
hp.setContentHandler(saxer)
+ hp.setProperty("http://xml.org/sax/properties/lexical-handler", saxer)
val is = new InputSource(in)
is.setEncoding("UTF-8")
hp.parse(is)
<!doctype html>
<!-- test -->
<!--[if lt IE 7]> <html class="no-js ie6" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="no-js ie9" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--> <html class="no-js lift:H5BP.lang" lang="en"> <!--<![endif]-->
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<title class="lift:Menu.title">App: </title>
<script class="lift:Html5Shiv"></script>
<style class="lift:CSS.blueprint"></style>
<style class="lift:CSS.fancyType"></style>
<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>
<script id="json" src="/classpath/json.js" type="text/javascript"></script>
<style type="text/css">
/* <![CDATA[ */
.edit_error_class {
display: block;
color: red;
}
.sidebar ul {
margin:0;
padding:0;
border-bottom:1px solid #ccc;
}
.sidebar ul li {
margin:0;
padding:0;
list-style:none;
border:1px solid #ccc;
border-bottom:none;
}
.sidebar ul li a {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li span {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li a:hover {
background-color: #eee;
}
/* ]]> */
</style>
</head>
<body>
<!-- test -->
<div class="container">
<!-- test -->
<div class="column span-12 last" style="text-align: right">
<h1 class="alt">app<img alt="" id="ajax-loader" style="display:none; margin-bottom: 0px; margin-left: 5px" src="/images/ajax-loader.gif"></h1>
</div>
<hr>
<div class="column span-6 colborder sidebar">
<hr class="space" >
<span class="lift:Menu.builder"></span>
<div class="lift:Msgs?showAll=true"></div>
<hr class="space" />
</div>
<div class="column span-17 last">
<div id="content">The main content will get bound here</div>
</div>
<hr />
<div class="column span-23 last" style="text-align: center">
<h4 class="alt">
<a href="http://www.liftweb.net"><i>Lift</i></a>
is Copyright 2007-2011 WorldWide Conferencing, LLC.
Distributed under an Apache 2.0 License.</h4>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment