browser_html.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* browser_html: a small tolerant HTML tree + CSS cascade for the browser.
  2. *
  3. * Freestanding and independent of ngui: it parses a byte buffer into a static
  4. * node/string arena and resolves the subset of CSS the browser understands.
  5. * One document is live at a time (the browser is single-threaded). */
  6. #ifndef BROWSER_HTML_H
  7. #define BROWSER_HTML_H
  8. #include "cursor_abi.h"
  9. #define BR_CURSOR_AUTO 0
  10. #define BR_MAX_NODES 16384
  11. #define BR_TEXT_SIZE 524288
  12. #define BR_ATTR_SIZE 4194304
  13. #define BR_CSS_SIZE 262144 /* inline <style> text; google.com has ~94 KB */
  14. #define BR_MAX_RULES 8192
  15. #define BR_RULE_FALLBACK 256
  16. #define BR_MAX_FACES 8
  17. #define BR_FACE_FAMILY 40
  18. #define BR_FACE_URL 192
  19. #define BR_MAX_VARS 512
  20. #define BR_MAX_CONTENT 512
  21. #define BR_TAG_MAX 128
  22. #define BR_RULE_HASH 256
  23. #define BR_DECLS 48
  24. #define BR_TITLE_MAX 96
  25. #define BR_MAX_SCRIPTS 32
  26. /* href/src values: google.com links its main script with a ~3.7 KB URL.
  27. * Matches BR_URL_MAX in browser.c and UHTTP_PATH_MAX. */
  28. #define BR_URL_ATTR_MAX 4095
  29. /* Node types. */
  30. #define BR_NODE_TEXT 0
  31. #define BR_NODE_ELEM 1
  32. #define BR_NODE_COMMENT 2
  33. #define BR_NODE_FRAGMENT 3
  34. /* Text-node flag: whitespace was preserved (<pre>). */
  35. #define BR_NODE_PREFORMAT 1
  36. /* Element flag: the HTML hidden attribute is present. */
  37. #define BR_NODE_HIDDEN 2
  38. /* Element flag: width_attr/height_attr hold an SVG viewBox. */
  39. #define BR_NODE_VIEWBOX 4
  40. /* Node is not part of the document tree (created or removed at runtime). */
  41. #define BR_NODE_DETACHED 8
  42. /* Extra attribute presence flags (data-collapse-*, state attributes).
  43. * Stored separately because struct br_style/selector matching needs them
  44. * but the node flags byte is already used. */
  45. #define BR_ATTR_COLLAPSE_TARGET 0x01
  46. #define BR_ATTR_COLLAPSE_TRIGGER 0x02
  47. #define BR_ATTR_IS_OPEN 0x04
  48. #define BR_ATTR_DISABLED 0x08
  49. #define BR_ATTR_OPEN 0x10
  50. #define BR_ATTR_INERT 0x20
  51. #define BR_ATTR_ANCHOR_MIDDLE 0x40
  52. #define BR_ATTR_ANCHOR_END 0x80
  53. /* Display values in struct br_style. */
  54. #define BR_DISPLAY_INLINE 0
  55. #define BR_DISPLAY_BLOCK 1
  56. #define BR_DISPLAY_NONE 2
  57. #define BR_DISPLAY_FLEX 3
  58. #define BR_DISPLAY_GRID 4
  59. #define BR_DISPLAY_INLINE_BLOCK 5
  60. #define BR_GRID_FIXED 16 /* grid_auto: fixed column count base */
  61. /* Decl-only bit: display:inline-flex is FLEX plus st->inline_flex. */
  62. #define BR_DISPLAY_INLINE_FLAG 0x40
  63. /* Text alignment. */
  64. #define BR_ALIGN_LEFT 0
  65. #define BR_ALIGN_CENTER 1
  66. #define BR_ALIGN_RIGHT 2
  67. /* list-style-type; AUTO follows the parent ul/ol. */
  68. #define BR_LIST_AUTO 0
  69. #define BR_LIST_DISC 1
  70. #define BR_LIST_CIRCLE 2
  71. #define BR_LIST_SQUARE 3
  72. #define BR_LIST_DECIMAL 4
  73. #define BR_LIST_LOWER_ALPHA 5
  74. #define BR_LIST_UPPER_ALPHA 6
  75. #define BR_LIST_LOWER_ROMAN 7
  76. #define BR_LIST_UPPER_ROMAN 8
  77. #define BR_LIST_NONE 9
  78. #define BR_BORDER_SOLID 0
  79. #define BR_BORDER_DASHED 1
  80. #define BR_BORDER_DOTTED 2
  81. enum br_tag {
  82. BR_TAG_UNKNOWN = 0,
  83. BR_TAG_HTML, BR_TAG_HEAD, BR_TAG_BODY, BR_TAG_TITLE, BR_TAG_STYLE,
  84. BR_TAG_SCRIPT, BR_TAG_META, BR_TAG_LINK, BR_TAG_BASE, BR_TAG_NOSCRIPT,
  85. BR_TAG_P, BR_TAG_DIV, BR_TAG_SPAN, BR_TAG_A, BR_TAG_B, BR_TAG_STRONG,
  86. BR_TAG_I, BR_TAG_EM, BR_TAG_U, BR_TAG_CODE, BR_TAG_FONT, BR_TAG_SMALL,
  87. BR_TAG_BIG, BR_TAG_BR, BR_TAG_HR, BR_TAG_IMG, BR_TAG_PICTURE,
  88. BR_TAG_H1, BR_TAG_H2, BR_TAG_H3, BR_TAG_H4, BR_TAG_H5, BR_TAG_H6,
  89. BR_TAG_UL, BR_TAG_OL, BR_TAG_LI, BR_TAG_BLOCKQUOTE, BR_TAG_PRE,
  90. BR_TAG_CENTER, BR_TAG_TABLE, BR_TAG_THEAD, BR_TAG_TBODY, BR_TAG_TFOOT,
  91. BR_TAG_TR, BR_TAG_TD, BR_TAG_TH, BR_TAG_CAPTION,
  92. BR_TAG_FORM, BR_TAG_INPUT, BR_TAG_BUTTON, BR_TAG_LABEL, BR_TAG_SELECT,
  93. BR_TAG_OPTION, BR_TAG_TEXTAREA, BR_TAG_IFRAME, BR_TAG_SVG,
  94. BR_TAG_VIDEO, BR_TAG_AUDIO, BR_TAG_CANVAS, BR_TAG_OBJECT,
  95. BR_TAG_MAIN, BR_TAG_HEADER, BR_TAG_FOOTER, BR_TAG_NAV, BR_TAG_SECTION,
  96. BR_TAG_ARTICLE, BR_TAG_ASIDE, BR_TAG_FIGURE, BR_TAG_FIGCAPTION,
  97. BR_TAG_DETAILS, BR_TAG_SUMMARY, BR_TAG_DL, BR_TAG_DT, BR_TAG_DD,
  98. BR_TAG_ADDRESS, BR_TAG_MARK, BR_TAG_TIME, BR_TAG_ABBR, BR_TAG_CITE,
  99. BR_TAG_Q, BR_TAG_S, BR_TAG_STRIKE, BR_TAG_SUB, BR_TAG_SUP,
  100. BR_TAG_BGSOUND, BR_TAG_EMBED, BR_TAG_PARAM, BR_TAG_SOURCE,
  101. BR_TAG_TRACK, BR_TAG_WBR, BR_TAG_AREA, BR_TAG_COL, BR_TAG_COLGROUP,
  102. BR_TAG_TEMPLATE
  103. };
  104. struct br_style {
  105. unsigned short color;
  106. unsigned short bg;
  107. unsigned short border_c[4]; /* top, right, bottom, left */
  108. unsigned short bg_image; /* 1-based background URL, 0 none */
  109. unsigned char bg_repeat; /* 0 repeat, 1 no-repeat, 2 repeat-x, 3 repeat-y */
  110. unsigned char bg_pos_x_kind, bg_pos_y_kind; /* 0 px, 1 percent */
  111. signed short bg_pos_x, bg_pos_y;
  112. unsigned char bg_size_kind; /* 0 auto/stretch, 1 px, 2 percent, 3 cover, 4 contain */
  113. unsigned short bg_size_w, bg_size_h;
  114. unsigned char has_bg;
  115. unsigned char bg_set; /* author set background (colour or transparent) */
  116. unsigned char font_px; /* 8..48 */
  117. unsigned short font_raw; /* font-size before unit resolution */
  118. unsigned char font_unit; /* 0 inherited, 1 px, 2 em, 3 rem, 4 % */
  119. unsigned char bold;
  120. unsigned char italic;
  121. unsigned char mono;
  122. unsigned char font_face; /* 1-based @font-face id, 0 = built-in */
  123. unsigned char underline;
  124. unsigned char strike; /* text-decoration: line-through */
  125. unsigned char overline; /* text-decoration: overline */
  126. unsigned char align; /* BR_ALIGN_* */
  127. unsigned char display; /* BR_DISPLAY_* */
  128. unsigned char vis_hidden, opacity_hidden, overflow_hidden;
  129. unsigned char float_left; /* float: left/right */
  130. unsigned char clear; /* 0 none, 1 left, 2 right, 3 both */
  131. unsigned char flex_grow; /* flex-grow, 0..100 */
  132. unsigned char flex_col; /* flex-direction: column */
  133. unsigned char flex_wrap; /* flex-wrap: wrap */
  134. unsigned char justify; /* 0 start, 1 center, 2 end, 3 between, 4 around */
  135. unsigned char align_items; /* 0 start/stretch, 1 center, 2 end */
  136. unsigned char align_self; /* 0 auto, else align_items + 1 */
  137. unsigned char grid_ncols; /* 1 or 2 supported */
  138. unsigned char grid_gap; /* px */
  139. unsigned char grid_slot; /* 0 auto, 1 full, 2 left, 3 right */
  140. unsigned char grid_auto; /* 1: repeat(auto-fit, minmax(width, 1fr));
  141. * BR_GRID_FIXED + N: N equal columns */
  142. unsigned char pos; /* 0 static, 1 absolute, 2 fixed, 3 relative */
  143. unsigned char left_set, top_set, right_set, bottom_set;
  144. signed short left, top, right, bottom;
  145. signed short z_index;
  146. unsigned char z_set;
  147. signed short tx, ty;
  148. unsigned char transform_set;
  149. unsigned char transform_scale; /* scale()/rotate() paint hint */
  150. signed short scale_x, scale_y; /* hundredths, 100 = 1.0 */
  151. signed short rotate_x100; /* hundredths of a degree */
  152. signed short origin_x, origin_y;
  153. unsigned char origin_x_kind, origin_y_kind; /* 0 px, 1 percent */
  154. unsigned char no_bullet; /* list-style: none (inherited) */
  155. unsigned char list_style; /* BR_LIST_* (inherited from ul/ol) */
  156. unsigned char list_item; /* li marker is drawn */
  157. unsigned char nowrap; /* white-space: nowrap (inherited) */
  158. unsigned char uppercase; /* text-transform: uppercase (inherited) */
  159. unsigned char cursor; /* CSS cursor (inherited): BR_CURSOR_AUTO or
  160. enum cursor_shape + 1 */
  161. unsigned char border_w[4]; /* top, right, bottom, left */
  162. unsigned char border_s[4]; /* BR_BORDER_* per side */
  163. unsigned char border_t[4]; /* 1 = fully transparent border side */
  164. /* border-radius in pixels, one entry per corner: TL, TR, BR, BL.
  165. * Percentages are stored as 255 so they clamp to the box half. */
  166. unsigned char radius[4];
  167. unsigned char width_kind; /* 0 none, 1 px, 2 percent, 3 calc */
  168. unsigned short width;
  169. signed short width_calc_pct;
  170. signed short width_calc_px;
  171. unsigned char max_width_kind;
  172. unsigned short max_width;
  173. unsigned char min_width_kind;
  174. unsigned short min_width;
  175. unsigned char height_kind;
  176. unsigned short height;
  177. signed short height_calc_pct, height_calc_px;
  178. unsigned char min_height_kind;
  179. unsigned short min_height;
  180. signed short min_height_calc_pct, min_height_calc_px;
  181. unsigned char max_height_kind;
  182. unsigned short max_height;
  183. signed short max_height_calc_pct, max_height_calc_px;
  184. signed short min_width_calc_pct, min_width_calc_px;
  185. signed short max_width_calc_pct, max_width_calc_px;
  186. unsigned char box_sizing; /* 0 content-box, 1 border-box */
  187. unsigned char ml_auto, mr_auto;
  188. unsigned char line_h_pct; /* percent of font size, 0 = default */
  189. unsigned short line_h_px; /* absolute px, 0 = default */
  190. signed short mt, mb, ml, mr;
  191. signed short pt, pb, pl, pr;
  192. /* mt, mr, mb, ml, pt, pr, pb, pl: 0 px, 1 %, 2 em, 3 rem, 4 auto,
  193. * 5 calc (the raw value is the percent, mp_calc_px the px part) */
  194. unsigned char mp_unit[8];
  195. signed short mp_calc_px[8];
  196. /* SVG paint (inherited, like CSS fill/stroke): BR_PAINT_* modes.
  197. * stroke_w is in 1/16 user units; 0 means the default 1. */
  198. unsigned char fill_mode, stroke_mode, fill_evenodd;
  199. unsigned short fill, stroke;
  200. unsigned short stroke_w;
  201. unsigned char inline_flex; /* display:inline-flex (not inherited) */
  202. unsigned short content; /* ::before/::after content: index + 1 */
  203. unsigned short aspect_w, aspect_h; /* aspect-ratio (x100), 0 = auto */
  204. };
  205. #define BR_PAINT_UNSET 0 /* fill: black, stroke: none */
  206. #define BR_PAINT_COLOR 1
  207. #define BR_PAINT_NONE 2
  208. #define BR_PAINT_CURRENT 3 /* currentColor -> style color */
  209. #define BR_PAINT_URL 4 /* url(#id): resolved from the attribute */
  210. struct br_node {
  211. unsigned char type;
  212. unsigned char tag;
  213. unsigned char flags;
  214. unsigned char attr_flags;
  215. unsigned short parent;
  216. unsigned short first;
  217. unsigned short last;
  218. unsigned short next;
  219. unsigned int text_off; /* text arena offset (text nodes) */
  220. unsigned short text_len;
  221. unsigned int href_off;
  222. unsigned int class_off;
  223. unsigned int id_off;
  224. unsigned int style_off;
  225. unsigned int src_off;
  226. unsigned int alt_off;
  227. unsigned int color_off;
  228. unsigned int rel_off;
  229. unsigned int name_off; /* raw tag name for custom elements */
  230. unsigned int extra_off; /* packed name\0value\0 list for getAttribute */
  231. unsigned short extra_len;
  232. unsigned short width_attr;
  233. unsigned short height_attr;
  234. unsigned char width_pct;
  235. unsigned char height_pct;
  236. unsigned char colspan;
  237. unsigned char rowspan;
  238. short bg_img_index; /* -1 until loaded, -2 failed */
  239. short img_index; /* -1 until the image is loaded */
  240. signed short svg_x, svg_y; /* inline SVG x/y or line x1/y1 */
  241. unsigned char svg_rx; /* inline SVG rect corner radius */
  242. struct br_style style; /* computed-style cache */
  243. unsigned char style_valid;
  244. unsigned char pe_state; /* br_pseudo() cache: 1/4 checked, 2/8 has */
  245. };
  246. struct br_decl {
  247. unsigned char prop;
  248. unsigned char important;
  249. short val;
  250. };
  251. struct br_rule {
  252. unsigned int sel_off; /* selector text in the attr arena */
  253. unsigned int key_off; /* rightmost compound, for a fast prefilter */
  254. unsigned short spec; /* selector specificity */
  255. unsigned char nd;
  256. unsigned char single; /* one compound, no ancestor walk needed */
  257. unsigned char pe; /* 0 element, 1 ::before, 2 ::after */
  258. struct br_decl d[BR_DECLS];
  259. };
  260. /* An inline <script> body. off/len index script_src, the buffer passed to
  261. * br_parse(); the caller must keep it alive for as long as the document (the
  262. * browser keeps the page body until the next navigation). */
  263. struct br_script {
  264. unsigned int off; /* inline body, or empty for src scripts */
  265. unsigned int len;
  266. unsigned int src_off; /* src attribute in the attr arena, 0 if inline */
  267. unsigned int module; /* type="module": run as an ES module */
  268. unsigned int node; /* the <script> element (document.currentScript) */
  269. };
  270. struct br_doc {
  271. struct br_node *nodes;
  272. unsigned int nnode;
  273. unsigned int root;
  274. char *text;
  275. unsigned int text_used;
  276. char *attrs;
  277. unsigned int attrs_used;
  278. struct br_rule *rules;
  279. unsigned int nrules;
  280. unsigned int rule_cap;
  281. char title[BR_TITLE_MAX];
  282. unsigned int inline_css_len;
  283. /* CSS custom properties, stored in the attr arena. A pragmatic
  284. * single table: the last definition wins, which matches :root. */
  285. unsigned int var_name[BR_MAX_VARS];
  286. unsigned int var_val[BR_MAX_VARS];
  287. /* BR_VAR_ROOT: set by a :root, html, body or * rule; BR_VAR_CONFLICT: two
  288. * component rules disagree, so var() uses its fallback. */
  289. unsigned char var_flags[BR_MAX_VARS];
  290. unsigned short var_slot[1024];
  291. unsigned int nvars;
  292. /* Rule index: rightmost class/id/tag -> linked list of rule indices. */
  293. unsigned short rule_next[BR_MAX_RULES];
  294. unsigned short rule_bucket[BR_RULE_HASH];
  295. unsigned short rule_tag[BR_TAG_MAX];
  296. unsigned short rule_any;
  297. /* ::before/::after content strings (attr arena offsets); attr(x) is
  298. * stored as \001x\002 and expanded per element. */
  299. unsigned int content_off[BR_MAX_CONTENT];
  300. unsigned short ncontent;
  301. unsigned short npe_rules;
  302. int truncated;
  303. int plain; /* text/plain: one preformatted text node */
  304. const char *script_src; /* buffer the script offsets point into */
  305. struct br_script scripts[BR_MAX_SCRIPTS];
  306. unsigned short nscripts;
  307. unsigned short scripts_truncated;
  308. };
  309. void br_doc_init(struct br_doc *d);
  310. /* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
  311. * style (inheriting from the element's style `elem`) and its generated
  312. * text. Returns the text length; 0 when there is no content, or when the
  313. * pseudo-element is display:none or absolutely positioned. */
  314. unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
  315. const struct br_style *elem, struct br_style *out,
  316. char *buf, unsigned int cap);
  317. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  318. unsigned int cap);
  319. int br_parse(struct br_doc *d, const char *html, unsigned int len);
  320. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
  321. unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
  322. /* External CSS is parsed by the browser after br_parse(); parse the inline
  323. * <style> blocks last so the cascade order matches the document. */
  324. void br_set_viewport(unsigned int width);
  325. void br_set_theme(unsigned short text, unsigned short bg,
  326. unsigned short panel, unsigned short link);
  327. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
  328. /* CSS colour value (names, #hex, rgb()/rgba()) -> RGB565; 1 on success. */
  329. int br_parse_color(const char *s, unsigned int len, unsigned short *out);
  330. void br_parse_inline_css(struct br_doc *d);
  331. void br_reset_styles(struct br_doc *d);
  332. /* A node's style: parent inherited values, UA defaults, matching rules, then
  333. * the inline style attribute. Results are cached in the node until
  334. * br_reset_styles(). */
  335. void br_style_for(struct br_doc *d, unsigned int node,
  336. const struct br_style *parent, struct br_style *out);
  337. int br_tag_is_block(unsigned int tag);
  338. int br_tag_is_void(unsigned int tag);
  339. const char *br_tag_name(unsigned int tag);
  340. unsigned int br_tag_from_name(const char *name);
  341. const char *br_bg_image_url(unsigned int idx);
  342. unsigned int br_root_font_px(void);
  343. /* @font-face table. Ids are 1-based; the browser resolves URLs against the
  344. * stored base (the stylesheet URL or the page URL) and loads the faces. */
  345. int br_font_face_count(void);
  346. const char *br_font_face_url(int id);
  347. const char *br_font_face_base(int id);
  348. const char *br_font_face_family(int id);
  349. void br_set_font_base(const char *base);
  350. /* Runtime DOM helpers used by the JavaScript bindings. br_find_id returns
  351. * node + 1 (0 means no match); br_text_content concatenates the subtree's
  352. * text nodes into out and returns the (possibly truncated) length. */
  353. unsigned int br_find_id(struct br_doc *d, const char *id);
  354. unsigned int br_find_tag(struct br_doc *d, unsigned int tag);
  355. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  356. char *out, unsigned int cap);
  357. int br_set_text_content(struct br_doc *d, unsigned int node,
  358. const char *text, unsigned int len);
  359. int br_set_text_node(struct br_doc *d, unsigned int node,
  360. const char *text, unsigned int len);
  361. /* Runtime DOM construction and mutation (node handles are index + 1). */
  362. unsigned int br_create_element(struct br_doc *d, const char *name,
  363. unsigned int len);
  364. unsigned int br_create_text(struct br_doc *d, const char *text,
  365. unsigned int len);
  366. unsigned int br_create_comment(struct br_doc *d, const char *text,
  367. unsigned int len);
  368. unsigned int br_create_fragment(struct br_doc *d);
  369. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep);
  370. int br_append_child(struct br_doc *d, unsigned int parent,
  371. unsigned int child);
  372. int br_insert_before(struct br_doc *d, unsigned int parent,
  373. unsigned int child, unsigned int before);
  374. int br_remove_child(struct br_doc *d, unsigned int parent,
  375. unsigned int child);
  376. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  377. unsigned int child);
  378. const char *br_find_attr(struct br_doc *d, unsigned int node,
  379. const char *name);
  380. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  381. const char *value, unsigned int len);
  382. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name);
  383. /* Replace a node's children with a parsed HTML fragment. */
  384. int br_set_inner_html(struct br_doc *d, unsigned int node,
  385. const char *html, unsigned int len);
  386. /* Simple selector subset: #id, .class, tag, [attr], [attr=v],
  387. * [attr^=v], [attr$=v], [attr*=v], compounds and descendant chains. */
  388. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  389. const char *selector, unsigned int *out,
  390. unsigned int max);
  391. int br_matches(struct br_doc *d, unsigned int node, const char *selector);
  392. const char *br_href(struct br_doc *d, unsigned int node);
  393. const char *br_src(struct br_doc *d, unsigned int node);
  394. const char *br_alt(struct br_doc *d, unsigned int node);
  395. const char *br_rel(struct br_doc *d, unsigned int node);
  396. const char *br_value(struct br_doc *d, unsigned int node);
  397. const char *br_placeholder(struct br_doc *d, unsigned int node);
  398. const char *br_type(struct br_doc *d, unsigned int node);
  399. #endif