browser_html.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /* Text-node flag: whitespace was preserved (<pre>). */
  24. #define BR_NODE_PREFORMAT 1
  25. /* Element flag: the HTML hidden attribute is present. */
  26. #define BR_NODE_HIDDEN 2
  27. /* Element flag: width_attr/height_attr hold an SVG viewBox. */
  28. #define BR_NODE_VIEWBOX 4
  29. /* Extra attribute presence flags (data-collapse-*, state attributes).
  30. * Stored separately because struct br_style/selector matching needs them
  31. * but the node flags byte is already used. */
  32. #define BR_ATTR_COLLAPSE_TARGET 0x01
  33. #define BR_ATTR_COLLAPSE_TRIGGER 0x02
  34. #define BR_ATTR_IS_OPEN 0x04
  35. #define BR_ATTR_DISABLED 0x08
  36. #define BR_ATTR_OPEN 0x10
  37. #define BR_ATTR_INERT 0x20
  38. #define BR_ATTR_ANCHOR_MIDDLE 0x40
  39. #define BR_ATTR_ANCHOR_END 0x80
  40. /* Display values in struct br_style. */
  41. #define BR_DISPLAY_INLINE 0
  42. #define BR_DISPLAY_BLOCK 1
  43. #define BR_DISPLAY_NONE 2
  44. #define BR_DISPLAY_FLEX 3
  45. #define BR_DISPLAY_GRID 4
  46. #define BR_DISPLAY_INLINE_BLOCK 5
  47. /* Text alignment. */
  48. #define BR_ALIGN_LEFT 0
  49. #define BR_ALIGN_CENTER 1
  50. #define BR_ALIGN_RIGHT 2
  51. enum br_tag {
  52. BR_TAG_UNKNOWN = 0,
  53. BR_TAG_HTML, BR_TAG_HEAD, BR_TAG_BODY, BR_TAG_TITLE, BR_TAG_STYLE,
  54. BR_TAG_SCRIPT, BR_TAG_META, BR_TAG_LINK, BR_TAG_BASE, BR_TAG_NOSCRIPT,
  55. BR_TAG_P, BR_TAG_DIV, BR_TAG_SPAN, BR_TAG_A, BR_TAG_B, BR_TAG_STRONG,
  56. BR_TAG_I, BR_TAG_EM, BR_TAG_U, BR_TAG_CODE, BR_TAG_FONT, BR_TAG_SMALL,
  57. BR_TAG_BIG, BR_TAG_BR, BR_TAG_HR, BR_TAG_IMG, BR_TAG_PICTURE,
  58. BR_TAG_H1, BR_TAG_H2, BR_TAG_H3, BR_TAG_H4, BR_TAG_H5, BR_TAG_H6,
  59. BR_TAG_UL, BR_TAG_OL, BR_TAG_LI, BR_TAG_BLOCKQUOTE, BR_TAG_PRE,
  60. BR_TAG_CENTER, BR_TAG_TABLE, BR_TAG_THEAD, BR_TAG_TBODY, BR_TAG_TFOOT,
  61. BR_TAG_TR, BR_TAG_TD, BR_TAG_TH, BR_TAG_CAPTION,
  62. BR_TAG_FORM, BR_TAG_INPUT, BR_TAG_BUTTON, BR_TAG_LABEL, BR_TAG_SELECT,
  63. BR_TAG_OPTION, BR_TAG_TEXTAREA, BR_TAG_IFRAME, BR_TAG_SVG,
  64. BR_TAG_VIDEO, BR_TAG_AUDIO, BR_TAG_CANVAS, BR_TAG_OBJECT,
  65. BR_TAG_MAIN, BR_TAG_HEADER, BR_TAG_FOOTER, BR_TAG_NAV, BR_TAG_SECTION,
  66. BR_TAG_ARTICLE, BR_TAG_ASIDE, BR_TAG_FIGURE, BR_TAG_FIGCAPTION,
  67. BR_TAG_DETAILS, BR_TAG_SUMMARY, BR_TAG_DL, BR_TAG_DT, BR_TAG_DD,
  68. BR_TAG_ADDRESS, BR_TAG_MARK, BR_TAG_TIME, BR_TAG_ABBR, BR_TAG_CITE,
  69. BR_TAG_Q, BR_TAG_S, BR_TAG_STRIKE, BR_TAG_SUB, BR_TAG_SUP,
  70. BR_TAG_BGSOUND, BR_TAG_EMBED, BR_TAG_PARAM, BR_TAG_SOURCE,
  71. BR_TAG_TRACK, BR_TAG_WBR, BR_TAG_AREA, BR_TAG_COL, BR_TAG_COLGROUP
  72. };
  73. struct br_style {
  74. unsigned short color;
  75. unsigned short bg;
  76. unsigned short border_color;
  77. unsigned char has_bg;
  78. unsigned char font_px; /* 8..48 */
  79. unsigned char bold;
  80. unsigned char italic;
  81. unsigned char mono;
  82. unsigned char underline;
  83. unsigned char align; /* BR_ALIGN_* */
  84. unsigned char display; /* BR_DISPLAY_* */
  85. unsigned char vis_hidden, opacity_hidden, overflow_hidden;
  86. unsigned char float_left; /* float: left/right */
  87. unsigned char flex_grow; /* flex-grow, 0..100 */
  88. unsigned char flex_col; /* flex-direction: column */
  89. unsigned char flex_wrap; /* flex-wrap: wrap */
  90. unsigned char grid_ncols; /* 1 or 2 supported */
  91. unsigned char grid_gap; /* px */
  92. unsigned char grid_slot; /* 0 auto, 1 full, 2 left, 3 right */
  93. unsigned char grid_auto; /* repeat(auto-fit, minmax(width, 1fr)) */
  94. unsigned char pos; /* 0 static, 1 absolute, 2 fixed */
  95. unsigned char left_set, top_set;
  96. signed short left, top;
  97. unsigned char no_bullet; /* list-style: none (inherited) */
  98. unsigned char uppercase; /* text-transform: uppercase (inherited) */
  99. unsigned char border_w;
  100. /* border-radius in pixels, one entry per corner: TL, TR, BR, BL.
  101. * Percentages are stored as 255 so they clamp to the box half. */
  102. unsigned char radius[4];
  103. unsigned char width_kind; /* 0 none, 1 px, 2 percent */
  104. unsigned short width;
  105. unsigned char max_width_kind;
  106. unsigned short max_width;
  107. unsigned char height_kind;
  108. unsigned short height;
  109. unsigned char line_h_pct; /* percent of font size, 0 = default */
  110. unsigned short line_h_px; /* absolute px, 0 = default */
  111. signed short mt, mb, ml, mr;
  112. signed short pt, pb, pl, pr;
  113. };
  114. struct br_node {
  115. unsigned char type;
  116. unsigned char tag;
  117. unsigned char flags;
  118. unsigned char attr_flags;
  119. unsigned short parent;
  120. unsigned short first;
  121. unsigned short last;
  122. unsigned short next;
  123. unsigned short text_off; /* text arena offset (text nodes) */
  124. unsigned short text_len;
  125. unsigned int href_off;
  126. unsigned int class_off;
  127. unsigned int id_off;
  128. unsigned int style_off;
  129. unsigned int src_off;
  130. unsigned int alt_off;
  131. unsigned int color_off;
  132. unsigned int rel_off;
  133. unsigned int name_off; /* raw tag name for custom elements */
  134. unsigned short width_attr;
  135. unsigned short height_attr;
  136. unsigned char width_pct;
  137. unsigned char height_pct;
  138. short img_index; /* -1 until the image is loaded */
  139. signed short svg_x, svg_y; /* inline SVG x/y or line x1/y1 */
  140. unsigned char svg_rx; /* inline SVG rect corner radius */
  141. struct br_style style; /* computed-style cache */
  142. unsigned char style_valid;
  143. };
  144. struct br_decl {
  145. unsigned char prop;
  146. unsigned char important;
  147. short val;
  148. };
  149. struct br_rule {
  150. unsigned int sel_off; /* selector text in the attr arena */
  151. unsigned int key_off; /* rightmost compound, for a fast prefilter */
  152. unsigned short spec; /* selector specificity */
  153. unsigned char nd;
  154. unsigned char single; /* one compound, no ancestor walk needed */
  155. struct br_decl d[BR_DECLS];
  156. };
  157. /* An inline <script> body. off/len index script_src, the buffer passed to
  158. * br_parse(); the caller must keep it alive for as long as the document (the
  159. * browser keeps the page body until the next navigation). */
  160. struct br_script {
  161. unsigned int off; /* inline body, or empty for src scripts */
  162. unsigned int len;
  163. unsigned int src_off; /* src attribute in the attr arena, 0 if inline */
  164. };
  165. struct br_doc {
  166. struct br_node *nodes;
  167. unsigned int nnode;
  168. unsigned int root;
  169. char *text;
  170. unsigned int text_used;
  171. char *attrs;
  172. unsigned int attrs_used;
  173. struct br_rule *rules;
  174. unsigned int nrules;
  175. unsigned int rule_cap;
  176. char title[BR_TITLE_MAX];
  177. unsigned int inline_css_len;
  178. /* CSS custom properties, stored in the attr arena. A pragmatic
  179. * single table: the last definition wins, which matches :root. */
  180. unsigned int var_name[BR_MAX_VARS];
  181. unsigned int var_val[BR_MAX_VARS];
  182. unsigned short var_slot[1024];
  183. unsigned int nvars;
  184. /* Rule index: rightmost class/id/tag -> linked list of rule indices. */
  185. unsigned short rule_next[BR_MAX_RULES];
  186. unsigned short rule_bucket[BR_RULE_HASH];
  187. unsigned short rule_tag[BR_TAG_MAX];
  188. unsigned short rule_any;
  189. int truncated;
  190. int plain; /* text/plain: one preformatted text node */
  191. const char *script_src; /* buffer the script offsets point into */
  192. struct br_script scripts[BR_MAX_SCRIPTS];
  193. unsigned short nscripts;
  194. unsigned short scripts_truncated;
  195. };
  196. void br_doc_init(struct br_doc *d);
  197. void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
  198. unsigned int cap);
  199. int br_parse(struct br_doc *d, const char *html, unsigned int len);
  200. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
  201. unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
  202. /* External CSS is parsed by the browser after br_parse(); parse the inline
  203. * <style> blocks last so the cascade order matches the document. */
  204. void br_set_viewport(unsigned int width);
  205. void br_set_theme(unsigned short text, unsigned short bg,
  206. unsigned short panel, unsigned short link);
  207. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
  208. void br_parse_inline_css(struct br_doc *d);
  209. void br_reset_styles(struct br_doc *d);
  210. /* A node's style: parent inherited values, UA defaults, matching rules, then
  211. * the inline style attribute. Results are cached in the node until
  212. * br_reset_styles(). */
  213. void br_style_for(struct br_doc *d, unsigned int node,
  214. const struct br_style *parent, struct br_style *out);
  215. int br_tag_is_block(unsigned int tag);
  216. int br_tag_is_void(unsigned int tag);
  217. const char *br_tag_name(unsigned int tag);
  218. unsigned int br_tag_from_name(const char *name);
  219. /* Runtime DOM helpers used by the JavaScript bindings. br_find_id returns
  220. * node + 1 (0 means no match); br_text_content concatenates the subtree's
  221. * text nodes into out and returns the (possibly truncated) length. */
  222. unsigned int br_find_id(struct br_doc *d, const char *id);
  223. unsigned int br_text_content(struct br_doc *d, unsigned int node,
  224. char *out, unsigned int cap);
  225. int br_set_text_content(struct br_doc *d, unsigned int node,
  226. const char *text, unsigned int len);
  227. const char *br_href(struct br_doc *d, unsigned int node);
  228. const char *br_src(struct br_doc *d, unsigned int node);
  229. const char *br_alt(struct br_doc *d, unsigned int node);
  230. const char *br_rel(struct br_doc *d, unsigned int node);
  231. const char *br_value(struct br_doc *d, unsigned int node);
  232. const char *br_placeholder(struct br_doc *d, unsigned int node);
  233. const char *br_type(struct br_doc *d, unsigned int node);
  234. #endif