browser_html.h 7.3 KB

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