/* browser_html: see browser_html.h. A deliberately tolerant parser: bad
* nesting is repaired, unknown tags are transparent, and the CSS engine only
* implements the properties in the browser's documented subset. */
#include "browser_html.h"
#include
static int is_space(char c);
static int is_ident(char c);
/* ---- storage (one document per process) ---- */
static struct br_node nodes_store[BR_MAX_NODES];
static char text_store[BR_TEXT_SIZE];
static char attr_store[BR_ATTR_SIZE];
static struct br_rule rule_fallback[BR_RULE_FALLBACK];
static char css_store[BR_CSS_SIZE];
static int root_font_px = 16;
#define BR_BG_URLS 16
static char bg_urls[BR_BG_URLS][192];
static unsigned int bg_url_count;
/* @font-face definitions, one document at a time. The URL is resolved by
* the browser against `base` (the sheet URL, or the page URL for = 0 ? (unsigned int)p : len;
unsigned int cend = end;
unsigned int idx;
while (cend > i && is_space(html[cend - 1]))
cend--;
if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
for (k = i; k < cend; k++)
css_store[css_len++] = html[k];
css_store[css_len++] = '\n';
}
/* The CSS is collected above, but keep a display:none
* element so getElementsByTagName('style')/parentNode
* behave like a real document. */
idx = node_append(d, stack[sp], BR_NODE_ELEM,
(unsigned char)tag);
if (idx)
parse_attrs(d, &d->nodes[idx],
html + attr_start,
attr_end - attr_start);
i = p >= 0 ? (unsigned int)p + 7 : len;
while (i < len && html[i] != '>')
i++;
if (i < len)
i++;
continue;
}
if (tag == BR_TAG_TITLE) {
int p = find_ci(html, len, "= 0 ? (unsigned int)p : len;
unsigned int tn = 0;
char tmp[BR_TITLE_MAX];
tn = decode_text(html + i, end - i, tmp,
sizeof(tmp), 1);
(void)tn;
s_copy(d->title, tmp, sizeof(d->title));
i = p >= 0 ? (unsigned int)p + 7 : len;
while (i < len && html[i] != '>')
i++;
if (i < len)
i++;
continue;
}
if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
tag == BR_TAG_IMG || br_tag_is_void(tag)) {
unsigned int idx;
implicit_close(d, stack, &sp, tag);
idx = node_append(d, stack[sp], BR_NODE_ELEM,
(unsigned char)tag);
if (idx)
parse_attrs(d, &d->nodes[idx],
html + attr_start,
attr_end - attr_start);
continue;
}
if (sp >= 63) {
d->truncated = 1;
continue;
}
implicit_close(d, stack, &sp, tag);
{
unsigned int idx = node_append(d, stack[sp],
BR_NODE_ELEM,
(unsigned char)tag);
if (!idx)
continue;
parse_attrs(d, &d->nodes[idx],
html + attr_start,
attr_end - attr_start);
if (tag == BR_TAG_UNKNOWN)
d->nodes[idx].name_off =
store_attr(d, name, n, 31);
if (!selfclose) {
sp++;
stack[sp] = (unsigned short)idx;
if (tag == BR_TAG_PRE)
pre++;
}
}
}
}
d->inline_css_len = css_len;
return 0;
}
int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
{
if (!d->nodes)
br_doc_init(d);
doc_reset(d);
d->plain = 1;
d->nodes[0].type = BR_NODE_ELEM;
d->nodes[0].tag = BR_TAG_UNKNOWN;
append_text(d, 0, text, len, 1);
return 0;
}
/* A standalone image response: a synthetic document with one
. The
* caller decodes the already-fetched body into image slot 0 and sets the
* returned node's img_index. */
unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
{
unsigned int idx;
if (!d->nodes)
br_doc_init(d);
doc_reset(d);
d->nodes[0].type = BR_NODE_ELEM;
d->nodes[0].tag = BR_TAG_UNKNOWN;
idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
if (!idx)
return 0;
d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
return idx;
}