Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
html_writer.h
1#ifndef SOURCEMETA_CORE_HTML_WRITER_H_
2#define SOURCEMETA_CORE_HTML_WRITER_H_
3
4#ifndef SOURCEMETA_CORE_HTML_EXPORT
5#include <sourcemeta/core/html_export.h>
6#endif
7
8#include <sourcemeta/core/html_buffer.h>
9#include <sourcemeta/core/html_escape.h>
10#include <sourcemeta/core/preprocessor.h>
11
12#include <array> // std::array
13#include <cassert> // assert
14#include <cstddef> // std::size_t
15#include <cstdint> // std::uint8_t
16#include <string> // std::string
17#include <string_view> // std::string_view
18#include <vector> // std::vector
19
20namespace sourcemeta::core {
21
37class SOURCEMETA_CORE_HTML_EXPORT HTMLWriter {
38public:
40 SOURCEMETA_FORCEINLINE auto reserve(std::size_t bytes) -> void {
41 this->buffer_.reserve(bytes);
42 }
43
46 SOURCEMETA_FORCEINLINE auto close() -> HTMLWriter & {
47 this->tag_open_ = false;
48 if (this->tag_stack_.empty()) [[unlikely]] {
49 return *this;
50 }
51 const auto &closing{this->tag_stack_.back()};
52 // Copying every byte of the fixed storage is cheaper than copying a
53 // variable number of them, and the bytes past the tag are then dropped
54 this->buffer_.reserve_additional(closing.bytes.size());
55 this->buffer_.append_unchecked(
56 std::string_view{closing.bytes.data(), closing.bytes.size()});
57 this->buffer_.remove_suffix(closing.bytes.size() - closing.size);
58 this->tag_stack_.pop_back();
59 return *this;
60 }
61
64 SOURCEMETA_FORCEINLINE auto attribute(std::string_view name,
65 std::string_view value)
66 -> HTMLWriter & {
67 assert(this->tag_open_);
68 this->reopen_tag();
69 // The space before the name, the equals sign and the quotation mark after
70 // it, along with the end of the tag, which takes four bytes for a void
71 // element
72 this->buffer_.reserve_additional(name.size() + 7);
73 this->buffer_.append_unchecked(" ");
74 this->buffer_.append_unchecked(name);
75 this->buffer_.append_unchecked("=\"");
76 html_escape_append(this->buffer_, value);
77 this->buffer_.reserve_additional(4);
78 if (this->tag_open_is_void_) {
79 this->buffer_.append_unchecked("\" />");
80 } else {
81 this->buffer_.append_unchecked("\">");
82 }
83 return *this;
84 }
85
98 SOURCEMETA_FORCEINLINE auto attribute(std::string_view name) -> HTMLWriter & {
99 assert(this->tag_open_);
100 this->reopen_tag();
101 this->buffer_.reserve_additional(name.size() + 1);
102 this->buffer_.append_unchecked(" ");
103 this->buffer_.append_unchecked(name);
104 this->end_open_tag();
105 return *this;
106 }
107
123 SOURCEMETA_FORCEINLINE auto ensure_line_feed() -> HTMLWriter & {
124 this->tag_open_ = false;
125 if (this->buffer_.size() > 0 && this->buffer_.back() != '\n') {
126 this->buffer_.append('\n');
127 }
128
129 return *this;
130 }
131
145 [[nodiscard]] SOURCEMETA_FORCEINLINE auto take() -> std::string {
146 this->tag_open_ = false;
147 this->tag_stack_.clear();
148 return this->buffer_.take();
149 }
150
162 SOURCEMETA_FORCEINLINE auto clear() -> void {
163 this->tag_open_ = false;
164 this->tag_stack_.clear();
165 this->buffer_.clear();
166 }
167
174 SOURCEMETA_FORCEINLINE auto text(std::string_view content) -> HTMLWriter & {
175 this->tag_open_ = false;
176 html_escape_append(this->buffer_, content);
177 return *this;
178 }
179
182 SOURCEMETA_FORCEINLINE auto raw(std::string_view content) -> HTMLWriter & {
183 this->tag_open_ = false;
184 this->buffer_.append(content);
185 return *this;
186 }
187
189 [[nodiscard]] SOURCEMETA_FORCEINLINE auto str() -> const std::string & {
190 return this->buffer_.str();
191 }
192
194 auto write(std::ostream &stream) -> void;
195
196// Exporting symbols that depends on the standard C++ library is considered
197// safe.
198// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
199#if defined(_MSC_VER)
200#pragma warning(push)
201#pragma warning(disable : 4251)
202#endif
203
204#ifndef DOXYGEN
205// Macro to generate container element methods.
206// Container elements write <tag> on open and </tag> on close().
207// Overloads:
208// .tag() open with no attributes
209// .tag(text) open, write escaped text, close (shorthand)
210// NOLINTBEGIN(bugprone-macro-parentheses)
211#define HTML_WRITER_CONTAINER(name) \
212 SOURCEMETA_FORCEINLINE auto name() -> HTMLWriter & { \
213 this->open_tag("<" #name ">", ClosingTag{"</" #name ">"}); \
214 return *this; \
215 } \
216 SOURCEMETA_FORCEINLINE auto name(std::string_view text_content) \
217 -> HTMLWriter & { \
218 this->open_tag("<" #name ">", ClosingTag{"</" #name ">"}); \
219 this->text(text_content); \
220 this->close(); \
221 return *this; \
222 }
223// NOLINTEND(bugprone-macro-parentheses)
224
225// Same as above but with a different C++ method name than the HTML tag
226// NOLINTBEGIN(bugprone-macro-parentheses)
227#define HTML_WRITER_CONTAINER_NAMED(name, tag) \
228 SOURCEMETA_FORCEINLINE auto name() -> HTMLWriter & { \
229 this->open_tag("<" #tag ">", ClosingTag{"</" #tag ">"}); \
230 return *this; \
231 } \
232 SOURCEMETA_FORCEINLINE auto name(std::string_view text_content) \
233 -> HTMLWriter & { \
234 this->open_tag("<" #tag ">", ClosingTag{"</" #tag ">"}); \
235 this->text(text_content); \
236 this->close(); \
237 return *this; \
238 }
239// NOLINTEND(bugprone-macro-parentheses)
240
241// Macro to generate void element methods.
242// Void elements are self-closing: <tag /> or <tag attr="val" />
243// NOLINTBEGIN(bugprone-macro-parentheses)
244#define HTML_WRITER_VOID(name) \
245 SOURCEMETA_FORCEINLINE auto name() -> HTMLWriter & { \
246 this->void_tag("<" #name " />"); \
247 return *this; \
248 }
249// NOLINTEND(bugprone-macro-parentheses)
250#endif
251
252 // =========================================================================
253 // Document Structure Elements
254 // =========================================================================
255
257 HTML_WRITER_CONTAINER(html)
259 HTML_WRITER_VOID(base)
261 HTML_WRITER_CONTAINER(head)
263 HTML_WRITER_VOID(link)
265 HTML_WRITER_VOID(meta)
267 HTML_WRITER_CONTAINER(style)
269 HTML_WRITER_CONTAINER(title)
271 HTML_WRITER_CONTAINER(body)
272
273 // =========================================================================
274 // Content Sectioning Elements
275 // =========================================================================
276
277
278 HTML_WRITER_CONTAINER(address)
280 HTML_WRITER_CONTAINER(article)
282 HTML_WRITER_CONTAINER(aside)
284 HTML_WRITER_CONTAINER(footer)
286 HTML_WRITER_CONTAINER(header)
288 HTML_WRITER_CONTAINER(h1)
290 HTML_WRITER_CONTAINER(h2)
292 HTML_WRITER_CONTAINER(h3)
294 HTML_WRITER_CONTAINER(h4)
296 HTML_WRITER_CONTAINER(h5)
298 HTML_WRITER_CONTAINER(h6)
300 HTML_WRITER_CONTAINER(hgroup)
302 HTML_WRITER_CONTAINER(main)
304 HTML_WRITER_CONTAINER(nav)
306 HTML_WRITER_CONTAINER(section)
308 HTML_WRITER_CONTAINER(search)
309
310 // =========================================================================
311 // Text Content Elements
312 // =========================================================================
313
314
315 HTML_WRITER_CONTAINER(blockquote)
317 HTML_WRITER_CONTAINER(dd)
319 HTML_WRITER_CONTAINER(div)
321 HTML_WRITER_CONTAINER(dl)
323 HTML_WRITER_CONTAINER(dt)
325 HTML_WRITER_CONTAINER(figcaption)
327 HTML_WRITER_CONTAINER(figure)
329 HTML_WRITER_VOID(hr)
331 HTML_WRITER_CONTAINER(li)
333 HTML_WRITER_CONTAINER(menu)
335 HTML_WRITER_CONTAINER(ol)
337 HTML_WRITER_CONTAINER(p)
339 HTML_WRITER_CONTAINER(pre)
341 HTML_WRITER_CONTAINER(ul)
342
343 // =========================================================================
344 // Inline Text Semantics Elements
345 // =========================================================================
346
347
348 HTML_WRITER_CONTAINER(a)
350 HTML_WRITER_CONTAINER(abbr)
352 HTML_WRITER_CONTAINER(b)
354 HTML_WRITER_CONTAINER(bdi)
356 HTML_WRITER_CONTAINER(bdo)
358 HTML_WRITER_VOID(br)
360 HTML_WRITER_CONTAINER(cite)
362 HTML_WRITER_CONTAINER(code)
364 HTML_WRITER_CONTAINER(data)
366 HTML_WRITER_CONTAINER(dfn)
368 HTML_WRITER_CONTAINER(em)
370 HTML_WRITER_CONTAINER(i)
372 HTML_WRITER_CONTAINER(kbd)
374 HTML_WRITER_CONTAINER(mark)
376 HTML_WRITER_CONTAINER(q)
378 HTML_WRITER_CONTAINER(rp)
380 HTML_WRITER_CONTAINER(rt)
382 HTML_WRITER_CONTAINER(ruby)
384 HTML_WRITER_CONTAINER(s)
386 HTML_WRITER_CONTAINER(samp)
388 HTML_WRITER_CONTAINER(small)
390 HTML_WRITER_CONTAINER(span)
392 HTML_WRITER_CONTAINER(strong)
394 HTML_WRITER_CONTAINER(sub)
396 HTML_WRITER_CONTAINER(sup)
398 HTML_WRITER_CONTAINER(time)
400 HTML_WRITER_CONTAINER(u)
402 HTML_WRITER_CONTAINER(var)
404 HTML_WRITER_VOID(wbr)
405
406 // =========================================================================
407 // Image and Multimedia Elements
408 // =========================================================================
409
410
411 HTML_WRITER_VOID(area)
413 HTML_WRITER_CONTAINER(audio)
415 HTML_WRITER_VOID(img)
417 HTML_WRITER_CONTAINER(map)
419 HTML_WRITER_VOID(track)
421 HTML_WRITER_CONTAINER(video)
422
423 // =========================================================================
424 // Embedded Content Elements
425 // =========================================================================
426
427
428 HTML_WRITER_VOID(embed)
430 HTML_WRITER_CONTAINER(iframe)
432 HTML_WRITER_CONTAINER(object)
434 HTML_WRITER_CONTAINER(picture)
436 HTML_WRITER_CONTAINER(portal)
438 HTML_WRITER_VOID(source)
439
440 // =========================================================================
441 // Scripting Elements
442 // =========================================================================
443
444
445 HTML_WRITER_CONTAINER(canvas)
447 HTML_WRITER_CONTAINER(noscript)
449 HTML_WRITER_CONTAINER(script)
450
451 // =========================================================================
452 // Demarcating Edits Elements
453 // =========================================================================
454
455
456 HTML_WRITER_CONTAINER(del)
458 HTML_WRITER_CONTAINER(ins)
459
460 // =========================================================================
461 // Table Content Elements
462 // =========================================================================
463
464
465 HTML_WRITER_CONTAINER(caption)
467 HTML_WRITER_VOID(col)
469 HTML_WRITER_CONTAINER(colgroup)
471 HTML_WRITER_CONTAINER(table)
473 HTML_WRITER_CONTAINER(tbody)
475 HTML_WRITER_CONTAINER(td)
477 HTML_WRITER_CONTAINER(tfoot)
479 HTML_WRITER_CONTAINER(th)
481 HTML_WRITER_CONTAINER(thead)
483 HTML_WRITER_CONTAINER(tr)
484
485 // =========================================================================
486 // Forms Elements
487 // =========================================================================
488
489
490 HTML_WRITER_CONTAINER(button)
492 HTML_WRITER_CONTAINER(datalist)
494 HTML_WRITER_CONTAINER(fieldset)
496 HTML_WRITER_CONTAINER(form)
498 HTML_WRITER_VOID(input)
500 HTML_WRITER_CONTAINER(label)
502 HTML_WRITER_CONTAINER(legend)
504 HTML_WRITER_CONTAINER(meter)
506 HTML_WRITER_CONTAINER(optgroup)
508 HTML_WRITER_CONTAINER(option)
510 HTML_WRITER_CONTAINER(output)
512 HTML_WRITER_CONTAINER(progress)
514 HTML_WRITER_CONTAINER(select)
516 HTML_WRITER_CONTAINER(textarea)
517
518 // =========================================================================
519 // Interactive Elements
520 // =========================================================================
521
522
523 HTML_WRITER_CONTAINER(details)
525 HTML_WRITER_CONTAINER(dialog)
527 HTML_WRITER_CONTAINER(summary)
528
529 // =========================================================================
530 // Web Components Elements
531 // =========================================================================
532
533
534 HTML_WRITER_CONTAINER(slot)
536 // The trailing underscore keeps this element name from colliding with the
537 // template keyword
538 // NOLINTNEXTLINE(readability-identifier-naming)
539 HTML_WRITER_CONTAINER_NAMED(template_, template)
540
541#ifndef DOXYGEN
542#undef HTML_WRITER_CONTAINER
543#undef HTML_WRITER_CONTAINER_NAMED
544#undef HTML_WRITER_VOID
545#endif
546
547private:
548 // A closing tag kept in a fixed number of bytes, as copying a constant
549 // number of bytes is cheaper than copying a variable one
550 struct ClosingTag {
551 constexpr explicit ClosingTag(const std::string_view tag) noexcept
552 : size{static_cast<std::uint8_t>(tag.size())} {
553 for (std::size_t index = 0; index < tag.size(); index += 1) {
554 this->bytes[index] = tag[index];
555 }
556 }
557
558 std::array<char, 16> bytes{};
559 std::uint8_t size;
560 };
561
562 SOURCEMETA_FORCEINLINE auto open_tag(std::string_view opening,
563 const ClosingTag &closing) -> void {
564 this->buffer_.append(opening);
565 this->tag_stack_.push_back(closing);
566 this->tag_open_ = true;
567 this->tag_open_is_void_ = false;
568 }
569
570 SOURCEMETA_FORCEINLINE auto void_tag(std::string_view opening) -> void {
571 this->buffer_.append(opening);
572 this->tag_open_ = true;
573 this->tag_open_is_void_ = true;
574 }
575
576 // Remove the characters that end the tag opened last, so that attributes
577 // can go before them
578 SOURCEMETA_FORCEINLINE auto reopen_tag() -> void {
579 this->buffer_.remove_suffix(this->tag_open_is_void_ ? 3 : 1);
580 }
581
582 SOURCEMETA_FORCEINLINE auto end_open_tag() -> void {
583 if (this->tag_open_is_void_) {
584 this->buffer_.append(" />");
585 } else {
586 this->buffer_.append('>');
587 }
588 }
589
590 HTMLBuffer buffer_;
591 std::vector<ClosingTag> tag_stack_;
592 bool tag_open_{false};
593 bool tag_open_is_void_{false};
594#if defined(_MSC_VER)
595#pragma warning(pop)
596#endif
597};
598
599} // namespace sourcemeta::core
600
601#endif
SOURCEMETA_FORCEINLINE auto str() -> const std::string &
Get the rendered HTML string.
Definition html_writer.h:189
SOURCEMETA_FORCEINLINE auto close() -> HTMLWriter &
Definition html_writer.h:46
SOURCEMETA_FORCEINLINE auto attribute(std::string_view name) -> HTMLWriter &
Definition html_writer.h:98
auto write(std::ostream &stream) -> void
Write the rendered HTML to an output stream.
SOURCEMETA_FORCEINLINE auto reserve(std::size_t bytes) -> void
Pre-allocate the output buffer.
Definition html_writer.h:40
SOURCEMETA_FORCEINLINE auto clear() -> void
Definition html_writer.h:162
SOURCEMETA_FORCEINLINE auto attribute(std::string_view name, std::string_view value) -> HTMLWriter &
Definition html_writer.h:64
SOURCEMETA_FORCEINLINE auto text(std::string_view content) -> HTMLWriter &
Definition html_writer.h:174
SOURCEMETA_FORCEINLINE auto take() -> std::string
Definition html_writer.h:145
SOURCEMETA_FORCEINLINE auto ensure_line_feed() -> HTMLWriter &
Definition html_writer.h:123
SOURCEMETA_FORCEINLINE auto raw(std::string_view content) -> HTMLWriter &
Definition html_writer.h:182
Definition html_writer.h:37
auto html_escape_append(Output &output, const std::string_view input) -> void
Definition html_escape.h:58
SOURCEMETA_CORE_UNICODE_EXPORT auto script(const char32_t codepoint) noexcept -> UnicodeScript