Skip to main content

text_content_into

Function text_content_into 

Source
pub fn text_content_into(
    doc: &BaseDocument,
    node: NodeId,
    out: &mut [u8],
) -> Result<usize>
Expand description

node.textContent, written into a caller-supplied buffer.

Returns the full byte length, always. The bytes were written to out[..len] if and only if len <= out.len(); a value that does not fit leaves out untouched rather than truncated. Same contract as element::get_attribute_into.

§What this trades, which is not what the attribute reader trades

An attribute’s value exists in the document as contiguous bytes, so reading it into a buffer is one memcpy and the owning reader’s String was pure overhead. textContent has no such bytes. It is a concatenation over a subtree, so it does not exist anywhere until something builds it, and the only question is where.

So this does not remove a copy, it removes an allocation: the bytes land in the caller’s buffer instead of in a String that is then copied there. The price is a second traversal — one pass to measure, one to fill — because the “nothing is written unless it all fits” guarantee cannot be honoured by a single streaming pass that discovers the overflow halfway through.

Whether one allocation is worth one extra pointer-chasing walk of a subtree is a timing question, and this crate deliberately measures bytes rather than time. What is not a timing question: the allocation is gone, and a caller that already knows the length can skip the measuring pass by calling text_content_len itself.