X-Git-Url: http://nitlanguage.org diff --git a/lib/html/html.nit b/lib/html/html.nit index b0d6f65..b5f5529 100644 --- a/lib/html/html.nit +++ b/lib/html/html.nit @@ -19,13 +19,18 @@ module html # # You can define subclass and override methods head and body # +# ~~~nitish # class MyPage # super HTMLPage # redef body do add("p").text("Hello World!") # end +# ~~~ # # HTMLPage use fluent interface so you can chain calls as: -# add("div").attr("id", "mydiv").text("My Div") +# +# ~~~nitish +# add("div").attr("id", "mydiv").text("My Div") +# ~~~ class HTMLPage super Writable @@ -51,7 +56,10 @@ class HTMLPage end # Add a html tag to the current element + # + # ~~~nitish # add("div").attr("id", "mydiv").text("My Div") + # ~~~ fun add(tag: String): HTMLTag do var node = new HTMLTag(tag) current.add(node) @@ -59,14 +67,20 @@ class HTMLPage end # Add a raw html string + # + # ~~~nitish # add_html("top") + # ~~~ fun add_html(html: String) do current.add(new HTMLRaw("", html)) # Open a html tag + # + # ~~~nitish # open("ul") # add("li").text("item1") # add("li").text("item2") # close("ul") + # ~~~ fun open(tag: String): HTMLTag do stack.push(current) current = add(tag) @@ -93,7 +107,12 @@ class HTMLTag # `"div"` for `
`. var tag: String init do - self.is_void = (once ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]).has(tag) + self.is_void = (once void_list).has(tag) + end + + private fun void_list: Set[String] + do + return new HashSet[String].from(["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]) end # Is the HTML element a void element? @@ -104,14 +123,15 @@ class HTMLTag # Create a HTML elements with the specifed type and attributes. init with_attrs(tag: String, attrs: Map[String, String]) do - self.tag = tag + init(tag) self.attrs = attrs end # Tag attributes map - var attrs: Map[String, String] = new HashMap[String, String] + var attrs: Map[String, String] = new HashMap[String, String] is lazy # Get the attributed value of 'prop' or null if 'prop' is undifened + # # var img = new HTMLTag("img") # img.attr("src", "./image.png").attr("alt", "image") # assert img.get_attr("src") == "./image.png" @@ -121,6 +141,7 @@ class HTMLTag end # Set a 'value' for 'key' + # # var img = new HTMLTag("img") # img.attr("src", "./image.png").attr("alt", "image") # assert img.write_to_string == """Hello World!
" - # Text is escaped see: `standard::String::html_escape` + # Text is escaped see: `core::String::html_escape` fun text(txt: String): HTMLTag do - children.clear + if isset _children then children.clear append(txt) return self end # Append text to element + # # var p = new HTMLTag("p") # p.append("Hello") # p.add(new HTMLTag("br")) # p.append("World!") # assert p.write_to_string == "Hello
World!