browser_html.h 17 KB

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