blob: 6b3e887a57f3cb8af39a9bb3d156dc12a7bae097 [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#pragma once
27
28#include "seasocks/ToString.h"
29
30#include <functional>
31#include <iostream>
32#include <list>
33#include <sstream>
34#include <string>
35
36namespace seasocks {
37
38namespace html {
39
40class Element {
41 bool _isTextNode;
42 bool _needsClose;
43 std::string _nameOrText;
44 std::string _attributes;
45 std::list<Element> _children;
46
47 void append() {
48 }
49
50 template<typename T, typename... Args>
51 void append(const T& t, Args&& ...args) {
52 operator<<(t);
53 append(std::forward<Args>(args)...);
54 }
55
56 Element() : _isTextNode(true), _needsClose(false) {}
57public:
58 template<typename... Args>
59 Element(const std::string& name, bool needsClose, Args&&... args) :
60 _isTextNode(false),
61 _needsClose(needsClose),
62 _nameOrText(name) {
63 append(std::forward<Args>(args)...);
64 }
65
66 template<typename T>
67 static Element textElement(const T& text) {
68 Element e;
69 e._nameOrText = toString(text);
70 return e;
71 }
72
73 template<typename T>
74 Element& addAttribute(const char* attr, const T& value) {
75 _attributes += std::string(" ") + attr + "=\"" + toString(value) + "\"";
76 return *this;
77 }
78
79 Element& clazz(const std::string& clazz) {
80 return addAttribute("class", clazz);
81 }
82
83 Element& title(const std::string& title) {
84 return addAttribute("title", title);
85 }
86
87 Element& style(const std::string& style) {
88 return addAttribute("style", style);
89 }
90
91 Element& alt(const std::string& alt) {
92 return addAttribute("alt", alt);
93 }
94
95 Element& hidden() {
96 return addAttribute("style", "display:none;");
97 }
98
99 Element& id(const std::string& id) {
100 return addAttribute("id", id);
101 }
102
103 Element& operator <<(const char* child) {
104 _children.push_back(textElement(child));
105 return *this;
106 }
107
108 Element& operator <<(const std::string& child) {
109 _children.push_back(textElement(child));
110 return *this;
111 }
112
113 Element& operator <<(const Element& child) {
114 _children.push_back(child);
115 return *this;
116 }
117
118 template<class T>
119 typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, Element&>::type
120 operator <<(const T& child) {
121 _children.push_back(textElement(child));
122 return *this;
123 }
124
125 friend std::ostream& operator << (std::ostream& os, const Element& elem) {
126 if (elem._isTextNode) {
127 os << elem._nameOrText;
128 for (auto it = elem._children.cbegin(); it != elem._children.cend(); ++it) {
129 os << *it;
130 }
131 return os;
132 }
133 os << "<" << elem._nameOrText << elem._attributes << ">";
134 for (auto it = elem._children.cbegin(); it != elem._children.cend(); ++it) {
135 os << *it;
136 }
137 if (elem._needsClose) {
138 os << "</" << elem._nameOrText << ">";
139 }
140 return os;
141 }
142
143 template<typename C, typename F>
144 Element& addAll(const C& container, F functor) {
145 for (auto it = container.cbegin(); it != container.cend(); ++it) {
146 operator<<(functor(*it));
147 }
148 return *this;
149 }
150
151 std::string str() const {
152 std::ostringstream os;
153 os << *this;
154 return os.str();
155 }
156};
157
158#define HTMLELEM(XX) template<typename... Args> inline Element XX(Args&&... args) { return Element(#XX, true, std::forward<Args>(args)...); }
159HTMLELEM(html)
160HTMLELEM(head)
161HTMLELEM(title)
162HTMLELEM(body)
163HTMLELEM(h1)
164HTMLELEM(h2)
165HTMLELEM(h3)
166HTMLELEM(h4)
167HTMLELEM(h5)
168HTMLELEM(table)
169HTMLELEM(thead)
170HTMLELEM(tbody)
171HTMLELEM(tr)
172HTMLELEM(td)
173HTMLELEM(th)
174HTMLELEM(div)
175HTMLELEM(span)
176HTMLELEM(ul)
177HTMLELEM(ol)
178HTMLELEM(li)
179HTMLELEM(label)
180HTMLELEM(button)
181#undef HTMLELEM
182
183inline Element empty() { return Element::textElement(""); }
184
185template<typename T>
186inline Element text(const T& t) { return Element::textElement(t); }
187
188inline Element img(const std::string& src) { return Element("img", false).addAttribute("src", src); }
189
190inline Element checkbox() { return Element("input", false).addAttribute("type", "checkbox"); }
191
192inline Element externalScript(const std::string& src) { return Element("script", true).addAttribute("src", src).addAttribute("type", "text/javascript"); }
193
194inline Element inlineScript(const std::string& script) { return Element("script", true, script).addAttribute("type", "text/javascript"); }
195
196inline Element link(const std::string& href, const std::string& rel) {
197 return Element("link", false).addAttribute("href", href).addAttribute("rel", rel);
198}
199
200template<typename...Args> inline Element a(const std::string& href, Args&&... args) {
201 return Element("a", true, std::forward<Args>(args)...).addAttribute("href", href);
202}
203
204} // namespace html
205
206} // namespace seasocks