| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /* browser_html: a small tolerant HTML tree + CSS cascade for the browser.
- *
- * Freestanding and independent of ngui: it parses a byte buffer into a static
- * node/string arena and resolves the subset of CSS the browser understands.
- * One document is live at a time (the browser is single-threaded). */
- #ifndef BROWSER_HTML_H
- #define BROWSER_HTML_H
- #define BR_MAX_NODES 16384
- #define BR_TEXT_SIZE 524288
- #define BR_ATTR_SIZE 4194304
- #define BR_CSS_SIZE 65536
- #define BR_MAX_RULES 8192
- #define BR_RULE_FALLBACK 256
- #define BR_MAX_FACES 8
- #define BR_FACE_FAMILY 40
- #define BR_FACE_URL 192
- #define BR_MAX_VARS 512
- #define BR_MAX_CONTENT 512
- #define BR_TAG_MAX 128
- #define BR_RULE_HASH 256
- #define BR_DECLS 48
- #define BR_TITLE_MAX 96
- #define BR_MAX_SCRIPTS 32
- /* Node types. */
- #define BR_NODE_TEXT 0
- #define BR_NODE_ELEM 1
- #define BR_NODE_COMMENT 2
- #define BR_NODE_FRAGMENT 3
- /* Text-node flag: whitespace was preserved (<pre>). */
- #define BR_NODE_PREFORMAT 1
- /* Element flag: the HTML hidden attribute is present. */
- #define BR_NODE_HIDDEN 2
- /* Element flag: width_attr/height_attr hold an SVG viewBox. */
- #define BR_NODE_VIEWBOX 4
- /* Node is not part of the document tree (created or removed at runtime). */
- #define BR_NODE_DETACHED 8
- /* Extra attribute presence flags (data-collapse-*, state attributes).
- * Stored separately because struct br_style/selector matching needs them
- * but the node flags byte is already used. */
- #define BR_ATTR_COLLAPSE_TARGET 0x01
- #define BR_ATTR_COLLAPSE_TRIGGER 0x02
- #define BR_ATTR_IS_OPEN 0x04
- #define BR_ATTR_DISABLED 0x08
- #define BR_ATTR_OPEN 0x10
- #define BR_ATTR_INERT 0x20
- #define BR_ATTR_ANCHOR_MIDDLE 0x40
- #define BR_ATTR_ANCHOR_END 0x80
- /* Display values in struct br_style. */
- #define BR_DISPLAY_INLINE 0
- #define BR_DISPLAY_BLOCK 1
- #define BR_DISPLAY_NONE 2
- #define BR_DISPLAY_FLEX 3
- #define BR_DISPLAY_GRID 4
- #define BR_DISPLAY_INLINE_BLOCK 5
- /* Decl-only bit: display:inline-flex is FLEX plus st->inline_flex. */
- #define BR_DISPLAY_INLINE_FLAG 0x40
- /* Text alignment. */
- #define BR_ALIGN_LEFT 0
- #define BR_ALIGN_CENTER 1
- #define BR_ALIGN_RIGHT 2
- /* list-style-type; AUTO follows the parent ul/ol. */
- #define BR_LIST_AUTO 0
- #define BR_LIST_DISC 1
- #define BR_LIST_CIRCLE 2
- #define BR_LIST_SQUARE 3
- #define BR_LIST_DECIMAL 4
- #define BR_LIST_LOWER_ALPHA 5
- #define BR_LIST_UPPER_ALPHA 6
- #define BR_LIST_LOWER_ROMAN 7
- #define BR_LIST_UPPER_ROMAN 8
- #define BR_LIST_NONE 9
- #define BR_BORDER_SOLID 0
- #define BR_BORDER_DASHED 1
- #define BR_BORDER_DOTTED 2
- enum br_tag {
- BR_TAG_UNKNOWN = 0,
- BR_TAG_HTML, BR_TAG_HEAD, BR_TAG_BODY, BR_TAG_TITLE, BR_TAG_STYLE,
- BR_TAG_SCRIPT, BR_TAG_META, BR_TAG_LINK, BR_TAG_BASE, BR_TAG_NOSCRIPT,
- BR_TAG_P, BR_TAG_DIV, BR_TAG_SPAN, BR_TAG_A, BR_TAG_B, BR_TAG_STRONG,
- BR_TAG_I, BR_TAG_EM, BR_TAG_U, BR_TAG_CODE, BR_TAG_FONT, BR_TAG_SMALL,
- BR_TAG_BIG, BR_TAG_BR, BR_TAG_HR, BR_TAG_IMG, BR_TAG_PICTURE,
- BR_TAG_H1, BR_TAG_H2, BR_TAG_H3, BR_TAG_H4, BR_TAG_H5, BR_TAG_H6,
- BR_TAG_UL, BR_TAG_OL, BR_TAG_LI, BR_TAG_BLOCKQUOTE, BR_TAG_PRE,
- BR_TAG_CENTER, BR_TAG_TABLE, BR_TAG_THEAD, BR_TAG_TBODY, BR_TAG_TFOOT,
- BR_TAG_TR, BR_TAG_TD, BR_TAG_TH, BR_TAG_CAPTION,
- BR_TAG_FORM, BR_TAG_INPUT, BR_TAG_BUTTON, BR_TAG_LABEL, BR_TAG_SELECT,
- BR_TAG_OPTION, BR_TAG_TEXTAREA, BR_TAG_IFRAME, BR_TAG_SVG,
- BR_TAG_VIDEO, BR_TAG_AUDIO, BR_TAG_CANVAS, BR_TAG_OBJECT,
- BR_TAG_MAIN, BR_TAG_HEADER, BR_TAG_FOOTER, BR_TAG_NAV, BR_TAG_SECTION,
- BR_TAG_ARTICLE, BR_TAG_ASIDE, BR_TAG_FIGURE, BR_TAG_FIGCAPTION,
- BR_TAG_DETAILS, BR_TAG_SUMMARY, BR_TAG_DL, BR_TAG_DT, BR_TAG_DD,
- BR_TAG_ADDRESS, BR_TAG_MARK, BR_TAG_TIME, BR_TAG_ABBR, BR_TAG_CITE,
- BR_TAG_Q, BR_TAG_S, BR_TAG_STRIKE, BR_TAG_SUB, BR_TAG_SUP,
- BR_TAG_BGSOUND, BR_TAG_EMBED, BR_TAG_PARAM, BR_TAG_SOURCE,
- BR_TAG_TRACK, BR_TAG_WBR, BR_TAG_AREA, BR_TAG_COL, BR_TAG_COLGROUP
- };
- struct br_style {
- unsigned short color;
- unsigned short bg;
- unsigned short border_c[4]; /* top, right, bottom, left */
- unsigned short bg_image; /* 1-based background URL, 0 none */
- unsigned char bg_repeat; /* 0 repeat, 1 no-repeat, 2 repeat-x, 3 repeat-y */
- unsigned char bg_pos_x_kind, bg_pos_y_kind; /* 0 px, 1 percent */
- signed short bg_pos_x, bg_pos_y;
- unsigned char bg_size_kind; /* 0 auto/stretch, 1 px, 2 percent, 3 cover, 4 contain */
- unsigned short bg_size_w, bg_size_h;
- unsigned char has_bg;
- unsigned char font_px; /* 8..48 */
- unsigned short font_raw; /* font-size before unit resolution */
- unsigned char font_unit; /* 0 inherited, 1 px, 2 em, 3 rem, 4 % */
- unsigned char bold;
- unsigned char italic;
- unsigned char mono;
- unsigned char font_face; /* 1-based @font-face id, 0 = built-in */
- unsigned char underline;
- unsigned char strike; /* text-decoration: line-through */
- unsigned char overline; /* text-decoration: overline */
- unsigned char align; /* BR_ALIGN_* */
- unsigned char display; /* BR_DISPLAY_* */
- unsigned char vis_hidden, opacity_hidden, overflow_hidden;
- unsigned char float_left; /* float: left/right */
- unsigned char clear; /* 0 none, 1 left, 2 right, 3 both */
- unsigned char flex_grow; /* flex-grow, 0..100 */
- unsigned char flex_col; /* flex-direction: column */
- unsigned char flex_wrap; /* flex-wrap: wrap */
- unsigned char justify; /* 0 start, 1 center, 2 end, 3 between, 4 around */
- unsigned char align_items; /* 0 start/stretch, 1 center, 2 end */
- unsigned char grid_ncols; /* 1 or 2 supported */
- unsigned char grid_gap; /* px */
- unsigned char grid_slot; /* 0 auto, 1 full, 2 left, 3 right */
- unsigned char grid_auto; /* repeat(auto-fit, minmax(width, 1fr)) */
- unsigned char pos; /* 0 static, 1 absolute, 2 fixed, 3 relative */
- unsigned char left_set, top_set, right_set, bottom_set;
- signed short left, top, right, bottom;
- signed short z_index;
- unsigned char z_set;
- signed short tx, ty;
- unsigned char transform_set;
- unsigned char transform_scale; /* scale()/rotate() paint hint */
- signed short scale_x, scale_y; /* hundredths, 100 = 1.0 */
- signed short rotate_x100; /* hundredths of a degree */
- signed short origin_x, origin_y;
- unsigned char origin_x_kind, origin_y_kind; /* 0 px, 1 percent */
- unsigned char no_bullet; /* list-style: none (inherited) */
- unsigned char list_style; /* BR_LIST_* (inherited from ul/ol) */
- unsigned char list_item; /* li marker is drawn */
- unsigned char nowrap; /* white-space: nowrap (inherited) */
- unsigned char uppercase; /* text-transform: uppercase (inherited) */
- unsigned char border_w[4]; /* top, right, bottom, left */
- unsigned char border_s[4]; /* BR_BORDER_* per side */
- unsigned char border_t[4]; /* 1 = fully transparent border side */
- /* border-radius in pixels, one entry per corner: TL, TR, BR, BL.
- * Percentages are stored as 255 so they clamp to the box half. */
- unsigned char radius[4];
- unsigned char width_kind; /* 0 none, 1 px, 2 percent, 3 calc */
- unsigned short width;
- signed short width_calc_pct;
- signed short width_calc_px;
- unsigned char max_width_kind;
- unsigned short max_width;
- unsigned char min_width_kind;
- unsigned short min_width;
- unsigned char height_kind;
- unsigned short height;
- signed short height_calc_pct, height_calc_px;
- unsigned char min_height_kind;
- unsigned short min_height;
- signed short min_height_calc_pct, min_height_calc_px;
- unsigned char max_height_kind;
- unsigned short max_height;
- signed short max_height_calc_pct, max_height_calc_px;
- signed short min_width_calc_pct, min_width_calc_px;
- signed short max_width_calc_pct, max_width_calc_px;
- unsigned char box_sizing; /* 0 content-box, 1 border-box */
- unsigned char ml_auto, mr_auto;
- unsigned char line_h_pct; /* percent of font size, 0 = default */
- unsigned short line_h_px; /* absolute px, 0 = default */
- signed short mt, mb, ml, mr;
- signed short pt, pb, pl, pr;
- /* mt, mr, mb, ml, pt, pr, pb, pl: 0 px, 1 %, 2 em, 3 rem, 4 auto,
- * 5 calc (the raw value is the percent, mp_calc_px the px part) */
- unsigned char mp_unit[8];
- signed short mp_calc_px[8];
- /* SVG paint (inherited, like CSS fill/stroke): BR_PAINT_* modes.
- * stroke_w is in 1/16 user units; 0 means the default 1. */
- unsigned char fill_mode, stroke_mode, fill_evenodd;
- unsigned short fill, stroke;
- unsigned short stroke_w;
- unsigned char inline_flex; /* display:inline-flex (not inherited) */
- unsigned short content; /* ::before/::after content: index + 1 */
- };
- #define BR_PAINT_UNSET 0 /* fill: black, stroke: none */
- #define BR_PAINT_COLOR 1
- #define BR_PAINT_NONE 2
- #define BR_PAINT_CURRENT 3 /* currentColor -> style color */
- #define BR_PAINT_URL 4 /* url(#id): resolved from the attribute */
- struct br_node {
- unsigned char type;
- unsigned char tag;
- unsigned char flags;
- unsigned char attr_flags;
- unsigned short parent;
- unsigned short first;
- unsigned short last;
- unsigned short next;
- unsigned int text_off; /* text arena offset (text nodes) */
- unsigned short text_len;
- unsigned int href_off;
- unsigned int class_off;
- unsigned int id_off;
- unsigned int style_off;
- unsigned int src_off;
- unsigned int alt_off;
- unsigned int color_off;
- unsigned int rel_off;
- unsigned int name_off; /* raw tag name for custom elements */
- unsigned int extra_off; /* packed name\0value\0 list for getAttribute */
- unsigned short extra_len;
- unsigned short width_attr;
- unsigned short height_attr;
- unsigned char width_pct;
- unsigned char height_pct;
- unsigned char colspan;
- unsigned char rowspan;
- short bg_img_index; /* -1 until loaded, -2 failed */
- short img_index; /* -1 until the image is loaded */
- signed short svg_x, svg_y; /* inline SVG x/y or line x1/y1 */
- unsigned char svg_rx; /* inline SVG rect corner radius */
- struct br_style style; /* computed-style cache */
- unsigned char style_valid;
- unsigned char pe_state; /* br_pseudo() cache: 1/4 checked, 2/8 has */
- };
- struct br_decl {
- unsigned char prop;
- unsigned char important;
- short val;
- };
- struct br_rule {
- unsigned int sel_off; /* selector text in the attr arena */
- unsigned int key_off; /* rightmost compound, for a fast prefilter */
- unsigned short spec; /* selector specificity */
- unsigned char nd;
- unsigned char single; /* one compound, no ancestor walk needed */
- unsigned char pe; /* 0 element, 1 ::before, 2 ::after */
- struct br_decl d[BR_DECLS];
- };
- /* An inline <script> body. off/len index script_src, the buffer passed to
- * br_parse(); the caller must keep it alive for as long as the document (the
- * browser keeps the page body until the next navigation). */
- struct br_script {
- unsigned int off; /* inline body, or empty for src scripts */
- unsigned int len;
- unsigned int src_off; /* src attribute in the attr arena, 0 if inline */
- unsigned int module; /* type="module": run as an ES module */
- };
- struct br_doc {
- struct br_node *nodes;
- unsigned int nnode;
- unsigned int root;
- char *text;
- unsigned int text_used;
- char *attrs;
- unsigned int attrs_used;
- struct br_rule *rules;
- unsigned int nrules;
- unsigned int rule_cap;
- char title[BR_TITLE_MAX];
- unsigned int inline_css_len;
- /* CSS custom properties, stored in the attr arena. A pragmatic
- * single table: the last definition wins, which matches :root. */
- unsigned int var_name[BR_MAX_VARS];
- unsigned int var_val[BR_MAX_VARS];
- unsigned short var_slot[1024];
- unsigned int nvars;
- /* Rule index: rightmost class/id/tag -> linked list of rule indices. */
- unsigned short rule_next[BR_MAX_RULES];
- unsigned short rule_bucket[BR_RULE_HASH];
- unsigned short rule_tag[BR_TAG_MAX];
- unsigned short rule_any;
- /* ::before/::after content strings (attr arena offsets); attr(x) is
- * stored as \001x\002 and expanded per element. */
- unsigned int content_off[BR_MAX_CONTENT];
- unsigned short ncontent;
- unsigned short npe_rules;
- int truncated;
- int plain; /* text/plain: one preformatted text node */
- const char *script_src; /* buffer the script offsets point into */
- struct br_script scripts[BR_MAX_SCRIPTS];
- unsigned short nscripts;
- unsigned short scripts_truncated;
- };
- void br_doc_init(struct br_doc *d);
- /* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
- * style (inheriting from the element's style `elem`) and its generated
- * text. Returns the text length; 0 when there is no content, or when the
- * pseudo-element is display:none or absolutely positioned. */
- unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
- const struct br_style *elem, struct br_style *out,
- char *buf, unsigned int cap);
- void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
- unsigned int cap);
- int br_parse(struct br_doc *d, const char *html, unsigned int len);
- int br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
- unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
- /* External CSS is parsed by the browser after br_parse(); parse the inline
- * <style> blocks last so the cascade order matches the document. */
- void br_set_viewport(unsigned int width);
- void br_set_theme(unsigned short text, unsigned short bg,
- unsigned short panel, unsigned short link);
- void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
- /* CSS colour value (names, #hex, rgb()/rgba()) -> RGB565; 1 on success. */
- int br_parse_color(const char *s, unsigned int len, unsigned short *out);
- void br_parse_inline_css(struct br_doc *d);
- void br_reset_styles(struct br_doc *d);
- /* A node's style: parent inherited values, UA defaults, matching rules, then
- * the inline style attribute. Results are cached in the node until
- * br_reset_styles(). */
- void br_style_for(struct br_doc *d, unsigned int node,
- const struct br_style *parent, struct br_style *out);
- int br_tag_is_block(unsigned int tag);
- int br_tag_is_void(unsigned int tag);
- const char *br_tag_name(unsigned int tag);
- unsigned int br_tag_from_name(const char *name);
- const char *br_bg_image_url(unsigned int idx);
- unsigned int br_root_font_px(void);
- /* @font-face table. Ids are 1-based; the browser resolves URLs against the
- * stored base (the stylesheet URL or the page URL) and loads the faces. */
- int br_font_face_count(void);
- const char *br_font_face_url(int id);
- const char *br_font_face_base(int id);
- const char *br_font_face_family(int id);
- void br_set_font_base(const char *base);
- /* Runtime DOM helpers used by the JavaScript bindings. br_find_id returns
- * node + 1 (0 means no match); br_text_content concatenates the subtree's
- * text nodes into out and returns the (possibly truncated) length. */
- unsigned int br_find_id(struct br_doc *d, const char *id);
- unsigned int br_find_tag(struct br_doc *d, unsigned int tag);
- unsigned int br_text_content(struct br_doc *d, unsigned int node,
- char *out, unsigned int cap);
- int br_set_text_content(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len);
- int br_set_text_node(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len);
- /* Runtime DOM construction and mutation (node handles are index + 1). */
- unsigned int br_create_element(struct br_doc *d, const char *name,
- unsigned int len);
- unsigned int br_create_text(struct br_doc *d, const char *text,
- unsigned int len);
- unsigned int br_create_comment(struct br_doc *d, const char *text,
- unsigned int len);
- unsigned int br_create_fragment(struct br_doc *d);
- unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep);
- int br_append_child(struct br_doc *d, unsigned int parent,
- unsigned int child);
- int br_insert_before(struct br_doc *d, unsigned int parent,
- unsigned int child, unsigned int before);
- int br_remove_child(struct br_doc *d, unsigned int parent,
- unsigned int child);
- unsigned int br_find_child(struct br_doc *d, unsigned int parent,
- unsigned int child);
- const char *br_find_attr(struct br_doc *d, unsigned int node,
- const char *name);
- int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
- const char *value, unsigned int len);
- int br_remove_attr(struct br_doc *d, unsigned int node, const char *name);
- /* Replace a node's children with a parsed HTML fragment. */
- int br_set_inner_html(struct br_doc *d, unsigned int node,
- const char *html, unsigned int len);
- /* Simple selector subset: #id, .class, tag, [attr], [attr=v],
- * [attr^=v], [attr$=v], [attr*=v], compounds and descendant chains. */
- unsigned int br_query_all(struct br_doc *d, unsigned int root,
- const char *selector, unsigned int *out,
- unsigned int max);
- int br_matches(struct br_doc *d, unsigned int node, const char *selector);
- const char *br_href(struct br_doc *d, unsigned int node);
- const char *br_src(struct br_doc *d, unsigned int node);
- const char *br_alt(struct br_doc *d, unsigned int node);
- const char *br_rel(struct br_doc *d, unsigned int node);
- const char *br_value(struct br_doc *d, unsigned int node);
- const char *br_placeholder(struct br_doc *d, unsigned int node);
- const char *br_type(struct br_doc *d, unsigned int node);
- #endif
|