ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
reflow_tokenize_attr.c File Reference

Attribute and CSS-identity helpers for the no-heap XHTML tokenizer: quoted-attribute scanning, <img src> / <a href> capture, CSS element-identity + inline-style extraction, link interning, and embedded-face resolution. More...

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "ra8_attributes.h"
#include "ra8_err.h"
#include "reflow.h"
#include "reflow_tokenize_internal.h"
Include dependency graph for reflow_tokenize_attr.c:

Go to the source code of this file.

Functions

static bool internal_attr_name_at (const uint8_t *tag, size_t i, const char *name, size_t name_len)
 True iff attribute name (case-insensitive) begins at tag[i].
static bool internal_attr_quoted_value (const uint8_t *tag, size_t tag_len, size_t pos, size_t *vstart, size_t *vlen)
 Parse = "value" after an attribute name; return the value span.
bool priv_reflow_tok_find_attr (const uint8_t *tag, size_t tag_len, const char *name, size_t name_len, size_t *out_voff, size_t *out_vlen)
 Locate a named attribute's quoted value span within a tag (no copy).
void priv_reflow_tok_capture_attr (reflow_t *engine, const uint8_t *tag, size_t tag_len, const char *name, size_t name_len, uint32_t *out_off, uint32_t *out_len)
 Copy a named attribute's quoted value from a tag span into the text pool.
ra8_css_element_t priv_reflow_tok_css_element (reflow_html_tag_t kind, const uint8_t *tag, size_t span)
 Build a CSS element identity from a just-opened tag's attributes.
ra8_css_style_t priv_reflow_tok_css_inline (const uint8_t *tag, size_t span)
 Parse a just-opened tag's inline style="..." into a CSS declaration.
uint8_t priv_reflow_tok_intern_link (reflow_t *engine, uint32_t href_off, uint32_t href_len)
 Intern an <a> href slice into the link-target table.
bool priv_reflow_tok_rel_is_stylesheet (const uint8_t *rel, size_t len)
 True if a rel attribute value contains the stylesheet token.
uint8_t priv_reflow_tok_resolve_face_slot (const reflow_t *engine, const ra8_css_style_t *comp)
 Map a cascaded run's (font-family + emphasis) to an embedded face slot.

Detailed Description

Attribute and CSS-identity helpers for the no-heap XHTML tokenizer: quoted-attribute scanning, <img src> / <a href> capture, CSS element-identity + inline-style extraction, link interning, and embedded-face resolution.

Companion translation unit to reflow_tokenize.c. It groups the routines that read a raw <...> start-tag span: locating a named quoted attribute, copying its value into the engine text pool, building the ra8_css_element_t identity (tag + id + class) the cascade matches against, parsing the inline style="..." declaration, interning an <a href> into the link-target table, detecting a <link rel> stylesheet, and mapping a cascaded font-family + emphasis to an embedded face slot. None of these touch the tokenizer's mutable tok_ctx_t; the markup handlers in reflow_tokenize.c call the cross-TU helpers promoted into reflow_tokenize_internal.h. Pure forward-pass scanning – no recursion (NASA P10 Rule 1) and bounded loops (Rule 2).

[Ring 4 / Reflow] {World: NS}

Since
0.1.0

Definition in file reflow_tokenize_attr.c.

Function Documentation

◆ internal_attr_name_at()

bool internal_attr_name_at ( const uint8_t * tag,
size_t i,
const char * name,
size_t name_len )
static

True iff attribute name (case-insensitive) begins at tag[i].

Matches name with a non-name byte (or the leading '<') immediately before it, so a substring like "xsrc" does not match "src" and "xid" does not match "id".

Parameters
[in]tagRaw tag span.
[in]iCandidate start offset (i + name_len must be in range).
[in]nameLower-case attribute name to match.
[in]name_lenLength of name, bytes.
Returns
true iff tag[i..i+name_len) equals name as an attribute name.
Return values
trueMatch.
falseNo match.
Precondition
tag, name are non-null and tag[i..i+name_len) are readable.
i + name_len <= tag span length.
Postcondition
No state mutated.
Return value depends solely on the inputs.
Note
Pure function; name must already be lower-case.
Since
0.1.0

Definition at line 66 of file reflow_tokenize_attr.c.

Referenced by priv_reflow_tok_find_attr().

◆ internal_attr_quoted_value()

bool internal_attr_quoted_value ( const uint8_t * tag,
size_t tag_len,
size_t pos,
size_t * vstart,
size_t * vlen )
static

Parse = "value" after an attribute name; return the value span.

Skips whitespace, requires =, skips whitespace, requires a quote, then scans to the matching quote. The span excludes the quotes.

Parameters
[in]tagRaw tag span.
[in]tag_lenLength of tag, bytes.
[in]posOffset just past the attribute name.
[out]vstartReceives the value start offset.
[out]vlenReceives the value length, bytes.
Returns
true iff a quoted value was found.
Return values
true*vstart / *vlen describe the value.
falseNo = "..." followed the name.
Precondition
tag, vstart, vlen are non-null.
pos <= tag_len.
Postcondition
On true, [*vstart, *vstart+*vlen) lies within the tag span.
On false, the outputs are unspecified (caller ignores them).
Note
Pure read of tag.
Since
0.1.0

Definition at line 100 of file reflow_tokenize_attr.c.

References priv_reflow_tok_is_xml_whitespace().

Referenced by priv_reflow_tok_find_attr().

◆ priv_reflow_tok_capture_attr()

void priv_reflow_tok_capture_attr ( reflow_t * engine,
const uint8_t * tag,
size_t tag_len,
const char * name,
size_t name_len,
uint32_t * out_off,
uint32_t * out_len )

Copy a named attribute's quoted value from a tag span into the text pool.

Copy a named attribute's quoted value from a tag span into the pool.

Scans the raw <...> span for attribute name (case-insensitive, not preceded by another name character) and copies its quoted value verbatim into the engine text pool. Used for <img src> / <a href> / element id. On no match, a malformed attribute, an empty value, or a full pool the outputs are zeroed, which callers treat as "attribute absent".

Parameters
[in,out]engineEngine whose text pool receives the value bytes.
[in]tagRaw tag span starting at '<'.
[in]tag_lenLength of tag, bytes.
[in]nameLower-case attribute name to find.
[in]name_lenLength of name, bytes.
[out]out_offReceives the text-pool offset of the value (0 if none).
[out]out_lenReceives the value byte length (0 if none).
Returns
Nothing.
Precondition
engine, tag, name, out_off, out_len are non-null.
engine->text_pool_used <= k_reflow_text_pool_bytes.
Postcondition
*out_len > 0 iff a quoted name value was stored in the pool.
The pool grows by *out_len bytes when a value is stored.
Note
Not thread-safe.
Since
0.1.0

Definition at line 196 of file reflow_tokenize_attr.c.

References k_reflow_text_pool_bytes, memcpy(), priv_reflow_tok_find_attr(), reflow_t::text_pool, and reflow_t::text_pool_used.

Referenced by internal_handle_void(), and internal_open_attrs().

◆ priv_reflow_tok_css_element()

ra8_css_element_t priv_reflow_tok_css_element ( reflow_html_tag_t kind,
const uint8_t * tag,
size_t span )

Build a CSS element identity from a just-opened tag's attributes.

Locates the id and class attribute value spans in place (no copy) so the cascade can match #id / .class selectors; the returned pointers alias tag. A missing attribute leaves its pointer NULL and its length zero. The element's tag enum is stored so type selectors also work.

Parameters
[in]kindClassified tag.
[in]tagRaw tag span starting at '<'.
[in]spanLength of tag, bytes.
Returns
The element identity for ra8_css_cascade().
Return values
ra8_css_element_tFully populated struct; absent attributes have NULL pointer and zero length.
Precondition
tag is non-null and holds span bytes.
kind is a valid reflow_html_tag_t value.
Postcondition
No state mutated; returned pointers alias tag.
el.tag == (uint8_t)kind.
Note
Pure read of tag.
Since
0.1.0

Definition at line 243 of file reflow_tokenize_attr.c.

References ra8_css_element_t::class_len, ra8_css_element_t::class_str, ra8_css_element_t::id, ra8_css_element_t::id_len, priv_reflow_tok_find_attr(), and ra8_css_element_t::tag.

Referenced by internal_handle_start().

◆ priv_reflow_tok_css_inline()

ra8_css_style_t priv_reflow_tok_css_inline ( const uint8_t * tag,
size_t span )

Parse a just-opened tag's inline style="..." into a CSS declaration.

Locates the style attribute within the raw tag span and delegates to ra8_css_parse_inline() to parse its value into a declaration struct. When the attribute is absent or the parser fails the returned struct has set == 0, indicating no inline declarations override the cascade.

Parameters
[in]tagRaw tag span starting at '<'.
[in]spanLength of tag, bytes.
Returns
The inline CSS declaration for this element.
Return values
ra8_css_style_tStruct with set == 0 if no style attribute is present.
Precondition
tag is non-null and holds span bytes.
span > 0 (a zero-span tag cannot carry attributes).
Postcondition
No state mutated.
Returned struct set field is 0 when no style attribute was found.
Note
Pure read of tag.
Since
0.1.0

Definition at line 279 of file reflow_tokenize_attr.c.

References priv_reflow_tok_find_attr(), and ra8_css_parse_inline().

Referenced by internal_open_styled().

◆ priv_reflow_tok_find_attr()

bool priv_reflow_tok_find_attr ( const uint8_t * tag,
size_t tag_len,
const char * name,
size_t name_len,
size_t * out_voff,
size_t * out_vlen )

Locate a named attribute's quoted value span within a tag (no copy).

The non-copying core of priv_reflow_tok_capture_attr(): returns the value's offset + length *within tag* so callers can either copy it to the pool or scan it in place (e.g. an inline style).

Parameters
[in]tagRaw tag span starting at '<'.
[in]tag_lenLength of tag, bytes.
[in]nameLower-case attribute name to find.
[in]name_lenLength of name, bytes.
[out]out_voffReceives the value offset within tag.
[out]out_vlenReceives the value length, bytes.
Returns
true iff a quoted name value was found.
Return values
true*out_voff / *out_vlen describe the value within tag.
falseAttribute absent or malformed.
Precondition
tag, name, out_voff, out_vlen are non-null.
tag_len >= name_len (zero-length searches return false immediately).
Postcondition
No state mutated.
On false, *out_voff and *out_vlen are unspecified.
Note
Pure read of tag.
Since
0.1.0

Definition at line 153 of file reflow_tokenize_attr.c.

References internal_attr_name_at(), and internal_attr_quoted_value().

Referenced by internal_handle_link(), priv_reflow_tok_capture_attr(), priv_reflow_tok_css_element(), and priv_reflow_tok_css_inline().

◆ priv_reflow_tok_intern_link()

uint8_t priv_reflow_tok_intern_link ( reflow_t * engine,
uint32_t href_off,
uint32_t href_len )

Intern an <a> href slice into the link-target table.

Appends a {href_off, href_len} entry and returns its 1-based id (stored in subsequent text tokens' reserved byte). An empty href or a full table returns 0 (the run renders as plain underlined text – not tappable).

Parameters
[in,out]engineEngine whose link-target table grows.
[in]href_offHref text-pool offset.
[in]href_lenHref byte length.
Returns
1-based link id, or 0 if not interned.
Return values
0Empty href or the table is full.
1..k_reflow_max_linksThe newly assigned 1-based link id.
Precondition
engine is non-null.
engine->link_target_count <= k_reflow_max_links.
Postcondition
On a non-zero return the table grew by one entry.
engine->link_target_count is unchanged on a zero return.
Note
Not thread-safe.
Since
0.1.0

Definition at line 315 of file reflow_tokenize_attr.c.

References reflow_link_target_t::href_len, reflow_link_target_t::href_off, k_reflow_max_links, reflow_t::link_target_count, and reflow_t::link_targets.

Referenced by internal_open_attrs().

◆ priv_reflow_tok_rel_is_stylesheet()

bool priv_reflow_tok_rel_is_stylesheet ( const uint8_t * rel,
size_t len )

True if a rel attribute value contains the stylesheet token.

Case-sensitive substring scan (EPUB <link> rels are authored lowercase; this also accepts the alternate stylesheet form). Bounded by the value length. The inner loop iterates at most len times so the scan is O(len) with no dynamic allocation.

Parameters
[in]relAttribute-value bytes (not NUL-terminated).
[in]lenLength of rel, bytes.
Returns
true if stylesheet occurs in rel.
Return values
trueThe substring "stylesheet" was found in rel.
falseThe substring is absent or len is too short to contain it.
Precondition
rel addresses len readable bytes.
rel is non-null (a zero len is a valid empty-value case).
Postcondition
No state mutated.
Return value depends solely on the inputs.
Note
Pure.
Since
0.1.0

Definition at line 347 of file reflow_tokenize_attr.c.

Referenced by internal_handle_link().

◆ priv_reflow_tok_resolve_face_slot()

uint8_t priv_reflow_tok_resolve_face_slot ( const reflow_t * engine,
const ra8_css_style_t * comp )

Map a cascaded run's (font-family + emphasis) to an embedded face slot.

Returns 0 (the engine's default bound face) when no embedded face is registered, the run has no resolved font-family, no @font-face matches, or the matched @font-face was never registered (its src had no manifest font). Otherwise returns 1 + the registry index, which the render pass uses to pick the matching stbtt_fontinfo.

Parameters
[in]engineEngine whose face registry + parsed sheet are consulted.
[in]compThe element's cascaded style (family + emphasis bits).
Returns
0 for the default face, else 1 + engine->faces[] index.
Return values
0No registered face matches the cascaded family + emphasis combination.
Precondition
engine and comp are non-null.
engine->face_count <= k_reflow_max_faces.
Postcondition
No state is modified.
Return value is in [0, engine->face_count].
Note
Pure; not thread-safe only insofar as it reads engine state.
Since
0.1.0

Definition at line 382 of file reflow_tokenize_attr.c.

References reflow_t::css, reflow_face_t::css_face_idx, reflow_t::face_count, reflow_t::faces, ra8_css_style_t::family_len, ra8_css_style_t::family_off, k_ra8_css_set_family, k_reflow_style_bold, k_reflow_style_italic, ra8_css_sheet_t::names, ra8_css_match_face(), ra8_css_style_t::set, and ra8_css_style_t::style.

Referenced by internal_open_styled().