stdlib/streams: Streamable now working on any Text type instead of just String.
[nit.git] / lib / html.nit
index 729f529..f49ba3a 100644 (file)
@@ -95,6 +95,9 @@ class HTMLTag
        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
 
        init with_attrs(tag: String, attrs: Map[String, String]) do
@@ -106,37 +109,48 @@ 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"
        fun get_attr(key: String): nullable String do
                if not attrs.has_key(key) then return null
                return attrs[key]
        end
 
        # Set a 'value' for 'key'
-       # var img = new HTMLTag("img")
-       # img.attr("src", "./image.png").attr("alt", "image")
+       #     var img = new HTMLTag("img")
+       #     img.attr("src", "./image.png").attr("alt", "image")
+       #     assert img.write_to_string      == """<img src="./image.png" alt="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")
+       #     var img = new HTMLTag("img")
+       #     img.add_class("logo").add_class("fullpage")
+       #     assert img.write_to_string      == """<img class="logo fullpage"/>"""
        fun add_class(klass: String): HTMLTag do
                classes.add(klass)
                return self
        end
+
+       # CSS classes
        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      == """<img class="logo fullpage"/>"""
        fun add_classes(classes: Collection[String]): HTMLTag do
                self.classes.add_all(classes)
                return self
        end
 
        # Set a CSS 'value' for 'prop'
-       # var img = new HTMLTag("img")
-       # img.css("border", "2px solid black").css("position", "absolute")
+       #     var img = new HTMLTag("img")
+       #     img.css("border", "2px solid black").css("position", "absolute")
+       #     assert img.write_to_string      == """<img style="border: 2px solid black; position: absolute"/>"""
        fun css(prop: String, value: String): HTMLTag do
                css_props[prop] = value
                return self
@@ -144,14 +158,42 @@ 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"
+       #     assert img.get_css("color")     == null
        fun get_css(prop: String): nullable String do
                if not css_props.has_key(prop) then return null
                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 == "<ul><li></li></ul>"
+       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"))
+       #     var ul = new HTMLTag("ul")
+       #     ul.add(new HTMLTag("li"))
+       #     assert ul.write_to_string    == "<ul><li></li></ul>"
        fun add(child: HTMLTag) do children.add(child)
 
        # List of children HTML elements
@@ -235,7 +277,7 @@ class HTMLTag
 
                if attrs.has_key("style") or not css_props.is_empty then
                        res.add " style=\""
-                       for k, v in attrs do
+                       for k, v in css_props do
                                res.add k.html_escape
                                res.add ": "
                                res.add v.html_escape