/* 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
#include
static int is_space(char c);
static int is_ident(char c);
/* ---- storage (one document per process) ----
*
* The node/text/attribute arenas are allocated from the ring-3 heap, not
* static BSS: a large article needs several MB and the process image lives
* in the 4 MB user window. heap_reset() on navigation releases them with
* the rest of the page heap. */
static struct br_rule rule_fallback[BR_RULE_FALLBACK];
static char css_store[BR_CSS_SIZE];
static int root_font_px = 16;
/* Distinct background-image URLs per page (app stores give every card an
* icon through an inline style). */
#define BR_BG_URLS 512
/* Background URLs live in one pool: a data: URL (Infima's menu chevron is
* an inline SVG) is far longer than a fixed slot. */
#define BR_BG_URL_POOL (128 * 1024)
#define BR_BG_URL_MAX 8191
static const char *bg_urls[BR_BG_URLS];
static char bg_url_pool[BR_BG_URL_POOL];
static unsigned int bg_url_pool_used;
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);
/* HTML ignores the self-closing flag on a
* normal element (`` opens a div); only
* SVG content (and unknown/custom tags, kept
* as before) honours it. */
if (selfclose && tag != BR_TAG_UNKNOWN &&
tag != BR_TAG_SVG &&
!stack_has_svg(d, stack, sp))
selfclose = 0;
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);
if (!d->nodes || !d->text || !d->attrs)
return -1;
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);
if (!d->nodes || !d->text || !d->attrs)
return 0;
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;
}