Skip to main content

flm_core/stdlib/backends/html/
markup.rs

1//! The markup value: a string of HTML that the HTML sink writes without
2//! escaping it.
3//!
4//! Everything else the HTML backend writes is escaped where it is written
5//! ([`super::escape`]), so a document cannot put a `<` into the output. A
6//! [`Markup`] value is one of two exceptions; the other is the content of a
7//! `<style>` or a `<script>`, which
8//! [`HtmlSink::raw_text_element`](super::HtmlSink::raw_text_element) writes.
9//! `Markup` is a type of its own so that the exception is easy to find: a
10//! reader looking for every place where markup enters an HTML page unescaped
11//! looks for the two constructors of [`Markup`].
12
13use std::fmt;
14
15/// A string of HTML that the [sink](super::HtmlSink) writes unchanged, without
16/// escaping.
17///
18/// **What it is for.** The HTML this library writes is escaped at the point of
19/// writing: document text goes through [`HtmlSink::text`](super::HtmlSink::text)
20/// and an attribute value through
21/// [`TagBuilder::attr`](super::TagBuilder::attr), each of which rewrites the
22/// characters that would otherwise be read as markup. A `Markup` value carries
23/// bytes that are *already* HTML and must not be rewritten — the output of a
24/// tool that produces markup, such as a formula typeset as inline
25/// `<svg>` — and [`HtmlSink::markup`](super::HtmlSink::markup) writes them
26/// through unchanged.
27///
28/// **What its creator must ensure.** Whoever calls [`trusted`](Self::trusted)
29/// accepts the requirement that the string is well-formed HTML that is safe to
30/// place in the page: it closes every element it opens, and it holds no
31/// script, no event handler attribute and no address a browser would execute,
32/// unless the caller put them there intentionally and is responsible for them.
33/// The library checks nothing about the string. If the bytes come from a
34/// document, from a network resource or from any other input the caller does
35/// not control, the caller sanitizes them first; this library's own features
36/// never pass document text to this constructor.
37///
38/// Outside this crate, unescaped bytes reach an HTML page in two ways only:
39/// through `trusted`, and through
40/// [`HtmlSink::raw_text_element`](super::HtmlSink::raw_text_element), which
41/// writes the content of a `<style>` or a `<script>` and refuses content that
42/// holds that element's closing tag. A review of what an extension can put into
43/// a page is therefore a review of its calls to these two.
44///
45/// There is no other constructor from a string (the crate-private
46/// `from_library` serves the library's own code), and the text inside is
47/// private:
48///
49/// ```compile_fail
50/// use flm_core::stdlib::backends::html::Markup;
51///
52/// let markup = Markup(String::from("<b>bold</b>"));
53/// ```
54///
55/// # Examples
56///
57/// ```
58/// use flm_core::stdlib::backends::html::{HtmlSink, Markup};
59///
60/// let mut sink = HtmlSink::new();
61/// sink.text("2 < 3");
62/// sink.markup(&Markup::trusted("<br>"));
63/// assert_eq!(sink.finish(), "2 &lt; 3<br>");
64/// ```
65#[derive(Debug, Clone, PartialEq, Eq, Hash)]
66pub struct Markup(String);
67
68impl Markup {
69    /// Returns a markup value holding `html`, on the caller's assurance that
70    /// the bytes are HTML that is safe to place in a page ([the type's
71    /// documentation](Markup) states what that assurance covers).
72    ///
73    /// It is the only way, outside this crate, to put unescaped bytes anywhere
74    /// in an HTML page other than inside a `<style>` or a `<script>` (see
75    /// [`HtmlSink::raw_text_element`](super::HtmlSink::raw_text_element)).
76    ///
77    /// # Examples
78    ///
79    /// ```
80    /// use flm_core::stdlib::backends::html::Markup;
81    ///
82    /// let markup = Markup::trusted("<svg viewBox=\"0 0 1 1\"></svg>");
83    /// assert_eq!(markup.as_str(), "<svg viewBox=\"0 0 1 1\"></svg>");
84    /// ```
85    pub fn trusted(html: impl Into<String>) -> Markup {
86        Markup(html.into())
87    }
88
89    /// Returns a markup value holding HTML this library itself wrote or
90    /// received from its own configuration: the HTML of a tree or a piece and
91    /// the document type line as the page assembly writes them, the content of
92    /// a raw text element as
93    /// [`HtmlSink::raw_text_element`](super::HtmlSink::raw_text_element) writes
94    /// it once its check has passed, and the markup body of a construct node
95    /// read back from a stored lowered tree.
96    ///
97    /// It is crate-private because those bytes are the library's own, a
98    /// feature author's, or the host's configuration, never a document's: code
99    /// outside this crate uses [`trusted`](Self::trusted) and accepts the
100    /// requirement stated there.
101    pub(crate) fn from_library(html: impl Into<String>) -> Markup {
102        Markup(html.into())
103    }
104
105    /// Returns the HTML as text.
106    pub fn as_str(&self) -> &str {
107        &self.0
108    }
109}
110
111impl fmt::Display for Markup {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        f.write_str(&self.0)
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn a_markup_value_carries_its_bytes_unchanged() {
123        let markup = Markup::trusted("<em>a & b</em>");
124        assert_eq!(markup.as_str(), "<em>a & b</em>");
125        assert_eq!(markup.to_string(), "<em>a & b</em>");
126        assert_eq!(Markup::from_library(String::from("x")).as_str(), "x");
127    }
128}