Custom DOCTYPE problem

51 views
Skip to first unread message

jhajduk

unread,
Feb 6, 2012, 3:50:27 AM2/6/12
to Lift
Hi,

I can't seem to get the .setDocType method of HtmlProperties to work.

I have the following.

class Boot {
def boot {

... stuff ..

LiftRules.htmlProperties.default.set((r: Req) =>new
JoeHtml5Properties(r.userAgent).setDocType(makeDocType).setHtmlWriter(htmlWriter))

}

def makeDocType = () => Full("""<!DOCTYPE html public "bewbz2">""")

// COPY PASTA FROM HtmlProperties.scala
final case class JoeHtml5Properties(userAgent: Box[String]) extends
HtmlProperties {
def docType: Box[String] = Full("""<!DOCTYPE html public
"BEWBZ">""")
def encoding: Box[String] = Empty

def contentType: Box[String] = {
Full("text/html; charset=utf-8")
}

def htmlParser: InputStream => Box[Elem] = Html5.parse _

def htmlWriter: (Node, Writer) => Unit =
Html5.write(_, _, false, !LiftRules.convertToEntity.vend)

def htmlOutputHeader: Box[String] = docType.map(_.trim + "\n")

val html5FormsSupport: Boolean = {
val r = S.request openOr Req.nil
r.isSafari5 || r.isFirefox36 || r.isFirefox40 ||
r.isChrome5 || r.isChrome6
}

val maxOpenRequests: Int =
LiftRules.maxConcurrentRequests.vend(S.request openOr Req.nil)
}


I am getting <!DOCTYPE html public "BEWBZ"> as my doctype, when I
should be getting <!DOCTYPE html public "bewbz2">

From poking through the code I can't seem to figure out why it is not
using the new doctype, because it is using my custom htmlWriter (not
shown) which is set in the same fashion.

jhajduk

unread,
Feb 6, 2012, 3:55:49 AM2/6/12
to Lift
30 seconds after writing this I noticed the following

if I set the HtmlOutputHeader to a new one it works.

...

LiftRules.htmlProperties.default.set((r: Req) =>new
JoeHtml5Properties(r.userAgent).setHtmlOutputHeader(testSetHeader).setHtmlWriter(htmlWriter))

}

def makeDocType = () => Full("""<!DOCTYPE html public "bewbz2">""")

def testSetHeader = () => makeDocType().map(_.trim + "\n")

It seems like regardless of what you set the docType to with
setDocType, the old htmloutput header will call the old doctype.

Regards,
Joseph Hajduk

David Pollak

unread,
Feb 6, 2012, 3:13:20 PM2/6/12
to lif...@googlegroups.com
It's an issues of what the meaning of "this" is.

Basically, htmlOutputHeader points to the "old" version of docType in:

  def setDocType(newDocType: () => Box[String]) = {
    val old = this
    new HtmlProperties {
      def docType = newDocType()
      def encoding = old.encoding
      def contentType = old.contentType
      def htmlOutputHeader = old.htmlOutputHeader
      def htmlParser = old.htmlParser
      def htmlWriter = old.htmlWriter
      def html5FormsSupport = old.html5FormsSupport
      def maxOpenRequests = old.maxOpenRequests
      def userAgent = old.userAgent
    }
  }

I chased this issue once before and can't really get a satisfactory answer to what should be done.  It's a nasty intersection of immutable data and inheritance. You're welcome to open a ticket at http://ticket.liftweb.net and assign it to me (you must be a watcher of the LiftWeb space on Assembla to open tickets).  I'll see if my brain can get it right this time.

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


jhajduk

unread,
Feb 7, 2012, 2:54:16 AM2/7/12
to Lift
I created the ticket #1194, and came up with a solution with some
specs that I posted in the comments which is also available here
https://gist.github.com/1758108

Thank you,
Joseph Hajduk

On Feb 6, 1:13 pm, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> It's an issues of what the meaning of "this" is.
>
> Basically, htmlOutputHeader points to the "old" version of docType in:
>
>   def setDocType(newDocType: () => Box[String]) = {
>     val old = this
>     new HtmlProperties {
>       def docType = newDocType()
>       def encoding = old.encoding
>       def contentType = old.contentType
>       def htmlOutputHeader = old.htmlOutputHeader
>       def htmlParser = old.htmlParser
>       def htmlWriter = old.htmlWriter
>       def html5FormsSupport = old.html5FormsSupport
>       def maxOpenRequests = old.maxOpenRequests
>       def userAgent = old.userAgent
>     }
>   }
>
> I chased this issue once before and can't really get a satisfactory answer
> to what should be done.  It's a nasty intersection of immutable data and
> inheritance. You're welcome to open a ticket athttp://ticket.liftweb.netandassign it to me (you must be a watcher of
> Visi.Pro, Cloud Computing for the Rest of Ushttp://visi.pro
> Lift, the simply functional web frameworkhttp://liftweb.net
Reply all
Reply to author
Forward
0 new messages