Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions pkg/html/shim/larva_html.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
// The C side of pkg/html: the few lexbor calls that are inline functions, macros or
// struct reads, which Odin cannot bind directly, plus the one hook the incremental
// parser needs (see larva_html_begin). Everything else is bound from Odin
// (pkg/html/lexbor.odin). Built by scripts/build-lexbor.sh.

#include <lexbor/css/css.h>
#include <lexbor/dom/dom.h>
#include <lexbor/html/html.h>
#include <lexbor/selectors/selectors.h>
#include <stdlib.h>

// One per document being parsed. The tree builder installs its own token callback on
// the tokenizer when chunk parsing begins; we wrap it so we see every token first.
typedef struct {
lxb_html_tokenizer_token_f orig;
void *orig_ctx;
lxb_html_tree_t *tree;
lxb_dom_node_t *script; // the <script> whose end tag was just tokenized, else NULL
size_t closed; // script end tags seen since the last take; > 1 means one was lost
lxb_dom_node_t *script;
size_t closed;
} larva_html_hook;

// --- CSS selectors ---------------------------------------------------------------
//
// A compiled selector list owns its memory: the CSS parser starts with none
// (lxb_css_parser_init leaves parser->memory NULL), so each lxb_css_selectors_parse
// creates a fresh one and hands it to the list it returns. Lists therefore outlive the
// engine that compiled them and are freed one by one with larva_css_free.

typedef struct {
lxb_css_parser_t *parser;
lxb_selectors_t *selectors;
} larva_css;

// Called per match in tree order; return true to stop the search.
typedef bool (*larva_css_found_f)(lxb_dom_node_t *node, void *ctx);

typedef struct {
larva_css_found_f found;
void *ctx;
} larva_css_find_ctx;

// Runs BEFORE the tree builder handles the token. For a script end tag in the text
// insertion mode the tree's current node is that <script> element; the tree pops it
// right after, so this is the one moment it can be captured. A `</script>` that is not
// closing a script element (inside a comment, in plain text) never reaches here as an
// end tag with that current node, so it is ignored.
static lxb_html_token_t *larva_hook_cb(lxb_html_tokenizer_t *tkz, lxb_html_token_t *token, void *ctx) {
larva_html_hook *h = ctx;

Expand All @@ -59,8 +39,6 @@ static lxb_html_token_t *larva_hook_cb(lxb_html_tokenizer_t *tkz, lxb_html_token
return h->orig(tkz, token, h->orig_ctx);
}

// Starts chunked parsing of `doc` and installs the hook. NULL on failure; free with
// larva_html_hook_free after the document's parse has ended.
larva_html_hook *larva_html_begin(lxb_html_document_t *doc) {
if (lxb_html_document_parse_chunk_begin(doc) != LXB_STATUS_OK) {
return NULL;
Expand All @@ -85,9 +63,6 @@ larva_html_hook *larva_html_begin(lxb_html_document_t *doc) {
return h;
}

// Returns the script element closed since the last call, and clears it. *closed is
// how many script end tags were seen in that time: the parser cuts its slices so that
// it is never more than 1, and treats more as a failure rather than lose a script.
lxb_dom_node_t *larva_html_take_script(larva_html_hook *h, size_t *closed) {
lxb_dom_node_t *s = h->script;
*closed = h->closed;
Expand All @@ -104,8 +79,6 @@ lxb_dom_node_t *larva_html_document_node(lxb_html_document_t *doc) {
return lxb_dom_interface_node(doc);
}

// textContent of `node`. The bytes belong to the document; release them with
// larva_html_text_free.
lxb_char_t *larva_html_text(lxb_dom_node_t *node, size_t *len) {
return lxb_dom_node_text_content(node, len);
}
Expand All @@ -116,8 +89,6 @@ void larva_html_text_free(lxb_dom_node_t *node, lxb_char_t *text) {
}
}

// The value of attribute `name` on element `node`, or NULL when absent. Owned by the
// element; valid until the element changes.
const lxb_char_t *larva_html_attr(lxb_dom_node_t *node, const lxb_char_t *name, size_t name_len, size_t *len) {
return lxb_dom_element_get_attribute(lxb_dom_interface_element(node), name, name_len, len);
}
Expand Down Expand Up @@ -146,8 +117,6 @@ larva_css *larva_css_create(void) {
return NULL;
}

// querySelectorAll reports a node once, however many selectors of a list
// (`a, a.x`) it matches; lexbor's default reports it once per selector.
lxb_selectors_opt_set(e->selectors, LXB_SELECTORS_OPT_MATCH_FIRST);
return e;
}
Expand All @@ -162,7 +131,6 @@ void larva_css_destroy(larva_css *e) {
free(e);
}

// NULL when `css` is not a valid selector list.
lxb_css_selector_list_t *larva_css_compile(larva_css *e, const lxb_char_t *css, size_t len) {
lxb_css_selector_list_t *list = lxb_css_selectors_parse(e->parser, css, len);

Expand All @@ -183,16 +151,13 @@ static lxb_status_t larva_css_found(lxb_dom_node_t *node, lxb_css_selector_speci
return f->found(node, f->ctx) ? LXB_STATUS_STOP : LXB_STATUS_OK;
}

// Matches the descendants of `root` (not `root` itself), in tree order. A stop from
// `found` is success, not an error.
bool larva_css_find(larva_css *e, lxb_dom_node_t *root, const lxb_css_selector_list_t *list, larva_css_found_f found,
void *ctx) {
larva_css_find_ctx f = {found, ctx};
lxb_status_t st = lxb_selectors_find(e->selectors, root, list, larva_css_found, &f);
return st == LXB_STATUS_OK || st == LXB_STATUS_STOP || st == LXB_STATUS_STOPPED;
}

// outerHTML of `node`.
lxb_status_t larva_html_serialize_node(lxb_dom_node_t *node, lxb_html_serialize_cb_f cb, void *ctx) {
return lxb_html_serialize_tree_cb(node, cb, ctx);
}
3 changes: 2 additions & 1 deletion scripts/check-js-graph.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ test('eager: a function-body require does not make its target eager', () => {
// that list equal to what the ratchet computes, so neither can drift alone.
test('eager: the Odin cross-check list matches the computed set', () => {
const odin = readFileSync(join(ROOT, 'cmd', 'lava', 'lazy_builtins_test.odin'), 'utf8');
const m = /STATIC_EAGER :: \[\?\]string\{([^}]*)\}/.exec(odin);
// odinfmt writes `[?]string {`; older text had no space before the brace.
const m = /STATIC_EAGER :: \[\?\]string\s*\{([^}]*)\}/.exec(odin);
assert.ok(m, 'STATIC_EAGER not found in lazy_builtins_test.odin');
const listed = [...m[1].matchAll(/"([^"]+)"/g)].map((x) => x[1]).sort();
assert.deepEqual(listed, eagerModules(ROOT));
Expand Down
9 changes: 6 additions & 3 deletions scripts/fmt-if.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ function formatSource(src, isC) {
const { code, state: next } = scan(line, state, isC);
state = next;
const indent = line.match(/^\s*/)[0];
const spread = !isC && !startState.raw && !startState.block ? spreadOneLineBlock(line, code, indent) : null;
const spread =
!isC && !startState.raw && !startState.block ? spreadOneLineBlock(line, code, indent) : null;
out.push(...(spread ?? [line]));
}

Expand Down Expand Up @@ -162,11 +163,13 @@ function formatSource(src, isC) {
if (!seenOpen && !isC && / do /.test(src2[j])) break; // a `do` form odinfmt left
if (j - i > 2000) break;
}
if (end + 1 < src2.length && !isBlank(src2[end + 1]) && !closesOrLabels(src2[end + 1])) blankAfter.add(end);
if (end + 1 < src2.length && !isBlank(src2[end + 1]) && !closesOrLabels(src2[end + 1]))
blankAfter.add(end);
}

for (let i = 0; i < src2.length; i++) {
if (blankBefore.has(i) && !(result.length && isBlank(result[result.length - 1]))) result.push('');
if (blankBefore.has(i) && !(result.length && isBlank(result[result.length - 1])))
result.push('');
result.push(src2[i]);
if (blankAfter.has(i)) result.push('');
}
Expand Down
Loading