browser_html.h 14 KB

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