X-Git-Url: http://nitlanguage.org diff --git a/lib/html.nit b/lib/html.nit index 515a871..4fc8530 100644 --- a/lib/html.nit +++ b/lib/html.nit @@ -96,8 +96,8 @@ class HTMLTag # Is the HTML element a void element? # - # assert new HTMLTag("img").is_void == true - # assert new HTMLTag("p").is_void == false + # assert (new HTMLTag("img")).is_void == true + # assert (new HTMLTag("p")).is_void == false var is_void: Bool init with_attrs(tag: String, attrs: Map[String, String]) do @@ -167,11 +167,52 @@ class HTMLTag return css_props[prop] end + # Replace `self` by `parent`. + # + # var elem = new HTMLTag("li") + # elem.add_outer(new HTMLTag("ul")) + # assert elem.write_to_string == "" + fun add_outer(parent: HTMLTag) do + # copy self in new object + var child = new HTMLTag(self.tag) + child.attrs = self.attrs + child.classes = self.classes + child.css_props = self.css_props + child.children = self.children + # add copy in parent children elements + parent.children.add(child) + # replace self by parent + self.tag = parent.tag + self.attrs = parent.attrs + self.classes = parent.classes + self.css_props = parent.css_props + self.is_void = parent.is_void + self.children = parent.children + end + # Add a HTML 'child' to self # var ul = new HTMLTag("ul") # ul.add(new HTMLTag("li")) # assert ul.write_to_string == "" - fun add(child: HTMLTag) do children.add(child) + # returns `self` for fluent programming + fun add(child: HTMLTag): HTMLTag + do + children.add(child) + return self + end + + # Create a new HTMLTag child and return it + # + # var ul = new HTMLTag("ul") + # ul.open("li").append("1").append("2") + # ul.open("li").append("3").append("4") + # assert ul.write_to_string == "" + fun open(tag: String): HTMLTag + do + var res = new HTMLTag(tag) + add(res) + return res + end # List of children HTML elements var children: Set[HTMLTag] = new HashSet[HTMLTag] @@ -201,11 +242,13 @@ class HTMLTag end # Append raw HTML to element + # # var p = new HTMLTag("p") # p.append("Hello") - # p.add_raw_html("") - # p.html #- "

Hello

" - # Note: the HTML in insered as it, no verification is done + # p.add_raw_html("foo") + # assert p.write_to_string == "

Hellofoo

" + # + # Note: the HTML in insered as it, no verification is done. fun add_raw_html(txt: String): HTMLTag do add(new HTMLRaw(txt)) return self