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