A growing implementation of HTML utilities per the HTML Living Standard. More...
Classes | |
| class | sourcemeta::core::HTMLBuffer |
| class | sourcemeta::core::HTMLWriter |
Functions | |
| SOURCEMETA_CORE_HTML_EXPORT auto | sourcemeta::core::html_entity (const std::string_view name) noexcept -> std::string_view |
| SOURCEMETA_CORE_HTML_EXPORT auto | sourcemeta::core::html_escape (std::string &text) -> void |
| template<typename Output> | |
| auto | sourcemeta::core::html_escape_append (Output &output, const std::string_view input) -> void |
A growing implementation of HTML utilities per the HTML Living Standard.
This functionality is included as follows:
| class sourcemeta::core::HTMLBuffer |
A fast append-only string buffer
Public Member Functions | |
| HTMLBuffer (const HTMLBuffer &)=delete | |
| HTMLBuffer (HTMLBuffer &&)=delete | |
| SOURCEMETA_FORCEINLINE auto | reserve (const std::size_t bytes) -> void |
| SOURCEMETA_FORCEINLINE auto | append (const char character) -> void |
| Append a single character to the buffer. | |
| SOURCEMETA_FORCEINLINE auto | append (const std::string_view data) -> void |
| Append a sequence of characters to the buffer. | |
| SOURCEMETA_FORCEINLINE auto | reserve_additional (const std::size_t bytes) -> void |
| SOURCEMETA_FORCEINLINE auto | append_unchecked (const std::string_view data) noexcept -> void |
| SOURCEMETA_FORCEINLINE auto | size () const noexcept -> std::size_t |
| Get the number of bytes accumulated so far. | |
| SOURCEMETA_FORCEINLINE auto | back () const noexcept -> char |
| Get the last character of the buffer, which must not be empty. | |
| SOURCEMETA_FORCEINLINE auto | remove_suffix (const std::size_t count) noexcept -> void |
| SOURCEMETA_FORCEINLINE auto | view () const noexcept -> std::string_view |
| SOURCEMETA_FORCEINLINE auto | clear () noexcept -> void |
| Discard the accumulated contents, keeping the capacity for reuse. | |
| SOURCEMETA_FORCEINLINE auto | str () -> const std::string & |
| Get the accumulated contents of the buffer. | |
| SOURCEMETA_FORCEINLINE auto | take () -> std::string |
| auto | write (std::ostream &stream) -> void |
| Write the accumulated contents to an output stream. | |
|
inlinenoexcept |
Append a sequence of characters whose capacity was already reserved through reserve_additional
|
inlinenoexcept |
Remove a number of bytes from the end of the buffer, which must hold at least that many bytes
|
inline |
Reserve capacity for at least a number of bytes in total up front, keeping the accumulated contents
|
inline |
Reserve capacity for at least a number of bytes on top of the accumulated contents, so that appending up to that many bytes through append_unchecked needs no further capacity checks
|
inlinenodiscard |
Move the accumulated contents out of the buffer, leaving it empty and ready to accumulate new contents
|
inlinenodiscardnoexcept |
Get a view of the accumulated contents, which stays valid until the buffer is modified
| class sourcemeta::core::HTMLWriter |
A streaming HTML writer that renders directly to a string buffer. No intermediate DOM tree is built. Elements are serialized as methods are called.
Public Member Functions | |
| SOURCEMETA_FORCEINLINE auto | reserve (std::size_t bytes) -> void |
| Pre-allocate the output buffer. | |
| SOURCEMETA_FORCEINLINE auto | close () -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | attribute (std::string_view name, std::string_view value) -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | attribute (std::string_view name) -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | ensure_line_feed () -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | take () -> std::string |
| SOURCEMETA_FORCEINLINE auto | clear () -> void |
| SOURCEMETA_FORCEINLINE auto | text (std::string_view content) -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | raw (std::string_view content) -> HTMLWriter & |
| SOURCEMETA_FORCEINLINE auto | str () -> const std::string & |
| Get the rendered HTML string. | |
| auto | write (std::ostream &stream) -> void |
| Write the rendered HTML to an output stream. | |
|
inline |
Add an attribute without a value, such as a boolean attribute, to the currently open tag. Must be called immediately after an element method and before any content. For example:
|
inline |
Add an attribute to the currently open tag. Must be called immediately after an element method and before any content.
|
inline |
Discard the output and every open element. For example:
|
inline |
Close the most recently opened element. Closing when no element is open has no effect.
|
inline |
Write a line feed unless nothing was written yet or the output already ends with one, so that the markup that follows starts on a line of its own. For example:
|
inline |
Write content without HTML-escaping. This is how the content of a raw-text element is emitted, since escaped text would corrupt it.
|
inlinenodiscard |
Move the rendered HTML string out of the writer, leaving the writer empty and ready to render another document. For example:
|
inline |
Write HTML-escaped text content. The single-argument element shorthand routes through this and therefore also escapes. The HTML serialization emits the content of a raw-text element literally, so escaping its content would corrupt it. This writer does not special-case content by element, so the content of a raw-text element must be written unescaped rather than as escaped text, and it must not contain that element's closing-tag sequence.
|
noexcept |
Look up the characters of a named character reference per the HTML Living Standard, which lists every one of them at https://html.spec.whatwg.org/entities.json. The name excludes the leading ampersand and includes the trailing semicolon, which the standard lets a few legacy names omit. The result is the UTF-8 encoding of the characters, or an empty view if the name is not a named character reference. For example:
| SOURCEMETA_CORE_HTML_EXPORT auto sourcemeta::core::html_escape | ( | std::string & | text | ) | -> void |
HTML character escaping implementation per HTML Living Standard. See: https://html.spec.whatwg.org/multipage/parsing.html#escapingString
This function escapes the five HTML special characters in-place, the ampersand, less-than sign, greater-than sign, double quote, and apostrophe, along with the no-break space, each becoming its corresponding HTML entity.
For example:
|
inline |
Append the HTML-escaped form of input to a string or to a buffer, without allocating a temporary string. The input must not reference the output, since appending to the output may relocate its storage. For example: