lib/serialization: add README.md
[nit.git] / lib / html / html.nit
index 4fc8530..b0d6f65 100644 (file)
@@ -27,7 +27,7 @@ module html
 # HTMLPage use fluent interface so you can chain calls as:
 #      add("div").attr("id", "mydiv").text("My Div")
 class HTMLPage
-       super Streamable
+       super Writable
 
        # Define head content
        fun head do end
@@ -60,7 +60,7 @@ class HTMLPage
 
        # Add a raw html string
        # add_html("<a href='#top'>top</a>")
-       fun add_html(html: String) do current.add(new HTMLRaw(html))
+       fun add_html(html: String) do current.add(new HTMLRaw("", html))
 
        # Open a html tag
        # open("ul")
@@ -84,13 +84,15 @@ class HTMLPage
        end
 end
 
+# An HTML element.
 class HTMLTag
-       super Streamable
+       super Writable
 
-       # HTML tagname: 'div' for <div></div>
+       # HTML element type.
+       #
+       # `"div"` for `<div></div>`.
        var tag: String
-       init(tag: String) do
-               self.tag = tag
+       init do
                self.is_void = (once ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]).has(tag)
        end
 
@@ -98,8 +100,9 @@ class HTMLTag
        #
        #     assert (new HTMLTag("img")).is_void    == true
        #     assert (new HTMLTag("p")).is_void      == false
-       var is_void: Bool
+       var is_void: Bool is noinit
 
+       # Create a HTML elements with the specifed type and attributes.
        init with_attrs(tag: String, attrs: Map[String, String]) do
                self.tag = tag
                self.attrs = attrs
@@ -120,7 +123,7 @@ class HTMLTag
        # Set a 'value' for 'key'
        #     var img = new HTMLTag("img")
        #     img.attr("src", "./image.png").attr("alt", "image")
-       #     assert img.write_to_string      == """<img src="./image.png" alt="image"/>"""
+       #     assert img.write_to_string      == """<img src=".&#47;image.png" alt="image"/>"""
        fun attr(key: String, value: String): HTMLTag do
                attrs[key] = value
                return self
@@ -237,7 +240,7 @@ class HTMLTag
        #     assert p.write_to_string      ==  "<p>Hello<br/>World!</p>"
        # Text is escaped see: standard::String::html_escape
        fun append(txt: String): HTMLTag do
-               add(new HTMLRaw(txt.html_escape))
+               add(new HTMLRaw("", txt.html_escape))
                return self
        end
 
@@ -250,7 +253,7 @@ class HTMLTag
        #
        # Note: the HTML in insered as it, no verification is done.
        fun add_raw_html(txt: String): HTMLTag do
-               add(new HTMLRaw(txt))
+               add(new HTMLRaw("", txt))
                return self
        end
 
@@ -326,7 +329,6 @@ end
 private class HTMLRaw
        super HTMLTag
 
-       private var content: String
-       init(content: String) do self.content = content
+       var content: String
        redef fun render_in(res) do res.add content
 end