gamnit: make `SpriteSet` public so clients can use its services
[nit.git] / lib / html / html.nit
index 6a24c07..b5f5529 100644 (file)
@@ -107,7 +107,12 @@ class HTMLTag
        # `"div"` for `<div></div>`.
        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?
@@ -118,12 +123,12 @@ 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
        #
@@ -156,7 +161,7 @@ class HTMLTag
        end
 
        # CSS classes
-       var classes: Set[String] = new HashSet[String]
+       var classes: Set[String] = new HashSet[String] is lazy
 
        # Add multiple CSS classes
        #
@@ -177,7 +182,7 @@ class HTMLTag
                css_props[prop] = value
                return self
        end
-       private var css_props: Map[String, String] = new HashMap[String, String]
+       private var css_props: Map[String, String] = new HashMap[String, String] is lazy
 
        # Get CSS value for 'prop'
        #
@@ -239,17 +244,17 @@ class HTMLTag
        end
 
        # List of children HTML elements
-       var children: Set[HTMLTag] = new HashSet[HTMLTag]
+       var children: Set[HTMLTag] = new HashSet[HTMLTag] is lazy
 
        # Clear all child and set the text of element
        #
        #     var p = new HTMLTag("p")
        #     p.text("Hello World!")
        #     assert p.write_to_string      ==  "<p>Hello World!</p>"
-       # 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
@@ -261,7 +266,7 @@ class HTMLTag
        #     p.add(new HTMLTag("br"))
        #     p.append("World!")
        #     assert p.write_to_string      ==  "<p>Hello<br/>World!</p>"
-       # 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))
                return self
@@ -295,11 +300,11 @@ class HTMLTag
                res.add "<"
                res.add tag
                render_attrs_in(res)
-               if is_void and children.is_empty then
+               if is_void and (not isset _children or children.is_empty) then
                        res.add "/>"
                else
                        res.add ">"
-                       for child in children do child.render_in(res)
+                       if isset _children then for child in children do child.render_in(res)
                        res.add "</"
                        res.add tag
                        res.add ">"
@@ -307,6 +312,7 @@ class HTMLTag
        end
 
        private fun render_attrs_in(res: Sequence[String]) do
+               if not isset _attrs and not isset _classes and not isset _css_props then return
                if attrs.has_key("class") or not classes.is_empty then
                        res.add " class=\""
                        for cls in classes do