Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 2 | // All rights reserved. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 5 | // modification, are permitted provided that the following conditions are met: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 8 | // list of conditions and the following disclaimer. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 12 | // and/or other materials provided with the distribution. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 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 |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 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 | |
| 36 | namespace seasocks { |
| 37 | |
| 38 | namespace html { |
| 39 | |
| 40 | class 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 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 50 | template <typename T, typename... Args> |
| 51 | void append(const T& t, Args&&... args) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 52 | operator<<(t); |
| 53 | append(std::forward<Args>(args)...); |
| 54 | } |
| 55 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 56 | Element() |
| 57 | : _isTextNode(true), _needsClose(false) { |
| 58 | } |
| 59 | |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 60 | public: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 61 | template <typename... Args> |
| 62 | Element(const std::string& name, bool needsClose, Args&&... args) |
| 63 | : _isTextNode(false), |
| 64 | _needsClose(needsClose), |
| 65 | _nameOrText(name) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 66 | append(std::forward<Args>(args)...); |
| 67 | } |
| 68 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 69 | template <typename T> |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 70 | static Element textElement(const T& text) { |
| 71 | Element e; |
| 72 | e._nameOrText = toString(text); |
| 73 | return e; |
| 74 | } |
| 75 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 76 | template <typename T> |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 77 | Element& addAttribute(const char* attr, const T& value) { |
| 78 | _attributes += std::string(" ") + attr + "=\"" + toString(value) + "\""; |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | Element& clazz(const std::string& clazz) { |
| 83 | return addAttribute("class", clazz); |
| 84 | } |
| 85 | |
| 86 | Element& title(const std::string& title) { |
| 87 | return addAttribute("title", title); |
| 88 | } |
| 89 | |
| 90 | Element& style(const std::string& style) { |
| 91 | return addAttribute("style", style); |
| 92 | } |
| 93 | |
| 94 | Element& alt(const std::string& alt) { |
| 95 | return addAttribute("alt", alt); |
| 96 | } |
| 97 | |
| 98 | Element& hidden() { |
| 99 | return addAttribute("style", "display:none;"); |
| 100 | } |
| 101 | |
| 102 | Element& id(const std::string& id) { |
| 103 | return addAttribute("id", id); |
| 104 | } |
| 105 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 106 | Element& operator<<(const char* child) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 107 | _children.push_back(textElement(child)); |
| 108 | return *this; |
| 109 | } |
| 110 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 111 | Element& operator<<(const std::string& child) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 112 | _children.push_back(textElement(child)); |
| 113 | return *this; |
| 114 | } |
| 115 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 116 | Element& operator<<(const Element& child) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 117 | _children.push_back(child); |
| 118 | return *this; |
| 119 | } |
| 120 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 121 | template <class T> |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 122 | typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, Element&>::type |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 123 | operator<<(const T& child) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 124 | _children.push_back(textElement(child)); |
| 125 | return *this; |
| 126 | } |
| 127 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 128 | friend std::ostream& operator<<(std::ostream& os, const Element& elem) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 129 | if (elem._isTextNode) { |
| 130 | os << elem._nameOrText; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 131 | for (const auto& it : elem._children) { |
| 132 | os << it; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 133 | } |
| 134 | return os; |
| 135 | } |
| 136 | os << "<" << elem._nameOrText << elem._attributes << ">"; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 137 | for (const auto& it : elem._children) { |
| 138 | os << it; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 139 | } |
| 140 | if (elem._needsClose) { |
| 141 | os << "</" << elem._nameOrText << ">"; |
| 142 | } |
| 143 | return os; |
| 144 | } |
| 145 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 146 | template <typename C, typename F> |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 147 | Element& addAll(const C& container, F functor) { |
| 148 | for (auto it = container.cbegin(); it != container.cend(); ++it) { |
| 149 | operator<<(functor(*it)); |
| 150 | } |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | std::string str() const { |
| 155 | std::ostringstream os; |
| 156 | os << *this; |
| 157 | return os.str(); |
| 158 | } |
| 159 | }; |
| 160 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 161 | #define HTMLELEM(XX) \ |
| 162 | template <typename... Args> \ |
| 163 | inline Element XX(Args&&... args) { \ |
| 164 | return Element(#XX, true, std::forward<Args>(args)...); \ |
| 165 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 166 | HTMLELEM(html) |
| 167 | HTMLELEM(head) |
| 168 | HTMLELEM(title) |
| 169 | HTMLELEM(body) |
| 170 | HTMLELEM(h1) |
| 171 | HTMLELEM(h2) |
| 172 | HTMLELEM(h3) |
| 173 | HTMLELEM(h4) |
| 174 | HTMLELEM(h5) |
| 175 | HTMLELEM(table) |
| 176 | HTMLELEM(thead) |
| 177 | HTMLELEM(tbody) |
| 178 | HTMLELEM(tr) |
| 179 | HTMLELEM(td) |
| 180 | HTMLELEM(th) |
| 181 | HTMLELEM(div) |
| 182 | HTMLELEM(span) |
| 183 | HTMLELEM(ul) |
| 184 | HTMLELEM(ol) |
| 185 | HTMLELEM(li) |
| 186 | HTMLELEM(label) |
| 187 | HTMLELEM(button) |
| 188 | #undef HTMLELEM |
| 189 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 190 | inline Element empty() { |
| 191 | return Element::textElement(""); |
| 192 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 193 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 194 | template <typename T> |
| 195 | inline Element text(const T& t) { |
| 196 | return Element::textElement(t); |
| 197 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 198 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 199 | inline Element img(const std::string& src) { |
| 200 | return Element("img", false).addAttribute("src", src); |
| 201 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 202 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 203 | inline Element checkbox() { |
| 204 | return Element("input", false).addAttribute("type", "checkbox"); |
| 205 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 206 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 207 | inline Element externalScript(const std::string& src) { |
| 208 | return Element("script", true).addAttribute("src", src).addAttribute("type", "text/javascript"); |
| 209 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 210 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 211 | inline Element inlineScript(const std::string& script) { |
| 212 | return Element("script", true, script).addAttribute("type", "text/javascript"); |
| 213 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 214 | |
| 215 | inline Element link(const std::string& href, const std::string& rel) { |
| 216 | return Element("link", false).addAttribute("href", href).addAttribute("rel", rel); |
| 217 | } |
| 218 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 219 | template <typename... Args> |
| 220 | inline Element a(const std::string& href, Args&&... args) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 221 | return Element("a", true, std::forward<Args>(args)...).addAttribute("href", href); |
| 222 | } |
| 223 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 224 | } // namespace html |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 225 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 226 | } // namespace seasocks |