pub struct HtmlSink { /* private fields */ }Expand description
The HTML sink: the only interface through which an HTML emitter writes bytes (see the module documentation).
A sink is created empty, written into through its entry points, and handed
over as a string by finish.
The bytes leave the sink through finish and by no other way: the Debug
form of a sink shows how many bytes have been written, how deep the
block-level nesting is and which elements are open, and never the bytes
themselves, so that no output can be read back through it — see the module
documentation for why that matters.
Implementations§
Source§impl HtmlSink
impl HtmlSink
Sourcepub fn text(&mut self, text: &str)
pub fn text(&mut self, text: &str)
Writes document text, escaped
(super::escape::escape_text_into).
Text is escaped the same way everywhere, a preformatted region included: a preformatted region changes how a browser lays text out, not how it reads markup.
Sourcepub fn markup(&mut self, markup: &Markup)
pub fn markup(&mut self, markup: &Markup)
Writes a Markup value: HTML that is written unchanged, without
escaping.
It is one of the two entry points that write bytes unescaped; the other
is raw_text_element, for the content of
a <style> or a <script>. See Markup for what the creator of the
value must ensure.
Sourcepub fn markup_block(&mut self, markup: &Markup)
pub fn markup_block(&mut self, markup: &Markup)
Writes a Markup value as block-level content: on a line of its
own, at the indentation of the place where it is written, and with the
closing tag of the enclosing element written on a line of its own too.
This is how HTML that this library already wrote is placed inside other HTML: the page assembly writes each document tree’s HTML into the page through it, so that the line break between two block-level tags is there at the join as it is everywhere else: after the opening tag of the element the trees are written into, between two trees, and before that element’s closing tag.
The bytes themselves are written unchanged. The indentation inside them is not touched: changing it would mean reading finished output back and rewriting it, which this sink offers no way to do. The line break and the indentation before them are written as the bytes are placed, as for every block-level element written through this sink.
This is not a second path for unescaped bytes. What it writes is a
Markup value, created under the requirement that type’s
documentation states, and this entry point changes nothing about that
requirement: it decides where the bytes are placed and nothing about
what they are. Everything markup says therefore
holds here too.
Nothing is introduced inside a preformatted region or in inline flow, exactly as when a block-level element is opened; the caller places the markup where a block-level element could be placed.
§Examples
use flm_core::stdlib::backends::html::{HtmlSink, Markup};
let mut sink = HtmlSink::new();
sink.open_block("main").finish();
sink.markup_block(&Markup::trusted("<p>one</p>\n<p>two</p>"));
sink.close();
// The markup begins on a line of its own, at the indentation of the
// place where it is written, and keeps its own line breaks inside.
assert_eq!(sink.finish(), "<main>\n <p>one</p>\n<p>two</p>\n</main>");Sourcepub fn open_block(&mut self, tag: &str) -> TagBuilder<'_>
pub fn open_block(&mut self, tag: &str) -> TagBuilder<'_>
Opens a block-level element with the tag name tag and hands back
the TagBuilder that writes its attributes.
Block-level is what the line breaks and the indentation are written
between (see the module documentation). The element stays open
until close.
§Preconditions
The caller has validated tag with is_valid_tag_name; the tag
name is then written unchanged. A name that fails that check is a
programming error in the calling code, never document input, since a
name that came from a construct node is checked by
the emitter, which reports the node it refused.
§Panics
Debug builds only: tag fails that check. A release build writes the
name unchanged, which may produce markup the caller did not intend.
Sourcepub fn open_inline(&mut self, tag: &str) -> TagBuilder<'_>
pub fn open_inline(&mut self, tag: &str) -> TagBuilder<'_>
Opens an inline element with the tag name tag and hands back the
TagBuilder that writes its attributes.
No whitespace is ever introduced around or inside an inline element:
whitespace there is text a reader sees. The element stays open until
close.
§Preconditions
The caller has validated tag, as for
open_block.
§Panics
Debug builds only: tag fails that check.
Sourcepub fn void_block(&mut self, tag: &str) -> TagBuilder<'_>
pub fn void_block(&mut self, tag: &str) -> TagBuilder<'_>
Writes a block-level element that has no content and is never closed —
<hr>, <meta> — and hands back the TagBuilder that writes its
attributes.
§Preconditions
The caller has validated tag, as for
open_block.
§Panics
Debug builds only: tag fails that check.
Sourcepub fn void_inline(&mut self, tag: &str) -> TagBuilder<'_>
pub fn void_inline(&mut self, tag: &str) -> TagBuilder<'_>
Writes an inline element that has no content and is never closed —
<img>, <br> — and hands back the TagBuilder that writes its
attributes.
§Preconditions
The caller has validated tag, as for
open_block.
§Panics
Debug builds only: tag fails that check.
Sourcepub fn close(&mut self)
pub fn close(&mut self)
Closes the element the sink opened last.
Elements therefore close in the order they were opened, by construction: there is no way to name another one.
§Panics
When no element is open. That is a mistake in the emitter — it closed something it did not open — and never anything a document can cause.
Sourcepub fn raw_text_element(
&mut self,
tag: &str,
attributes: &[(&str, &str)],
content: &str,
) -> Result<(), RawTextRefused>
pub fn raw_text_element( &mut self, tag: &str, attributes: &[(&str, &str)], content: &str, ) -> Result<(), RawTextRefused>
Writes a raw text element — a <style> or a <script> — with the
attributes given and content as its content, written byte for byte.
A browser reads the content of such an element as raw text up to the element’s closing tag, so nothing in it can be escaped: this is where the text of a style or script definition, and what a host put in its place, is written.
style and script are the two elements HTML reads that way, and this
entry point writes those two tags and no other: an element whose
content a browser does read as markup — <textarea>, <title> — is
written through open_block or
open_inline and text,
which escape what they write.
The attributes are written in the order given, each value escaped as any other attribute value.
This is one of the two entry points that write bytes unescaped; the
other is markup.
§Errors
RawTextRefused when content holds the element’s own closing tag,
in any letter case, which would end the element there; nothing is
written then.
§Preconditions
The content is written without escaping, so the caller must ensure that it does not come from a document or from any other input the caller does not control (this library passes only the text of style and script definitions here).
The caller has validated every attribute name with
is_valid_attribute_name; a name that fails that check is a
programming error in the calling code, never document input.
§Panics
When tag is neither style nor script. The content of this element
is written unescaped, so writing it into an element a browser parses
some other way would put unescaped bytes where they are read as markup;
that is a programming error in the calling code, and the method panics
in every build rather than only in a debug build.
Debug builds only: an attribute name fails the check above.
§Examples
use flm_core::stdlib::backends::html::HtmlSink;
let mut sink = HtmlSink::new();
sink.raw_text_element("style", &[("media", "screen")], ".flm-pres-par { margin: 0 }\n")
.expect("the stylesheet holds no closing tag");
assert_eq!(
sink.finish(),
"<style media=\"screen\">.flm-pres-par { margin: 0 }\n</style>",
);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many bytes have been written so far.
This is a count, not content: the sink still offers no way to read what it wrote, so no decision can depend on the bytes and no escaping can run over them a second time. The count exists because an emitter has to be able to say where in its output something was written — the HTML emitter records the offset at which a document tree placed in another tree belongs, so that the page assembly can write that tree’s HTML there.
§Examples
use flm_core::stdlib::backends::html::HtmlSink;
let mut sink = HtmlSink::new();
assert_eq!(sink.len(), 0);
sink.text("a<b");
assert_eq!(sink.len(), "a<b".len());Sourcepub fn open_tag(&self) -> Option<&str>
pub fn open_tag(&self) -> Option<&str>
The tag of the element the sink would close next — the one it opened
last — or None when no element is open.
Like len, this returns a fact about the sink’s state
and never the bytes written. It lets an emitter that keeps its own
record of the elements it has opened (the HTML emitter keeps one for the
elements inside a formula, which it may have to write a second time)
check, in a debug build, that its record and the sink’s agree.
§Examples
use flm_core::stdlib::backends::html::HtmlSink;
let mut sink = HtmlSink::new();
assert_eq!(sink.open_tag(), None);
sink.open_inline("em").finish();
assert_eq!(sink.open_tag(), Some("em"));
sink.close();
assert_eq!(sink.open_tag(), None);Trait Implementations§
Auto Trait Implementations§
impl Forget for HtmlSink
impl Freeze for HtmlSink
impl Move for HtmlSink
impl RefUnwindSafe for HtmlSink
impl Send for HtmlSinkwhere
RawVecInner: Send,
impl Sync for HtmlSinkwhere
RawVecInner: Sync,
impl Unpin for HtmlSink
impl UnsafeUnpin for HtmlSinkwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<OpenElement>: UnsafeUnpin,
impl UnwindSafe for HtmlSinkwhere
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more