X-Git-Url: http://nitlanguage.org diff --git a/lib/html/html.nit b/lib/html/html.nit index 4fc8530..72284ef 100644 --- a/lib/html/html.nit +++ b/lib/html/html.nit @@ -19,15 +19,20 @@ 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 Streamable + super Writable # Define head content fun head do end @@ -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)) + # ~~~ + 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) @@ -84,22 +98,30 @@ class HTMLPage end end +# An HTML element. class HTMLTag - super Streamable + super Writable - # HTML tagname: 'div' for
+ # HTML element type. + # + # `"div"` for `
`. var tag: String - init(tag: String) do - self.tag = tag - self.is_void = (once ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]).has(tag) + init do + 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? # # 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 @@ -109,6 +131,7 @@ class HTMLTag var attrs: Map[String, String] = new HashMap[String, String] # 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" @@ -118,15 +141,17 @@ 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 == """image""" + # assert img.write_to_string == """image""" fun attr(key: String, value: String): HTMLTag do attrs[key] = value return self end # Add a CSS class to the HTML tag + # # var img = new HTMLTag("img") # img.add_class("logo").add_class("fullpage") # assert img.write_to_string == """""" @@ -139,6 +164,7 @@ class HTMLTag var classes: Set[String] = new HashSet[String] # Add multiple CSS classes + # # var img = new HTMLTag("img") # img.add_classes(["logo", "fullpage"]) # assert img.write_to_string == """""" @@ -148,6 +174,7 @@ class HTMLTag end # Set a CSS 'value' for 'prop' + # # var img = new HTMLTag("img") # img.css("border", "2px solid black").css("position", "absolute") # assert img.write_to_string == """""" @@ -158,6 +185,7 @@ class HTMLTag private var css_props: Map[String, String] = new HashMap[String, String] # Get CSS value for 'prop' + # # var img = new HTMLTag("img") # img.css("border", "2px solid black").css("position", "absolute") # assert img.get_css("border") == "2px solid black" @@ -191,6 +219,7 @@ class HTMLTag end # Add a HTML 'child' to self + # # var ul = new HTMLTag("ul") # ul.add(new HTMLTag("li")) # assert ul.write_to_string == "" @@ -218,10 +247,11 @@ class HTMLTag var children: Set[HTMLTag] = new HashSet[HTMLTag] # Clear all child and set the text of element + # # var p = new HTMLTag("p") # p.text("Hello World!") # assert p.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 @@ -230,14 +260,15 @@ class HTMLTag 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!

" - # Text is escaped see: standard::String::html_escape + # Text is escaped see: core::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 +281,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 +357,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