Skip to content

The headless browser for AI agents and web scraping

Merge main and preserve inherited document base URLs

SGavrl8ba7aed82c31
diff --cc crates/obscura-js/src/ops.rs
index 9ac5b184,281ed4e1..6e3cb024
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@@ -6465,15 -6698,19 +6698,14 @@@ fn op_waapi_control(state: &OpState, id
changed
}
-// Not tied to `render`: the JS layer resolves every relative URL through here, in all build
-// variants.
+// Rendering and JS URL reflection share one document-generation cache. Geometry
+// and image getters must not run a whole-document selector on every read.
- #[cfg(feature = "render")]
pub(crate) fn document_base_url(state: &ObscuraState) -> Option<String> {
- let document_url = url::Url::parse(state.about_base_url.as_deref().unwrap_or(&state.url)).ok()?;
- let base_href = state.dom.as_ref().and_then(|dom| {
- dom.query_selector("base[href]")
- .ok()
- .flatten()
- .and_then(|id| {
- dom.get_node(id)
- .and_then(|node| node.get_attribute("href").map(str::to_string))
- })
- });
+ base_values_memoized(state).0
+}
+
+fn resolve_document_base_url(document_url: &str, base_href: Option<&str>) -> Option<String> {
+ let document_url = url::Url::parse(document_url).ok()?;
match base_href {
// https://html.spec.whatwg.org/multipage/semantics.html#set-the-frozen-base-url
// A data: or javascript: base falls back to the document URL. Accepting it would instead
@@@ -6517,24 -6754,22 +6749,25 @@@ pub struct BaseUrlCache
/// Both base values behind a cache. Uncached, each one walks the tree and runs the selector
/// engine, which would make `a.href` an O(nodes) read.
fn base_values_memoized(state: &ObscuraState) -> (Option<String>, Option<String>) {
++ let document_url = state.about_base_url.as_deref().unwrap_or(&state.url);
if let Some(cached) = state.base_url_cache.borrow().as_ref() {
if cached.activity_generation == state.activity_generation
&& cached.document_generation == state.document_generation
-- && cached.url == state.url
++ && cached.url == document_url
{
return (cached.resolved.clone(), cached.raw_href.clone());
}
}
- let resolved = document_base_url(state);
+ // Resolve both forms from the same first <base href>. Keep the existing
+ // conservative mutation key so reordering/removal and URL changes are seen.
let raw_href = document_base_href(state);
- let resolved = resolve_document_base_url(&state.url, raw_href.as_deref());
++ let resolved = resolve_document_base_url(document_url, raw_href.as_deref());
*state.base_url_cache.borrow_mut() = Some(BaseUrlCache {
activity_generation: state.activity_generation,
document_generation: state.document_generation,
#[cfg(feature = "render")]
task_generation: state.animation_task_generation,
-- url: state.url.clone(),
++ url: document_url.to_string(),
resolved: resolved.clone(),
raw_href: raw_href.clone(),
});