noonoo-engine 0.21.0

Noonoo's orchestration: a parsed, styled, laid-out engine over a DOM document.
Documentation
//! Subresources: `<img>` sources, loaded through the host's
//! [`Loader`](noonoo_net::Loader) and kept for layout and paint.

use std::collections::{HashMap, HashSet};

use noonoo_layout::{Image, Images};
use noonoo_net::{Completions, Loader, RequestId};

use crate::Engine;

/// The loader, what was asked of it, and what arrived.
#[derive(Default)]
pub(crate) struct Resources {
    pub(crate) loader: Option<Box<dyn Loader + Send + Sync>>,
    pub(crate) base_url: Option<String>,
    pub(crate) completions: Completions,
    pub(crate) sheets: crate::sheets::Sheets,
    pub(crate) fonts: crate::fonts::Fonts,
    /// Each source as written, once requested (loaded, failed or pending).
    requested: HashSet<String>,
    /// Pending requests → the source as written.
    pending: HashMap<RequestId, String>,
    pub(crate) images: Images,
}

impl Engine {
    /// The URL the document's references resolve against (its own URL).
    pub fn set_base_url(&mut self, url: impl Into<String>) {
        self.resources.base_url = Some(url.into());
        // Inline `url()` values resolve against it.
        self.refresh_stylesheets();
    }

    /// The URL references resolve against, if one was set.
    pub fn base_url(&self) -> Option<&str> {
        self.resources.base_url.as_deref()
    }

    /// Load subresources through `loader` from now on. Without one, nothing
    /// is loaded: an engine reaches the filesystem or the network only
    /// when its host says so.
    pub fn set_loader(&mut self, loader: impl Loader + Send + Sync + 'static) {
        self.resources.loader = Some(Box::new(loader));
    }

    /// The queue loads finish into: set a waker on it to hear when to call
    /// [`Engine::poll_resources`].
    pub fn completions(&self) -> &Completions {
        &self.resources.completions
    }

    /// Start loading each `<img src>` and each `<link rel=stylesheet>` not
    /// yet requested, resolved against the base URL; returns how many loads
    /// started. Call it after parsing and after DOM edits that may add
    /// either; sheets an `@import` brings in start as their parent arrives.
    ///
    /// A link is skipped when it is `disabled`, has a `type` other than
    /// `text/css`, an `alternate` `rel`, or only non-screen `media` (such as
    /// `print`). Every load goes through the host's loader, so its policy
    /// covers sheets as it covers images.
    pub fn load_resources(&mut self) -> usize {
        if self.resources.loader.is_none() {
            return 0;
        }
        let mut started = self.load_links();
        started += self.load_fonts();
        let Some(loader) = &self.resources.loader else {
            return started;
        };
        for element in self.dom.get_elements_by_tag_name("img") {
            let Some(src) = self.dom.element(element).and_then(|e| e.attr("src")) else {
                continue;
            };
            let src = src.trim();
            if src.is_empty() || !self.resources.requested.insert(src.to_owned()) {
                continue;
            }
            let url = match &self.resources.base_url {
                Some(base) => noonoo_net::resolve(base, src),
                None => src.to_owned(),
            };
            let (request, completion) = self.resources.completions.request(url);
            self.resources.pending.insert(request.id, src.to_owned());
            loader.start(request, completion);
            started += 1;
        }
        started
    }

    /// Take in the loads that finished; true when an image arrived or the
    /// stylesheets in force changed, so the host must restyle and lay out
    /// again ([`Engine::restyle`] sees the new sheets). Call it when the
    /// [`Engine::completions`] waker fires. A load that failed or isn't an
    /// image keeps its element's `alt` text; a sheet that failed changes
    /// nothing; neither is retried.
    pub fn poll_resources(&mut self) -> bool {
        let mut changed = false;
        let mut sheets_changed = false;
        for loaded in self.resources.completions.drain() {
            if self.resources.fonts.owns(loaded.id) {
                changed |= self.font_arrived(loaded.id, loaded.result);
                continue;
            }
            if self.resources.sheets.owns(loaded.id) {
                let index = self.resources.sheets.take(loaded.id);
                sheets_changed |= self.sheet_arrived(index, loaded.result);
                continue;
            }
            let Some(src) = self.resources.pending.remove(&loaded.id) else {
                continue;
            };
            if let Some(image) = loaded.result.ok().and_then(Image::from_bytes) {
                self.resources.images.insert(src, image);
                changed = true;
            }
        }
        if sheets_changed {
            self.refresh_stylesheets();
            // A new sheet may declare faces.
            self.load_fonts();
        }
        changed || sheets_changed
    }

    /// The images loaded so far, by `src` as written.
    pub fn images(&self) -> &Images {
        &self.resources.images
    }
}