browser_html.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 2560
  9. #define BR_TEXT_SIZE 32768
  10. #define BR_ATTR_SIZE 131072
  11. #define BR_CSS_SIZE 12288
  12. #define BR_MAX_RULES 1792
  13. #define BR_DECLS 16
  14. #define BR_TITLE_MAX 96
  15. /* Node types. */
  16. #define BR_NODE_TEXT 0
  17. #define BR_NODE_ELEM 1
  18. /* Text-node flag: whitespace was preserved (<pre>). */
  19. #define BR_NODE_PREFORMAT 1
  20. /* Display values in struct br_style. */
  21. #define BR_DISPLAY_INLINE 0
  22. #define BR_DISPLAY_BLOCK 1
  23. #define BR_DISPLAY_NONE 2
  24. /* Text alignment. */
  25. #define BR_ALIGN_LEFT 0
  26. #define BR_ALIGN_CENTER 1
  27. #define BR_ALIGN_RIGHT 2
  28. enum br_tag {
  29. BR_TAG_UNKNOWN = 0,
  30. BR_TAG_HTML, BR_TAG_HEAD, BR_TAG_BODY, BR_TAG_TITLE, BR_TAG_STYLE,
  31. BR_TAG_SCRIPT, BR_TAG_META, BR_TAG_LINK, BR_TAG_BASE, BR_TAG_NOSCRIPT,
  32. BR_TAG_P, BR_TAG_DIV, BR_TAG_SPAN, BR_TAG_A, BR_TAG_B, BR_TAG_STRONG,
  33. BR_TAG_I, BR_TAG_EM, BR_TAG_U, BR_TAG_CODE, BR_TAG_FONT, BR_TAG_SMALL,
  34. BR_TAG_BIG, BR_TAG_BR, BR_TAG_HR, BR_TAG_IMG,
  35. BR_TAG_H1, BR_TAG_H2, BR_TAG_H3, BR_TAG_H4, BR_TAG_H5, BR_TAG_H6,
  36. BR_TAG_UL, BR_TAG_OL, BR_TAG_LI, BR_TAG_BLOCKQUOTE, BR_TAG_PRE,
  37. BR_TAG_CENTER, BR_TAG_TABLE, BR_TAG_THEAD, BR_TAG_TBODY, BR_TAG_TFOOT,
  38. BR_TAG_TR, BR_TAG_TD, BR_TAG_TH, BR_TAG_CAPTION,
  39. BR_TAG_FORM, BR_TAG_INPUT, BR_TAG_BUTTON, BR_TAG_LABEL, BR_TAG_SELECT,
  40. BR_TAG_OPTION, BR_TAG_TEXTAREA, BR_TAG_IFRAME, BR_TAG_SVG,
  41. BR_TAG_VIDEO, BR_TAG_AUDIO, BR_TAG_CANVAS, BR_TAG_OBJECT,
  42. BR_TAG_MAIN, BR_TAG_HEADER, BR_TAG_FOOTER, BR_TAG_NAV, BR_TAG_SECTION,
  43. BR_TAG_ARTICLE, BR_TAG_ASIDE, BR_TAG_FIGURE, BR_TAG_FIGCAPTION,
  44. BR_TAG_DETAILS, BR_TAG_SUMMARY, BR_TAG_DL, BR_TAG_DT, BR_TAG_DD,
  45. BR_TAG_ADDRESS, BR_TAG_MARK, BR_TAG_TIME, BR_TAG_ABBR, BR_TAG_CITE,
  46. BR_TAG_Q, BR_TAG_S, BR_TAG_STRIKE, BR_TAG_SUB, BR_TAG_SUP,
  47. BR_TAG_BGSOUND, BR_TAG_EMBED, BR_TAG_PARAM, BR_TAG_SOURCE,
  48. BR_TAG_TRACK, BR_TAG_WBR, BR_TAG_AREA, BR_TAG_COL, BR_TAG_COLGROUP
  49. };
  50. struct br_style {
  51. unsigned short color;
  52. unsigned short bg;
  53. unsigned short border_color;
  54. unsigned char has_bg;
  55. unsigned char scale; /* 1 or 2 */
  56. unsigned char bold;
  57. unsigned char underline;
  58. unsigned char align; /* BR_ALIGN_* */
  59. unsigned char display; /* BR_DISPLAY_* */
  60. unsigned char float_left; /* float: left/right */
  61. unsigned char no_bullet; /* list-style: none (inherited) */
  62. unsigned char uppercase; /* text-transform: uppercase (inherited) */
  63. unsigned char border_w;
  64. unsigned char width_kind; /* 0 none, 1 px, 2 percent */
  65. unsigned short width;
  66. unsigned char max_width_kind;
  67. unsigned short max_width;
  68. unsigned char height_kind;
  69. unsigned short height;
  70. unsigned char line_h_pct; /* percent of font size, 0 = default */
  71. unsigned short line_h_px; /* absolute px, 0 = default */
  72. signed short mt, mb, ml, mr;
  73. signed short pt, pb, pl, pr;
  74. };
  75. struct br_node {
  76. unsigned char type;
  77. unsigned char tag;
  78. unsigned char flags;
  79. unsigned char spare;
  80. unsigned short parent;
  81. unsigned short first;
  82. unsigned short last;
  83. unsigned short next;
  84. unsigned short text_off; /* text arena offset (text nodes) */
  85. unsigned short text_len;
  86. unsigned short href_off;
  87. unsigned short class_off;
  88. unsigned short id_off;
  89. unsigned short style_off;
  90. unsigned short src_off;
  91. unsigned short alt_off;
  92. unsigned short color_off;
  93. unsigned short rel_off;
  94. unsigned short width_attr;
  95. unsigned short height_attr;
  96. unsigned char width_pct;
  97. unsigned char height_pct;
  98. short img_index; /* -1 until the image is loaded */
  99. struct br_style style; /* computed-style cache */
  100. unsigned char style_valid;
  101. };
  102. struct br_decl {
  103. unsigned char prop;
  104. unsigned char important;
  105. short val;
  106. };
  107. struct br_rule {
  108. unsigned short sel_off; /* selector text in the attr arena */
  109. unsigned char nd;
  110. struct br_decl d[BR_DECLS];
  111. };
  112. struct br_doc {
  113. struct br_node *nodes;
  114. unsigned int nnode;
  115. unsigned int root;
  116. char *text;
  117. unsigned int text_used;
  118. char *attrs;
  119. unsigned int attrs_used;
  120. struct br_rule *rules;
  121. unsigned int nrules;
  122. char title[BR_TITLE_MAX];
  123. unsigned int inline_css_len;
  124. int truncated;
  125. int plain; /* text/plain: one preformatted text node */
  126. };
  127. void br_doc_init(struct br_doc *d);
  128. int br_parse(struct br_doc *d, const char *html, unsigned int len);
  129. int br_parse_plain(struct br_doc *d, const char *text, unsigned int len);
  130. unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
  131. /* External CSS is parsed by the browser after br_parse(); parse the inline
  132. * <style> blocks last so the cascade order matches the document. */
  133. void br_set_viewport(unsigned int width);
  134. void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
  135. void br_parse_inline_css(struct br_doc *d);
  136. void br_reset_styles(struct br_doc *d);
  137. /* A node's style: parent inherited values, UA defaults, matching rules, then
  138. * the inline style attribute. Results are cached in the node until
  139. * br_reset_styles(). */
  140. void br_style_for(struct br_doc *d, unsigned int node,
  141. const struct br_style *parent, struct br_style *out);
  142. int br_tag_is_block(unsigned int tag);
  143. int br_tag_is_void(unsigned int tag);
  144. const char *br_tag_name(unsigned int tag);
  145. unsigned int br_tag_from_name(const char *name);
  146. const char *br_href(struct br_doc *d, unsigned int node);
  147. const char *br_src(struct br_doc *d, unsigned int node);
  148. const char *br_alt(struct br_doc *d, unsigned int node);
  149. const char *br_rel(struct br_doc *d, unsigned int node);
  150. #endif