Skip to content

HerringtonDarkholme/ScalaHtml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This repository, Scala HTML, is an experiment on Scala's language power. It provides a DSL to generate plain text html and compile-time linting by abusing type system. Scala HTML aims at both syntax level flexibility and type level safety, at the cost of poor runtime performance and compiling time.

Feature:

  1. Nothing but Plain Scala:
div.test.heheh |(
  p.testCls(id:="heh") | (
    div.child | (
      p.whathaha | a()
)))
  1. Class shortcut:

div.test will render <div class="test"></div>

  1. Attribute:

Every attribute is an object that has method := and that can be applied to tags. form(action:="test"), div(id:="hehe") and implicit conversion for (String, String) Pair like div("data-convert":="implicit")

  1. Linting

User can only pass elements of specific tag to another tag.

  val a = a() | p()
  // won't compile because `a` accept inline element
  // not true in html5 though, just for example
  val ul = ul() | li()
  1. Type Level Element Finder

Scala HTML has a jQ object mocking jQuery.

val k =
  div.test.heheh |(
    p.testCls(id:="heh") | (
      div.child("data-test":="h") | (
        p.whathaha | a()
  )))

val pElement = jQ(k).has[P]
// return an P[Div[P[A[Nothing]]]]

However, element finding is done in compile-time. Finding a non-existing element will cause compiling failure.

About

A type level programming experiment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages