ps-blitz-script 0.5.0

JavaScript execution for Blitz using the Boa engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Realm-local document input streams.

use std::cell::{Cell, RefCell};
use std::collections::HashSet;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;

use blitz_dom::{NodeData, NodeId, local_name};
use blitz_html::DocumentHtmlParser;
use blitz_html::stream::{StreamInput, StreamingParser};
use boa_engine::{
    Context, Finalize, JsData, JsNativeError, JsResult, JsValue, Source, Trace, js_string,
};

use crate::dom::{
    define_accessor, define_method, dom_ctx, js_str, node_id_of_value, node_wrapper, this_node_id,
    to_rust_string,
};
use crate::module::{BlitzModuleLoader, ImportMap, SharedFetcher};
use crate::state::DomCtx;

#[derive(Trace, Finalize, JsData)]
struct InputState {
    #[unsafe_ignore_trace]
    original: RefCell<Option<Arc<str>>>,
    #[unsafe_ignore_trace]
    parser: RefCell<Option<Rc<RefCell<StreamingParser>>>>,
    #[unsafe_ignore_trace]
    boundary_pending: Cell<bool>,
    #[unsafe_ignore_trace]
    script_created: Cell<bool>,
    #[unsafe_ignore_trace]
    restart: Cell<bool>,
    #[unsafe_ignore_trace]
    generation: Cell<usize>,
    #[unsafe_ignore_trace]
    is_xml: Cell<bool>,
    #[unsafe_ignore_trace]
    executed: RefCell<HashSet<NodeId>>,
    #[unsafe_ignore_trace]
    fetcher: RefCell<Option<SharedFetcher>>,
    #[unsafe_ignore_trace]
    loader: Rc<BlitzModuleLoader>,
}

fn state(context: &Context) -> &InputState {
    context
        .get_data::<InputState>()
        .expect("document input state missing")
}

pub(crate) fn install(context: &mut Context, loader: Rc<BlitzModuleLoader>) {
    context.insert_data(InputState {
        original: RefCell::new(None),
        parser: RefCell::new(None),
        boundary_pending: Cell::new(false),
        script_created: Cell::new(false),
        restart: Cell::new(false),
        generation: Cell::new(0),
        is_xml: Cell::new(false),
        executed: RefCell::new(HashSet::new()),
        fetcher: RefCell::new(None),
        loader,
    });
    let ctx = dom_ctx(context).expect("document input needs a DOM context");
    let prototype = ctx.state.borrow().protos().document.clone();
    define_method(&prototype, "open", 0, open, context);
    define_method(&prototype, "write", 0, write, context);
    define_method(&prototype, "writeln", 0, writeln, context);
    define_method(&prototype, "close", 0, close, context);
    define_method(&prototype, "hasFocus", 0, has_focus, context);
    define_accessor(&prototype, "referrer", Some(referrer), None, context);
    define_accessor(&prototype, "contentType", Some(content_type), None, context);
    define_accessor(&prototype, "compatMode", Some(compat_mode), None, context);
}

pub(crate) fn prepare(
    context: &Context,
    html: &str,
    fetcher: SharedFetcher,
    parser: Option<StreamingParser>,
) {
    let input = state(context);
    input
        .is_xml
        .set(DocumentHtmlParser::is_xhtml_document(html));
    input.boundary_pending.set(parser.is_some());
    // Only an unfinished navigation needs a source scan for prefetch. At EOF
    // the live tree is complete, including on pages without scripts.
    *input.original.borrow_mut() = parser.as_ref().map(|_| Arc::from(html));
    *input.parser.borrow_mut() = parser.map(|parser| Rc::new(RefCell::new(parser)));
    *input.fetcher.borrow_mut() = Some(fetcher);
}

pub(crate) fn original(context: &Context) -> Option<Arc<str>> {
    state(context).original.borrow().clone()
}

fn document_receiver(this: &JsValue, context: &mut Context) -> JsResult<(DomCtx, bool)> {
    let id = this_node_id(this)?;
    let ctx = dom_ctx(context)?;
    let main = {
        let document = ctx.doc.borrow();
        if !matches!(
            document.get_node(id).map(|node| &node.data),
            Some(NodeData::Document(_))
        ) {
            return Err(JsNativeError::typ()
                .with_message("Illegal Document receiver")
                .into());
        }
        id == document.root_node().id
    };
    Ok((ctx, main))
}

fn main_document(this: &JsValue, context: &mut Context) -> JsResult<DomCtx> {
    let (ctx, main) = document_receiver(this, context)?;
    if !main {
        return Err(crate::domc::exception(
            "NotSupportedError",
            "Detached document input streams are not supported",
            context,
        ));
    }
    if state(context).is_xml.get() {
        return Err(crate::domc::exception(
            "InvalidStateError",
            "XML documents do not support HTML input streams",
            context,
        ));
    }
    Ok(ctx)
}

fn reset(context: &mut Context, clear_listeners: bool) -> DomCtx {
    let ctx = dom_ctx(context).expect("document input needs a DOM context");
    let children = ctx.doc.borrow().root_node().children.clone();
    for child in children {
        crate::dom::remove_and_free_node(&ctx, child, context);
    }
    ctx.mutate_doc().begin_document_stream();
    if clear_listeners {
        let mut runtime = ctx.state.borrow_mut();
        runtime.node_listeners.clear();
        runtime.window_listeners.clear();
        runtime.pointer_capture.clear();
    }
    state(context).executed.borrow_mut().clear();
    state(context).boundary_pending.set(false);
    let next = state(context).generation.get().wrapping_add(1);
    state(context).generation.set(next);
    crate::domc::set_current_script(context, None);
    crate::domc::set_ready_state(context, "loading");
    ctx
}

pub(crate) fn begin(context: &mut Context) {
    // Construction already positioned the live parser at its first boundary.
    // Keep its nodes, resources and shim attachments. document.open() remains
    // the operation that replaces a document and resets its input stream.
    state(context).original.borrow_mut().take();
}

pub(crate) fn active(context: &Context) -> bool {
    state(context).parser.borrow().is_some()
}

pub(crate) fn generation(context: &Context) -> usize {
    state(context).generation.get()
}

pub(crate) fn take_restart(context: &Context) -> bool {
    state(context).restart.replace(false)
}

pub(crate) fn was_executed(context: &Context, id: NodeId) -> bool {
    state(context).executed.borrow().contains(&id)
}

fn is_current_parser(context: &Context, parser: &Rc<RefCell<StreamingParser>>) -> bool {
    state(context)
        .parser
        .borrow()
        .as_ref()
        .is_some_and(|current| Rc::ptr_eq(current, parser))
}

/// Advance navigation input to its next script boundary.
pub(crate) fn advance(context: &mut Context) -> bool {
    if state(context).script_created.get() {
        return false;
    }
    // Execute the boundary reached during construction before feeding more
    // navigation input, even when that first script is deferred or inert.
    if state(context).boundary_pending.replace(false) {
        return true;
    }
    let parser = state(context).parser.borrow().clone();
    let Some(parser) = parser else {
        return false;
    };
    crate::dom::custom_elements::parser_scope(context, |context| {
        let script = parser.borrow_mut().next_script();
        if script.is_none() {
            parser.borrow_mut().finish();
            state(context).parser.borrow_mut().take();
        }
    });
    true
}

fn open(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    main_document(this, context)?;
    if active(context)
        && (!state(context).script_created.get() || current_script(context)?.is_some())
    {
        return Ok(this.clone());
    }
    state(context).original.borrow_mut().take();
    let ctx = reset(context, true);
    *state(context).parser.borrow_mut() = Some(Rc::new(RefCell::new(StreamingParser::new(
        Rc::clone(&ctx.doc),
        "",
    ))));
    state(context).script_created.set(true);
    state(context).restart.set(true);
    Ok(this.clone())
}

fn current_script(context: &mut Context) -> JsResult<Option<NodeId>> {
    let global = context.global_object().clone();
    let document = global
        .get(js_string!("document"), context)?
        .as_object()
        .ok_or_else(|| JsNativeError::typ().with_message("Document is missing"))?;
    Ok(node_id_of_value(
        &document.get(js_string!("currentScript"), context)?,
    ))
}

fn write_input(
    this: &JsValue,
    arguments: &[JsValue],
    newline: bool,
    context: &mut Context,
) -> JsResult<JsValue> {
    let ctx = main_document(this, context)?;
    let mut text = String::new();
    for argument in arguments {
        text.push_str(&to_rust_string(argument, context)?);
    }
    if newline {
        text.push('\n');
    }
    if !active(context) {
        open(this, &[], context)?;
    }
    let parser = state(context)
        .parser
        .borrow()
        .clone()
        .expect("input stream missing");
    let input = StreamInput::new(&text);
    loop {
        let next = crate::dom::custom_elements::parser_scope(context, |_| {
            parser.borrow_mut().feed_written(&input)
        });
        // Reactions can open, close, or replace the input stream before the
        // script returned by the old parser gets a chance to execute.
        if !is_current_parser(context, &parser) {
            break;
        }
        let Some(script) = next else {
            break;
        };
        execute_written(script, context)?;
        if !is_current_parser(context, &parser) {
            break;
        }
    }
    ctx.mark_layout_dirty();
    ctx.doc.borrow().shell_provider.request_redraw();
    Ok(JsValue::undefined())
}

fn write(this: &JsValue, arguments: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    write_input(this, arguments, false, context)
}

fn writeln(this: &JsValue, arguments: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    write_input(this, arguments, true, context)
}

fn close(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let ctx = main_document(this, context)?;
    if state(context).script_created.get() {
        let parser = state(context).parser.borrow_mut().take();
        state(context).script_created.set(false);
        if let Some(parser) = parser {
            crate::dom::custom_elements::parser_scope(context, |_| {
                parser.borrow_mut().finish();
            });
        }
        ctx.mark_layout_dirty();
        ctx.doc.borrow().shell_provider.request_redraw();
    }
    Ok(JsValue::undefined())
}

fn dispatch_resource_event(
    ctx: &DomCtx,
    id: NodeId,
    name: &str,
    context: &mut Context,
) -> JsResult<()> {
    let node = node_wrapper(ctx, id, context);
    let target: JsValue = node.clone().into();
    let event = crate::dom::event::create_event(ctx, name, false, false, &target, context);
    crate::dom::event::set_event_field(&event, "isTrusted", &JsValue::from(true));
    if let Some(dispatch) = node.get(js_string!("dispatchEvent"), context)?.as_object() {
        dispatch.call(&target, &[event.into()], context)?;
    }
    Ok(())
}

fn execute_written(id: NodeId, context: &mut Context) -> JsResult<()> {
    let ctx = dom_ctx(context)?;
    let (kind, source, text, deferred, base) = {
        let document = ctx.doc.borrow();
        let Some(node) = document.get_node(id) else {
            return Ok(());
        };
        let Some(element) = node.element_data() else {
            return Ok(());
        };
        let kind = element
            .attr(local_name!("type"))
            .unwrap_or("")
            .trim()
            .to_ascii_lowercase();
        let source = element.attr(local_name!("src")).map(str::to_string);
        let deferred = kind == "module"
            || (source.is_some()
                && element.attr(local_name!("defer")).is_some()
                && element.attr(local_name!("async")).is_none());
        if element.attr(local_name!("nomodule")).is_some() {
            return Ok(());
        }
        (
            kind,
            source,
            node.text_content(),
            deferred,
            document.url().clone(),
        )
    };
    if deferred {
        return Ok(());
    }
    if kind == "importmap" {
        let loader = Rc::clone(&state(context).loader);
        loader.set_import_map(ImportMap::parse(&text, Some(&base)));
        state(context).executed.borrow_mut().insert(id);
        return Ok(());
    }
    if !matches!(
        kind.as_str(),
        "" | "text/javascript" | "application/javascript"
    ) {
        return Ok(());
    }
    state(context).executed.borrow_mut().insert(id);
    let external = source.is_some();
    let (code, url) = if let Some(source) = source {
        let url = match base.join(&source) {
            Ok(url) => url,
            Err(_) => {
                dispatch_resource_event(&ctx, id, "error", context)?;
                return Ok(());
            }
        };
        let shared = state(context)
            .fetcher
            .borrow()
            .clone()
            .expect("script fetcher missing");
        let fetcher = Rc::clone(&shared.borrow());
        match fetcher.fetch(&url) {
            Ok(code) => (code, url),
            Err(error) => {
                eprintln!("blitz-script: written script fetch failed: {error}");
                dispatch_resource_event(&ctx, id, "error", context)?;
                return Ok(());
            }
        }
    } else {
        (text, base)
    };
    let previous = current_script(context)?;
    crate::domc::set_current_script(context, Some(id));
    let path = url.to_string();
    let source = Source::from_bytes(code.as_bytes()).with_path(Path::new(&path));
    let result = context.eval(source);
    crate::domc::set_current_script(context, previous);
    if let Err(error) = result {
        eprintln!("blitz-script: written script failed: {error}");
    }
    if external {
        dispatch_resource_event(&ctx, id, "load", context)?;
    }
    Ok(())
}

fn has_focus(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let (ctx, main) = document_receiver(this, context)?;
    Ok(JsValue::from(main && ctx.doc.borrow().window_focused()))
}

fn referrer(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let (ctx, main) = document_receiver(this, context)?;
    let document = ctx.doc.borrow();
    Ok(js_str(if main { document.referrer() } else { "" }))
}

fn content_type(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let (ctx, main) = document_receiver(this, context)?;
    let doc = ctx.doc.borrow();
    if main {
        return Ok(js_str(doc.content_type()));
    }
    // A detached document (DOMParser, createHTMLDocument) keeps its own type.
    match doc.get_node(this_node_id(this)?).map(|node| &node.data) {
        Some(NodeData::Document(data)) => Ok(js_str(data.content_type)),
        _ => Ok(js_str(doc.content_type())),
    }
}

fn compat_mode(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let id = this_node_id(this)?;
    let (ctx, _) = document_receiver(this, context)?;
    let document = ctx.doc.borrow();
    let quirks = matches!(
        document.get_node(id).map(|node| &node.data),
        Some(NodeData::Document(data)) if data.quirks_mode == 2
    );
    Ok(js_str(if quirks { "BackCompat" } else { "CSS1Compat" }))
}