Sunday, July 19, 2009

My HTML in Scala Wishlist

One of my favorite features in Scala is its native support for XML(link warning, shameless plug.) I love the "projections DSL" (for lack of a better term) that is the scala.xml package. It is great for slicing up XML, and is almost like having native XPath and XQuery built into the language. Using XML in Scala for HTML generation is not as universally appealing to people. Some people think it encourages mixing presentation and business logic and is not "clean." I have even heard folks say that this is a critical flaw in Lift. I completely disagree. Of course I also think MVC is an anti-pattern, but anyways... HTML support in Scala is great in my opinion, but could be even better. Here is my wishlist.
  • Better understanding HTML semantics. For example, it is great that the compiler won't let me do something like this <div>Hello world</di>. However, I would not want it to even let me do <di>Hello world</di>. Similarly I should not be able to stick a thead outside of a table. If I have an anchor tag with an href attribute, then I'd like the compiler to do some validation on the value. Am I asking for too much? Well, that's I'm supposed to do, right? Is some/all of this possible with the help of XML schema support? Maybe so.
  • CSS. HTML without CSS ceased to exist a long time ago. With all of the DSL possibilities in Scala, is there some way to allow CSS literals and create some type safety for me. For example, I if I fat finger the name of a style on a class attribute, the compiler should catch this. Also I can say from experience that introducing an object-oriented model is really useful in CSS. Finally, the XPath/XQuery-like support in Scala XML makes me think that something similar could be done for CSS selectors.
  • JavaScript. Honestly I don't know what I would want here. I like JavaScript, but I know the idea of being able to write your web application in a single language is very appealing to a lot of people. If nothing else, it would once again be nice if the compiler would prevent me from doing something like <button onclick="foo()" ...> if there as no function called "foo" in scope.
  • Tooling. Most modern IDEs support things like syntax highlighting, code completion, error checking, code formatting for HTML and XML+schema. I expect at least this much for XML/HTML literals in Scala. I'd also like to be able to step through and debug literals. Refactoring on subtrees of XML (extract method for example) would be sweet. Oh, and large XML literals should not overwhelm my IDE :-)

3 comments:

  1. Interesting thoughts around type safety markup - after reading your article I guess the next step for you would be to implement a library that extends the functionality of scala.xml... specifically, you mention how you would like non-valid tags (di in your example) to be flagged, but remember that its only invalid in XHTML which is a micro-format of XML and XML validity is all scala cares about :-)

    Be it through a compiler plugin or library you could probably extend the scala.xml stuff to where you want it to be...?

    Tim

    ReplyDelete
  2. It's very much a work-in-progress (actually, more of a work-in-suspension right now), but I'm working on a Scala DSL to generate HTML that tries to reflect HTML's restrictions in the type system.

    Check out http://bitbucket.org/rafael/hoops/

    ReplyDelete
  3. I think that the best solution would be to extend Scala compiler, using compiler plugin. Ofcourse it is quite hard, but also would make working with HTML a great experience. Real time validation in every kind of editor, because all work would be done by the compiler, and at the same time the code would "feel like html".

    ReplyDelete