browser_html.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #define BR_MAX_NODES 6144
  9. #define BR_TEXT_SIZE 65536
  10. #define BR_ATTR_SIZE 524288
  11. #define BR_CSS_SIZE 12288
  12. #define BR_MAX_RULES 4608
  13. #define BR_RULE_FALLBACK 256
  14. #define BR_MAX_VARS 512
  15. #define BR_TAG_MAX 128
  16. #define BR_RULE_HASH 256
  17. #define BR_DECLS 16
  18. #define BR_TITLE_MAX 96
  19. #define BR_MAX_SCRIPTS 32
  20. /* Node types. */
  21. #define BR_NODE_TEXT 0
  22. #define BR_NODE_ELEM 1
  23. #define BR_NODE_COMMENT 2
  24. #define BR_NODE_FRAGMENT 3
  25. /* Text-node flag: whitespace was preserved (<pre>). */
  26. #define BR_NODE_PREFORMAT 1
  27. /* Element flag: the HTML hidden attribute is present. */
  28. #define BR_NODE_HIDDEN 2
  29. /* Element flag: width_attr/height_attr hold an SVG viewBox. */
  30. #define BR_NODE_VIEWBOX 4
  31. /* Node is not part of the document tree (created or removed at runtime). */
  32. #define BR_NODE_DETACHED 8
  33. /* Extra attribute presence flags (data-collapse-*, state attributes).
  34. * Stored separately because struct br_style/selector matching needs them
  35. * but the node flags byte is already used. */
  36. #define BR_ATTR_COLLAPSE_TARGET 0x01
  37. #define BR_ATTR_COLLAPSE_TRIGGER 0x02
  38. #define BR_ATTR_IS_OPEN 0x04
  39. #define BR_ATTR_DISABLED 0x08
  40. #define BR_ATTR_OPEN 0x10
  41. #define BR_ATTR_INERT 0x20
  42. #define BR_ATTR_ANCHOR_MIDDLE 0x40
  43. #define BR_ATTR_ANCHOR_END 0x80
  44. /* Display values in struct br_style. */
  45. #define BR_DISPLAY_INLINE 0
  46. #define BR_DISPLAY_BLOCK 1
  47. #define BR_DISPLAY_NONE 2
  48. #define BR_DISPLAY_FLEX 3
  49. #define BR_DISPLAY_GRID 4
  50. #define BR_DISPLAY_INLINE_BLOCK 5
  51. /* Text alignment. */
  52. #define BR_ALIGN_LEFT 0
  53. #define BR_ALIGN_CENTER 1
  54. #define BR_ALIGN_RIGHT 2
  55. enum br_tag {
  56. BR_TAG_UNKNOWN = 0,
  57. BR_TAG_HTML, BR_TAG_HEAD, BR_TAG_BODY, BR_TAG_TITLE, BR_TAG_STYLE,
  58. BR_TAG_SCRIPT, BR_TAG_META, BR_TAG_LINK, BR_TAG_BASE, BR_TAG_NOSCRIPT,
  59. BR_TAG_P, BR_TAG_DIV, BR_TAG_SPAN, BR_TAG_A, BR_TAG_B, BR_TAG_STRONG,
  60. BR_TAG_I, BR_TAG_EM, BR_TAG_U, BR_TAG_CODE, BR_TAG_FONT, BR_TAG_SMALL,
  61. BR_TAG_BIG, BR_TAG_BR, BR_TAG_HR, BR_TAG_IMG, BR_TAG_PICTURE,
  62. BR_TAG_H1, BR_TAG_H2, BR_TAG_H3, BR_TAG_H4, BR_TAG_H5, BR_TAG_H6,
  63. BR_TAG_UL, BR_TAG_OL, BR_TAG_LI, BR_TAG_BLOCKQUOTE, BR_TAG_PRE,
  64. BR_TAG_CENTER, BR_TAG_TABLE, BR_TAG_THEAD, BR_TAG_TBODY, BR_TAG_TFOOT,
  65. BR_TAG_TR, BR_TAG_TD, BR_TAG_TH, BR_TAG_CAPTION,
  66. BR_TAG_FORM, BR_TAG_INPUT, BR_TAG_BUTTON, BR_TAG_LABEL, BR_TAG_SELECT,
  67. BR_TAG_OPTION, BR_TAG_TEXTAREA, BR_TAG_IFRAME, BR_TAG_SVG,
  68. BR_TAG_VIDEO, BR_TAG_AUDIO, BR_TAG_CANVAS, BR_TAG_OBJECT,
  69. BR_TAG_MAIN, BR_TAG_HEADER, BR_TAG_FOOTER, BR_TAG_NAV, BR_TAG_SECTION,
  70. BR_TAG_ARTICLE, BR_TAG_ASIDE, BR_TAG_FIGURE, BR_TAG_FIGCAPTION,
  71. BR_TAG_DETAILS, BR_TAG_SUMMARY, BR_TAG_DL, BR_TAG_DT, BR_TAG_DD,
  72. BR_TAG_ADDRESS, BR_TAG_MARK, BR_TAG_TIME, BR_TAG_ABBR, BR_TAG_CITE,
  73. BR_TAG_Q, BR_TAG_S, BR_TAG_STRIKE, BR_TAG_SUB, BR_TAG_SUP,
  74. BR_TAG_BGSOUND, BR_TAG_EMBED, BR_TAG_PARAM, BR_TAG_SOURCE,
  75. BR_TAG_TRACK, BR_TAG_WBR, BR_TAG_AREA, BR_TAG_COL, BR_TAG_COLGROUP
  76. };
  77. struct br_style {
  78. unsigned short color;
  79. unsigned short bg;
  80. unsigned short border_c[4]; /* top, right, bottom, left */
  81. unsigned short bg_image; /* 1-based background URL, 0 none */
  82. unsigned char has_bg;
  83. unsigned char font_px; /* 8..48 */
  84. unsigned short font_raw; /* font-size before unit resolution */
  85. unsigned char font_unit; /* 0 inherited, 1 px, 2 em, 3 rem, 4 % */
  86. unsigned char bold;
  87. unsigned char italic;
  88. unsigned char mono;
  89. unsigned char underline;
  90. unsigned char align; /* BR_ALIGN_* */
  91. unsigned char display; /* BR_DISPLAY_* */
  92. unsigned char vis_hidden, opacity_hidden, overflow_hidden;
  93. unsigned char float_left; /* float: left/right */
  94. unsigned char clear; /* 0 none, 1 left, 2 right, 3 both */
  95. unsigned char flex_grow; /* flex-grow, 0..100 */
  96. unsigned char flex_col; /* flex-direction: column */
  97. unsigned char flex_wrap; /* flex-wrap: wrap */
  98. unsigned char justify; /* 0 start, 1 center, 2 end, 3 between, 4 around */
  99. unsigned char align_items; /* 0 start/stretch, 1 center, 2 end */
  100. unsigned char grid_ncols; /* 1 or 2 supported */
  101. unsigned char grid_gap; /* px */
  102. unsigned char grid_slot; /* 0 auto, 1 full, 2 left, 3 right */
  103. unsigned char grid_auto; /* repeat(auto-fit, minmax(width, 1fr)) */
  104. unsigned char pos; /* 0 static, 1 absolute, 2 fixed, 3 relative */
  105. unsigned char left_set, top_set, right_set, bottom_set;
  106. signed short left, top, right, bottom;
  107. signed short z_index;
  108. unsigned char z_set;
  109. signed short tx, ty;
  110. unsigned char transform_set;
  111. unsigned char no_bullet; /* list-style: none (inherited) */
  112. unsigned char list_item; /* li marker is drawn */
  113. unsigned char nowrap; /* white-space: nowrap (inherited) */
  114. unsigned char uppercase; /* text-transform: uppercase (inherited) */
  115. unsigned char border_w[4]; /* top, right, bottom, left */
  116. /* border-radius in pixels, one entry per corner: TL, TR, BR, BL.
  117. * Percentages are stored as 255 so they clamp to the box half. */
  118. unsigned char radius[4];
  119. unsigned char width_kind; /* 0 none, 1 px, 2 percent, 3 calc */
  120. unsigned short width;
  121. signed short width_calc_pct;
  122. signed short width_calc_px;
  123. unsigned char max_width_kind;
  124. unsigned short max_width;
  125. unsigned char min_width_kind;
  126. unsigned short min_width;
  127. unsigned char height_kind;
  128. unsigned short height;
  129. unsigned char min_height_kind;
  130. unsigned short min_height;
  131. unsigned char max_height_kind;
  132. unsigned short max_height;
  133. unsigned char box_sizing; /* 0 content-box, 1 border-box */
  134. unsigned char ml_auto, mr_auto;
  135. unsigned char line_h_pct; /* percent of font size, 0 = default */
  136. unsigned short line_h_px; /* absolute px, 0 = default */
  137. signed short mt, mb, ml, mr;
  138. signed short pt, pb, pl, pr;
  139. /* mt, mr, mb, ml, pt, pr, pb, pl: 0 px, 1 %, 2 em, 3 rem, 4 auto */
  140. unsigned char mp_unit[8];
  141. };
  142. struct br_node {
  143. unsigned char type;
  144. unsigned char tag;
  145. unsigned char flags;
  146. unsigned char attr_flags;
  147. unsigned short parent;
  148. unsigned short first;
  149. unsigned short last;
  150. unsigned short next;
  151. unsigned short text_off; /* text arena offset (text nodes) */
  152. unsigned short text_len;
  153. unsigned int href_off;
  154. unsigned int class_off;
  155. unsigned int id_off;
  156. unsigned int style_off;
  157. unsigned int src_off;
  158. unsigned int alt_off;
  159. unsigned int color_off;
  160. unsigned int rel_off;
  161. unsigned int name_off; /* raw tag name for custom elements */
  162. unsigned int extra_off; /* packed name\0value\0 list for getAttribute */
  163. unsigned short extra_len;
  164. unsigned short width_attr;
  165. unsigned short height_attr;
  166. unsigned char width_pct;
  167. unsigned char height_pct;
  168. unsigned char colspan;
  169. unsigned char rowspan;
  170. short bg_img_index; /* -1 until loaded, -2 failed */
  171. short img_index; /* -1 until the image is loaded */
  172. signed short svg_x, svg_y; /* inline SVG x/y or line x1/y1 */
  173. unsigned char svg_rx; /* inline SVG rect corner radius */
  174. struct br_style style; /* computed-style cache */
  175. unsigned char style_valid;
  176. };
  177. struct br_decl {
  178. unsigned char prop;
  179. unsigned char important;
  180. short val;
  181. };
  182. struct br_rule {
  183. unsigned int sel_off; /* selector text in the attr arena */
  184. unsigned int key_off; /* rightmost compound, for a fast prefilter */
  185. unsigned short spec; /* selector specificity */
  186. unsigned char nd;
  187. unsigned char single; /* one compound, no ancestor walk needed */
  188. struct br_decl d[BR_DECLS];
  189. };
  190. /* An inline <script> body. off/len index script_src, the buffer passed to
  191. * br_parse(); the caller must keep it alive for as long as the document (the
  192. * browser keeps the page body until the next navigation). */
  193. struct br_script {
  194. unsigned int off; /* inline body, or empty for src scripts */
  195. unsigned int len;
  196. unsigned int src_off; /* src attribute in the attr arena, 0 if inline */
  197. };
  198. struct br_doc {
  199. struct br_node *nodes;
  200. unsigned int nnode;
  201. unsigned int root;
  202. char *text;
  203. unsigned int text_used;
  204. char *attrs;
  205. unsigned int attrs_used;
  206. struct br_rule *rules;
  207. unsigned int nrules;
  208. unsigned int rule_cap;
  209. char title[BR_TITLE_MAX];
  210. unsigned int inline_css_len;
  211. /* CSS custom properties, stored in the attr arena. A pragmatic
  212. * single table: the last definition wins, which matches :root. */
  213. unsigned int var_name[BR_MAX_VARS];
  214. unsigned int var_val[BR_MAX_VARS];
  215. unsigned short var_slot[1024];
  216. unsigned int nvars;
  217. /* Rule index: rightmost class/id/tag -> linked list of rule indices. */
  218. unsigned short rule_next[BR_MAX_RULES];
  219. unsigned short rule_bucket[BR_RULE_HASH];
  220. unsigned short rule_tag[BR_TAG_MAX];
  221. unsigned short rule_any;
  222. int truncated;
  223. int plain; /* text/plain: one preformatted text node */
  224. const char *script_src; /* buffer the script offsets point into */
  225. struct br_script scripts[BR_MAX_SCRIPTS];
  226. unsigned short nscripts;
  227. unsigned short scripts_truncated;
  228. };
  229. void br_doc_init(struct br_doc *d);
  230. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  231. unsigned int cap);
  232. int br_parse(struct br_doc *d, const char *html, unsigned int len);
  233. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
  234. unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
  235. /* External CSS is parsed by the browser after br_parse(); parse the inline
  236. * <style> blocks last so the cascade order matches the document. */
  237. void br_set_viewport(unsigned int width);
  238. void br_set_theme(unsigned short text, unsigned short bg,
  239. unsigned short panel, unsigned short link);
  240. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
  241. void br_parse_inline_css(struct br_doc *d);
  242. void br_reset_styles(struct br_doc *d);
  243. /* A node's style: parent inherited values, UA defaults, matching rules, then
  244. * the inline style attribute. Results are cached in the node until
  245. * br_reset_styles(). */
  246. void br_style_for(struct br_doc *d, unsigned int node,
  247. const struct br_style *parent, struct br_style *out);
  248. int br_tag_is_block(unsigned int tag);
  249. int br_tag_is_void(unsigned int tag);
  250. const char *br_tag_name(unsigned int tag);
  251. unsigned int br_tag_from_name(const char *name);
  252. const char *br_bg_image_url(unsigned int idx);
  253. unsigned int br_root_font_px(void);
  254. /* Runtime DOM helpers used by the JavaScript bindings. br_find_id returns
  255. * node + 1 (0 means no match); br_text_content concatenates the subtree's
  256. * text nodes into out and returns the (possibly truncated) length. */
  257. unsigned int br_find_id(struct br_doc *d, const char *id);
  258. unsigned int br_find_tag(struct br_doc *d, unsigned int tag);
  259. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  260. char *out, unsigned int cap);
  261. int br_set_text_content(struct br_doc *d, unsigned int node,
  262. const char *text, unsigned int len);
  263. int br_set_text_node(struct br_doc *d, unsigned int node,
  264. const char *text, unsigned int len);
  265. /* Runtime DOM construction and mutation (node handles are index + 1). */
  266. unsigned int br_create_element(struct br_doc *d, const char *name,
  267. unsigned int len);
  268. unsigned int br_create_text(struct br_doc *d, const char *text,
  269. unsigned int len);
  270. unsigned int br_create_comment(struct br_doc *d, const char *text,
  271. unsigned int len);
  272. unsigned int br_create_fragment(struct br_doc *d);
  273. unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep);
  274. int br_append_child(struct br_doc *d, unsigned int parent,
  275. unsigned int child);
  276. int br_insert_before(struct br_doc *d, unsigned int parent,
  277. unsigned int child, unsigned int before);
  278. int br_remove_child(struct br_doc *d, unsigned int parent,
  279. unsigned int child);
  280. unsigned int br_find_child(struct br_doc *d, unsigned int parent,
  281. unsigned int child);
  282. const char *br_find_attr(struct br_doc *d, unsigned int node,
  283. const char *name);
  284. int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
  285. const char *value, unsigned int len);
  286. int br_remove_attr(struct br_doc *d, unsigned int node, const char *name);
  287. /* Replace a node's children with a parsed HTML fragment. */
  288. int br_set_inner_html(struct br_doc *d, unsigned int node,
  289. const char *html, unsigned int len);
  290. /* Simple selector subset: #id, .class, tag, [attr], [attr=v],
  291. * [attr^=v], [attr$=v], [attr*=v], compounds and descendant chains. */
  292. unsigned int br_query_all(struct br_doc *d, unsigned int root,
  293. const char *selector, unsigned int *out,
  294. unsigned int max);
  295. int br_matches(struct br_doc *d, unsigned int node, const char *selector);
  296. const char *br_href(struct br_doc *d, unsigned int node);
  297. const char *br_src(struct br_doc *d, unsigned int node);
  298. const char *br_alt(struct br_doc *d, unsigned int node);
  299. const char *br_rel(struct br_doc *d, unsigned int node);
  300. const char *br_value(struct br_doc *d, unsigned int node);
  301. const char *br_placeholder(struct br_doc *d, unsigned int node);
  302. const char *br_type(struct br_doc *d, unsigned int node);
  303. #endif