d3a92eb628d7c0a4e7ff7c9272005c33ec26ccd6
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # HTML output facilities
20 # You can define subclass and override methods head and body
25 # redef body do add("p").text("Hello World!")
29 # HTMLPage use fluent interface so you can chain calls as:
32 # add("div").attr("id", "mydiv").text("My Div")
42 private var root
= new HTMLTag("html")
43 private var current
: HTMLTag = root
44 private var stack
= new List[HTMLTag]
46 redef fun write_to
(stream
) do
54 stream
.write
"<!DOCTYPE html>"
58 # Add a html tag to the current element
61 # add("div").attr("id", "mydiv").text("My Div")
63 fun add
(tag
: String): HTMLTag do
64 var node
= new HTMLTag(tag
)
69 # Add a raw html string
72 # add_html("<a href='#top'>top</a>")
74 fun add_html
(html
: String) do current
.add
(new HTMLRaw("", html
))
80 # add("li").text("item1")
81 # add("li").text("item2")
84 fun open
(tag
: String): HTMLTag do
90 # Close previously opened tag
91 # Ensure: tag = previous.tag
92 fun close
(tag
: String) do
93 if not tag
== current
.tag
then
94 print
"Error: Trying to close '{tag}', last opened tag was '{current.tag}'."
107 # `"div"` for `<div></div>`.
110 self.is_void
= (once
["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]).has
(tag
)
113 # Is the HTML element a void element?
115 # assert (new HTMLTag("img")).is_void == true
116 # assert (new HTMLTag("p")).is_void == false
117 var is_void
: Bool is noinit
119 # Create a HTML elements with the specifed type and attributes.
120 init with_attrs
(tag
: String, attrs
: Map[String, String]) do
126 var attrs
: Map[String, String] = new HashMap[String, String]
128 # Get the attributed value of 'prop' or null if 'prop' is undifened
130 # var img = new HTMLTag("img")
131 # img.attr("src", "./image.png").attr("alt", "image")
132 # assert img.get_attr("src") == "./image.png"
133 fun get_attr
(key
: String): nullable String do
134 if not attrs
.has_key
(key
) then return null
138 # Set a 'value' for 'key'
140 # var img = new HTMLTag("img")
141 # img.attr("src", "./image.png").attr("alt", "image")
142 # assert img.write_to_string == """<img src="./image.png" alt="image"/>"""
143 fun attr
(key
: String, value
: String): HTMLTag do
148 # Add a CSS class to the HTML tag
150 # var img = new HTMLTag("img")
151 # img.add_class("logo").add_class("fullpage")
152 # assert img.write_to_string == """<img class="logo fullpage"/>"""
153 fun add_class
(klass
: String): HTMLTag do
159 var classes
: Set[String] = new HashSet[String]
161 # Add multiple CSS classes
163 # var img = new HTMLTag("img")
164 # img.add_classes(["logo", "fullpage"])
165 # assert img.write_to_string == """<img class="logo fullpage"/>"""
166 fun add_classes
(classes
: Collection[String]): HTMLTag do
167 self.classes
.add_all
(classes
)
171 # Set a CSS 'value' for 'prop'
173 # var img = new HTMLTag("img")
174 # img.css("border", "2px solid black").css("position", "absolute")
175 # assert img.write_to_string == """<img style="border: 2px solid black; position: absolute"/>"""
176 fun css
(prop
: String, value
: String): HTMLTag do
177 css_props
[prop
] = value
180 private var css_props
: Map[String, String] = new HashMap[String, String]
182 # Get CSS value for 'prop'
184 # var img = new HTMLTag("img")
185 # img.css("border", "2px solid black").css("position", "absolute")
186 # assert img.get_css("border") == "2px solid black"
187 # assert img.get_css("color") == null
188 fun get_css
(prop
: String): nullable String do
189 if not css_props
.has_key
(prop
) then return null
190 return css_props
[prop
]
193 # Replace `self` by `parent`.
195 # var elem = new HTMLTag("li")
196 # elem.add_outer(new HTMLTag("ul"))
197 # assert elem.write_to_string == "<ul><li></li></ul>"
198 fun add_outer
(parent
: HTMLTag) do
199 # copy self in new object
200 var child
= new HTMLTag(self.tag
)
201 child
.attrs
= self.attrs
202 child
.classes
= self.classes
203 child
.css_props
= self.css_props
204 child
.children
= self.children
205 # add copy in parent children elements
206 parent
.children
.add
(child
)
207 # replace self by parent
208 self.tag
= parent
.tag
209 self.attrs
= parent
.attrs
210 self.classes
= parent
.classes
211 self.css_props
= parent
.css_props
212 self.is_void
= parent
.is_void
213 self.children
= parent
.children
216 # Add a HTML 'child' to self
218 # var ul = new HTMLTag("ul")
219 # ul.add(new HTMLTag("li"))
220 # assert ul.write_to_string == "<ul><li></li></ul>"
221 # returns `self` for fluent programming
222 fun add
(child
: HTMLTag): HTMLTag
228 # Create a new HTMLTag child and return it
230 # var ul = new HTMLTag("ul")
231 # ul.open("li").append("1").append("2")
232 # ul.open("li").append("3").append("4")
233 # assert ul.write_to_string == "<ul><li>12</li><li>34</li></ul>"
234 fun open
(tag
: String): HTMLTag
236 var res
= new HTMLTag(tag
)
241 # List of children HTML elements
242 var children
: Set[HTMLTag] = new HashSet[HTMLTag]
244 # Clear all child and set the text of element
246 # var p = new HTMLTag("p")
247 # p.text("Hello World!")
248 # assert p.write_to_string == "<p>Hello World!</p>"
249 # Text is escaped see: `core::String::html_escape`
250 fun text
(txt
: String): HTMLTag do
257 # Append text to element
259 # var p = new HTMLTag("p")
261 # p.add(new HTMLTag("br"))
263 # assert p.write_to_string == "<p>Hello<br/>World!</p>"
264 # Text is escaped see: core::String::html_escape
265 fun append
(txt
: String): HTMLTag do
266 add
(new HTMLRaw("", txt
.html_escape
))
270 # Append raw HTML to element
272 # var p = new HTMLTag("p")
274 # p.add_raw_html("<bla/>foo")
275 # assert p.write_to_string == "<p>Hello<bla/>foo</p>"
277 # Note: the HTML in insered as it, no verification is done.
278 fun add_raw_html
(txt
: String): HTMLTag do
279 add
(new HTMLRaw("", txt
))
283 redef fun write_to
(stream
) do
284 var res
= new Array[String]
291 # In order to avoid recursive concatenation,
292 # this function collects in `res` all the small fragments of `String`
293 private fun render_in
(res
: Sequence[String])
298 if is_void
and children
.is_empty
then
302 for child
in children
do child
.render_in
(res
)
309 private fun render_attrs_in
(res
: Sequence[String]) do
310 if attrs
.has_key
("class") or not classes
.is_empty
then
312 for cls in classes do
313 res.add cls.html_escape
316 if attrs.has_key("class") then
317 res.add attrs["class"].html_escape
320 if res.last == " " then res.pop
324 if attrs
.has_key
("style") or not css_props
.is_empty
then
326 for k, v in css_props do
327 res.add k.html_escape
329 res.add v.html_escape
332 if attrs.has_key("style
") then
333 res.add(attrs["style
"].html_escape)
335 if res.last == "; " then res.pop
339 if attrs
.is_empty
then return
341 for key
, value
in attrs
do
342 if key
== "class" or key
== "style" then continue
344 res
.add key
.html_escape
346 res.add value.html_escape
352 private class HTMLRaw
356 redef fun render_in
(res
) do res
.add content