browser_html.h 8.0 KB

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