Skip to main content

HtmlSink

Struct HtmlSink 

Source
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

Source

pub fn new() -> HtmlSink

An empty sink.

Source

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.

Source

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.

Source

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>");
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>",
);
Source

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&lt;b".len());
Source

pub fn is_empty(&self) -> bool

Whether nothing has been written yet.

Source

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);
Source

pub fn finish(self) -> String

Returns the HTML written, ending the sink.

§Panics

When an element is still open. That is a mistake in the emitter — it would hand over a page with an unclosed element — and never anything a document can cause.

Trait Implementations§

Source§

impl Clone for HtmlSink

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HtmlSink

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The sink’s state — the bytes written so far counted, the block-level depth, the open elements by tag name and how many of them are preformatted regions — and never the bytes written.

Source§

impl Default for HtmlSink

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for HtmlSink

§

impl Freeze for HtmlSink
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, PhantomData<OpenElement>: Freeze,

§

impl Move for HtmlSink

§

impl RefUnwindSafe for HtmlSink

§

impl Send for HtmlSink
where RawVecInner: Send,

§

impl Sync for HtmlSink
where RawVecInner: Sync,

§

impl Unpin for HtmlSink
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for HtmlSink

§

impl UnwindSafe for HtmlSink
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.