Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

Expose Element current CSS zoom

ldm00195915706b2
diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rs
index d47c8fd17..33d6f128a 100644
--- a/moli-renderer-v8/src/native_bridge/element.rs
+++ b/moli-renderer-v8/src/native_bridge/element.rs
@@ -335,7 +335,7 @@ pub(crate) use forms::{
text_control_set_selection_range_internal,
text_control_set_selection_range_with_direction_internal, text_control_value,
};
-use rendered_state::node_check_visibility_callback;
+use rendered_state::{node_check_visibility_callback, node_current_css_zoom_getter_function};
#[derive(WebApiFunctionTemplate)]
#[webapi(name = "HTMLFormElement", enumerable)]
@@ -932,6 +932,12 @@ struct ExtendedElementPrototypeMethodsDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(name = "Element")]
struct ElementGeometryPrototypeDeclaration {
+ #[webapi(
+ accessor_property = "currentCSSZoom",
+ enumerable,
+ getter = node_current_css_zoom_getter_function
+ )]
+ current_css_zoom: (),
#[webapi(
accessor_property = "clientWidth",
enumerable,
diff --git a/moli-renderer-v8/src/native_bridge/element/rendered_state.rs b/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
index d8c660065..6a4b48255 100644
--- a/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
+++ b/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
@@ -13,8 +13,9 @@ use crate::{
use super::{
super::node::{
- node_runtime_and_handle_from_args_or_detached, require_element_method_receiver,
- throw_incompatible_method_receiver,
+ node_is_element, node_runtime_and_handle_from_args_or_detached,
+ node_runtime_and_handle_from_object_or_detached, require_element_method_receiver,
+ throw_incompatible_getter_receiver, throw_incompatible_method_receiver,
},
styles::StyleObservation,
};
@@ -33,9 +34,10 @@ pub(super) enum ElementContentVisibility {
Auto,
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct ElementRenderedStyle {
pub(super) display: ComputedDisplayKind,
+ effective_zoom: f32,
pub(super) visibility_visible: bool,
pub(super) content_visibility: ElementContentVisibility,
pub(super) content_visibility_applicable: bool,
@@ -49,10 +51,14 @@ impl ElementRenderedStyle {
pub(super) fn read_in_scope(scope: &mut StyleObservation<'_>, handle: DomHandle) -> Self {
let runtime = scope.runtime();
let style = scope.read(handle);
+ let effective_zoom = style
+ .computed_values()
+ .map_or(1.0, |values| values.effective_zoom.value());
let content_visibility = inline_content_visibility(runtime, handle);
if let Some(facts) = style.rendered_style_facts() {
return Self {
display: facts.display,
+ effective_zoom,
visibility_visible: facts.visibility_visible,
content_visibility,
content_visibility_applicable: facts.content_visibility_applicable,
@@ -66,6 +72,7 @@ impl ElementRenderedStyle {
let display = computed_display_kind(&display_value);
Self {
display,
+ effective_zoom,
visibility_visible: style.property("visibility") == "visible",
content_visibility,
content_visibility_applicable: fallback_content_visibility_applicable(&display_value),
@@ -307,6 +314,19 @@ impl ElementRenderedState {
self.path.first().and_then(|entry| entry.style.as_ref())
}
+ /// Returns the cumulative CSS zoom of the target's generated box.
+ ///
+ /// CSSOM View exposes `1` when the element has no associated box. Reading
+ /// the value from this style-observation path keeps it synchronous without
+ /// coupling the API to a retained screenshot geometry snapshot.
+ fn current_css_zoom(&self) -> f64 {
+ if self.box_state() != ElementBoxState::Box {
+ return 1.0;
+ }
+ self.target_style()
+ .map_or(1.0, |style| f64::from(style.effective_zoom))
+ }
+
fn has_zero_opacity(&self) -> bool {
self.path
.iter()
@@ -458,6 +478,29 @@ pub(super) fn node_check_visibility_callback<'s>(
rv.set_bool(true);
}
+pub(super) fn node_current_css_zoom_getter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let Ok((runtime_ptr, handle)) =
+ node_runtime_and_handle_from_object_or_detached(scope, args.this())
+ else {
+ throw_incompatible_getter_receiver(scope, "Element", "currentCSSZoom");
+ rv.set(v8::Number::new(scope, 1.0).into());
+ return;
+ };
+ let runtime = unsafe { &*runtime_ptr };
+ if !node_is_element(runtime, handle) {
+ throw_incompatible_getter_receiver(scope, "Element", "currentCSSZoom");
+ rv.set(v8::Number::new(scope, 1.0).into());
+ return;
+ }
+
+ let zoom = ElementRenderedState::read(runtime, handle).current_css_zoom();
+ rv.set(v8::Number::new(scope, zoom).into());
+}
+
fn computed_opacity_is_zero(value: &str) -> bool {
let value = value.trim();
if let Some(percent) = value.strip_suffix('%') {
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index a814ee4ad..4806eb27c 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -1,5 +1,84 @@
use super::*;
+#[test]
+fn element_current_css_zoom_observes_fresh_effective_style_for_rendered_boxes() {
+ let mut vm = new_parsed_test_vm(
+ "https://element-current-css-zoom.test/",
+ r#"<!doctype html><html><body>
+ <div id="unzoomed"><div id="unzoomed-child"></div></div>
+ <div id="outer" style="zoom: 2">
+ <div id="inner" style="zoom: 2">
+ <div id="rendered-child"></div>
+ <div id="non-rendered-child" style="display: none"></div>
+ </div>
+ </div>
+ <div id="hidden-ancestor" style="display: none; zoom: 3">
+ <div id="hidden-descendant" style="zoom: 2"></div>
+ </div>
+ </body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const get = id => document.getElementById(id);
+ const initial = [
+ get('unzoomed').currentCSSZoom,
+ get('outer').currentCSSZoom,
+ get('inner').currentCSSZoom,
+ get('rendered-child').currentCSSZoom,
+ get('non-rendered-child').currentCSSZoom,
+ get('hidden-descendant').currentCSSZoom
+ ];
+ get('unzoomed').style.zoom = 2;
+ const updated = [
+ get('unzoomed').currentCSSZoom,
+ get('unzoomed-child').currentCSSZoom
+ ];
+ get('outer').style.display = 'none';
+ const suppressed = [
+ get('outer').currentCSSZoom,
+ get('inner').currentCSSZoom,
+ get('rendered-child').currentCSSZoom
+ ];
+ const detached = document.createElement('div');
+ detached.style.zoom = 4;
+ const descriptor = Object.getOwnPropertyDescriptor(
+ Element.prototype,
+ 'currentCSSZoom'
+ );
+ let incompatible = 'none';
+ try {
+ descriptor.get.call({});
+ } catch (error) {
+ incompatible = error.name;
+ }
+ return JSON.stringify({
+ initial,
+ updated,
+ suppressed,
+ detached: detached.currentCSSZoom,
+ shape: [
+ typeof descriptor.get,
+ descriptor.get.length,
+ descriptor.enumerable,
+ descriptor.configurable,
+ Object.hasOwn(get('outer'), 'currentCSSZoom')
+ ],
+ incompatible
+ });
+})()
+"#,
+ )
+ .expect("Element.currentCSSZoom should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"initial":[1,2,4,4,1,1],"updated":[2,2],"suppressed":[1,1,1],"detached":1,"shape":["function",0,true,true,false],"incompatible":"TypeError"}"#
+ );
+}
+
fn inspector_active_child_window_scope_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
_args: v8::FunctionCallbackArguments<'s>,