dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / svg / svg_svg_element.lucb
#==============================================================================================
#
# svg_svg_element - SVG::SVGSVGElement: <svg>, an SVG viewport
#
# DESCRIPTION:
# Port of LibWeb/SVG/SVGSVGElement.cpp (lines 1-335) with the inline members of
# SVGSVGElement.h: construction (an SVGGraphicsElement with the SVGFitToViewBox
# mixin), its layout box, the width and height presentation attributes as style values
# (cached), the active viewBox (a <view> element, the viewBox attribute, or for SVG
# used as an image a fallback from width and height), the stubbed SVG DOM members, and
# the natural metrics of an SVG root (https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS).
#
# The caches `mutable Optional<RefPtr<StyleValue const>>` are a value and a `_set` flag
# each (types_svgsvg_element).
#
#==============================================================================================
# mark: Construction =============================================================================
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:29 Web::SVG::SVGSVGElement::SVGSVGElement.
pub func svg_svgsvg_element_construct(this: SvgSvgsvgElement*, document: DomDocument*, qualified_name: DomQualifiedName):
svg_graphics_element_construct(this.svg_graphics_element(), document, qualified_name)
# The vptrs of the mixin bases move to SVGSVGElement's tables.
let element = this.svg_element()
element.element.slottable_mixin.vtable = &svg_svgsvg_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &svg_svgsvg_element_aria_mixin_vtable
element.element.animatable.vtable = &svg_svgsvg_element_animatable_vtable
element.global_event_handlers.vtable = &svg_svgsvg_element_global_event_handlers_vtable
this.svg_fit_to_view_box.vtable = &svg_svgsvg_element_svg_fit_to_view_box_vtable
this.svg_fit_to_view_box.class_id = this.cell().class_id
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:34 Web::SVG::SVGSVGElement::initialize (override).
pub func svg_svgsvg_element_initialize_impl(base: JsCell*, realm: Realm*):
let this = (SvgSvgsvgElement*)base
web_set_prototype_for_interface(base, realm, BindInterfaceName.svgsvg_element)
svg_graphics_element_initialize_impl(base, realm)
svg_fit_to_view_box_initialize(&this.svg_fit_to_view_box, realm)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:41 Web::SVG::SVGSVGElement::visit_edges (override).
pub func svg_svgsvg_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (SvgSvgsvgElement*)base
svg_element_visit_edges_impl(base, visitor)
svg_fit_to_view_box_visit_edges(&this.svg_fit_to_view_box, visitor)
gc.cell_visitor_visit_t(visitor, this.m_active_view_element)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:48 Web::SVG::SVGSVGElement::create_layout_node (override).
pub func svg_svgsvg_element_create_layout_node_impl(base: DomElement*, style: CssComputedProperties*) -> LayoutNode*?:
let this = (SvgSvgsvgElement*)base
# heap().allocate<Layout::SVGSVGBox>(...): a layout node's initialize is JS::Cell's, which does nothing.
return realm_create_layout_svgsvg_box(dom_element_realm(base), dom_node_document(base.dom_node()), this, style).layout_node()
# mark: width and height =========================================================================
## `parse_css_value(SVGPresentationAttribute context, attribute.value_or(String {}), property_id)`, or, for an
## empty attribute, 100% (width_style_value_from_attribute and height_style_value_from_attribute).
func svg_svgsvg_element_dimension_style_value_from_attribute(this: SvgSvgsvgElement*, attribute_name: ak.FlyString, property_id: css_data.PropertyId) -> CssStyleValue*?:
var parsing_context: CssParsingParams = ---
css_parsing_params_construct_dom_document(&parsing_context, dom_node_document(this.dom_node()), CssParsingMode.svg_presentation_attribute)
let attribute = dom_element_attribute(this.dom_element(), attribute_name)
var result: CssStyleValue*? = none
let text = attribute else ak.String()
if let value = parse_css_value(parsing_context, ak.string_bytes_as_string_view(&text), property_id):
result = value
elif attribute != none and ak.string_is_empty(&text):
# If the `width` attribute is an empty string, it defaults to 100%.
# This matches WebKit and Blink, but not Firefox. The spec is unclear.
# FIXME: Figure out what to do here.
result = css_percentage_style_value_create(css_percentage_new(100.0)).css_style_value()
return result
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:53 Web::SVG::SVGSVGElement::width_style_value_from_attribute.
pub func svg_svgsvg_element_width_style_value_from_attribute(this: SvgSvgsvgElement*) -> CssStyleValue*?:
if this.m_cached_width_style_value_set:
return this.m_cached_width_style_value
let result = svg_svgsvg_element_dimension_style_value_from_attribute(this, svg_attribute_names_width, css_data.PropertyId.width)
this.m_cached_width_style_value = result
this.m_cached_width_style_value_set = true
return result
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:75 Web::SVG::SVGSVGElement::height_style_value_from_attribute.
pub func svg_svgsvg_element_height_style_value_from_attribute(this: SvgSvgsvgElement*) -> CssStyleValue*?:
if this.m_cached_height_style_value_set:
return this.m_cached_height_style_value
let result = svg_svgsvg_element_dimension_style_value_from_attribute(this, svg_attribute_names_height, css_data.PropertyId.height)
this.m_cached_height_style_value = result
this.m_cached_height_style_value_set = true
return result
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:97 Web::SVG::SVGSVGElement::attribute_changed (override).
pub func svg_svgsvg_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
let this = (SvgSvgsvgElement*)base
svg_graphics_element_attribute_changed_impl(base, name, old_value, value, namespace_)
svg_fit_to_view_box_attribute_changed(&this.svg_fit_to_view_box, base, name, value)
if ak.fly_string_equals_ignoring_ascii_case_fly_string(&name, &svg_attribute_names_width):
this.m_cached_width_style_value = none
this.m_cached_width_style_value_set = false
svg_svgsvg_element_update_fallback_view_box_for_svg_as_image(this)
if ak.fly_string_equals_ignoring_ascii_case_fly_string(&name, &svg_attribute_names_height):
this.m_cached_height_style_value = none
this.m_cached_height_style_value_set = false
svg_svgsvg_element_update_fallback_view_box_for_svg_as_image(this)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:112 Web::SVG::SVGSVGElement::children_changed (override).
pub func svg_svgsvg_element_children_changed_impl(base: DomNode*, arg0: DomNodeChildrenChangedMetadata):
let this = (SvgSvgsvgElement*)base
# FIXME: Add support for all types of SVG fragment identifier.
# See: https://svgwg.org/svg2-draft/linking.html#LinksIntoSVG
let url = dom_document_url(dom_node_document(base))
if let fragment = web_url.url_fragment(&url):
if let referenced_element = dom_parent_node_get_element_by_id(this.dom_parent_node(), ak.fly_string_from_string(fragment)):
if let view_element = as_if_svg_view_element(referenced_element):
svg_svgsvg_element_set_active_view_element(this, view_element)
return
svg_svgsvg_element_set_active_view_element(this, none)
## The absolute length in pixels of a width or height attribute's value, if it is one.
func svg_absolute_length_in_px(value: CssStyleValue*?) -> f64?:
let style_value = value else return none
if not css_style_value_is_length(style_value):
return none
let length = css_length_style_value_length(css_style_value_as_length(style_value))
if not css_length_is_absolute(&length):
return none
return css_length_absolute_length_to_px(&length).to_double()
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:127 Web::SVG::SVGSVGElement::update_fallback_view_box_for_svg_as_image.
func svg_svgsvg_element_update_fallback_view_box_for_svg_as_image(this: SvgSvgsvgElement*):
# AD-HOC: This creates a fallback viewBox for SVGs used as images.
# If the <svg> element has width and height, but no viewBox,
# we fall back to a synthetic viewBox="0 0 width height".
let width_attribute = dom_element_get_attribute_value(this.dom_element(), svg_attribute_names_width)
var parsing_context: CssParsingParams = ---
css_parsing_params_construct_dom_document(&parsing_context, dom_node_document(this.dom_node()), CssParsingMode.svg_presentation_attribute)
let width = svg_absolute_length_in_px(parse_css_value(parsing_context, ak.string_bytes_as_string_view(&width_attribute), css_data.PropertyId.width))
let height_attribute = dom_element_get_attribute_value(this.dom_element(), svg_attribute_names_height)
let height = svg_absolute_length_in_px(parse_css_value(parsing_context, ak.string_bytes_as_string_view(&height_attribute), css_data.PropertyId.height))
this.m_fallback_view_box_for_svg_as_image = none
if let used_width = width:
if let used_height = height:
if used_width > 0.0 and used_height > 0.0:
this.m_fallback_view_box_for_svg_as_image = SvgViewBox(min_x = 0.0, min_y = 0.0, width = used_width, height = used_height)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:156 Web::SVG::SVGSVGElement::set_fallback_view_box_for_svg_as_image.
pub func svg_svgsvg_element_set_fallback_view_box_for_svg_as_image(this: SvgSvgsvgElement*, view_box: SvgViewBox?):
this.m_fallback_view_box_for_svg_as_image = view_box
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:161 Web::SVG::SVGSVGElement::active_view_box (override).
pub func svg_svgsvg_element_active_view_box_impl(base: SvgGraphicsElement*) -> SvgViewBox?:
let this = (SvgSvgsvgElement*)base
if let active_view_element = this.m_active_view_element:
if let view_box = svg_fit_to_view_box_view_box(&active_view_element.svg_fit_to_view_box):
return view_box
if let view_box = svg_fit_to_view_box_view_box(&this.svg_fit_to_view_box):
return view_box
# NOTE: If the parent is a document, we're an <svg> element used as an image.
if let parent = tree_node_parent(this.dom_node()):
if dom_node_is_document(parent) and this.m_fallback_view_box_for_svg_as_image != none:
return this.m_fallback_view_box_for_svg_as_image
return none
# mark: SVG DOM ==================================================================================
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:176 Web::SVG::SVGSVGElement::x.
pub func svg_svgsvg_element_x(this: SvgSvgsvgElement*) -> SvgAnimatedLength*:
return svg_element_svg_animated_length_for_property(this.svg_element(), css_data.PropertyId.x)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:181 Web::SVG::SVGSVGElement::y.
pub func svg_svgsvg_element_y(this: SvgSvgsvgElement*) -> SvgAnimatedLength*:
return svg_element_svg_animated_length_for_property(this.svg_element(), css_data.PropertyId.y)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:186 Web::SVG::SVGSVGElement::width.
pub func svg_svgsvg_element_width(this: SvgSvgsvgElement*) -> SvgAnimatedLength*:
return svg_element_svg_animated_length_for_property(this.svg_element(), css_data.PropertyId.width)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:191 Web::SVG::SVGSVGElement::height.
pub func svg_svgsvg_element_height(this: SvgSvgsvgElement*) -> SvgAnimatedLength*:
return svg_element_svg_animated_length_for_property(this.svg_element(), css_data.PropertyId.height)
## `dbgln("(STUBBED) {}. Called on: {}", what, debug_description())`.
func svg_svgsvg_element_dbgln_stubbed(this: SvgSvgsvgElement*, what: str):
let description = dom_node_debug_description(this.dom_node())
ak.dbgln(f"(STUBBED) {what}. Called on: {ak.string_view_text(ak.string_bytes_as_string_view(&description))}")
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:196 Web::SVG::SVGSVGElement::current_scale.
pub func svg_svgsvg_element_current_scale(this: SvgSvgsvgElement*) -> f32:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::current_scale()")
return 1.0
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:202 Web::SVG::SVGSVGElement::set_current_scale.
pub func svg_svgsvg_element_set_current_scale(this: SvgSvgsvgElement*, arg0: f32):
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::set_current_scale()")
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:207 Web::SVG::SVGSVGElement::current_translate.
pub func svg_svgsvg_element_current_translate(this: SvgSvgsvgElement*) -> GeometryDomPointReadOnly*:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::current_translate()")
return geometry_dom_point_read_only_create(dom_element_realm(this.dom_element()))
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:213 Web::SVG::SVGSVGElement::get_intersection_list.
pub func svg_svgsvg_element_get_intersection_list(this: SvgSvgsvgElement*, rect: GeometryDomRectReadOnly*, reference_element: SvgElement*?) -> DomNodeList*:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::get_intersection_list()")
return dom_static_node_list_create(dom_element_realm(this.dom_element()), ak.Vector[DomNode*?]())
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:219 Web::SVG::SVGSVGElement::get_enclosure_list.
pub func svg_svgsvg_element_get_enclosure_list(this: SvgSvgsvgElement*, rect: GeometryDomRectReadOnly*, reference_element: SvgElement*?) -> DomNodeList*:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::get_enclosure_list()")
return dom_static_node_list_create(dom_element_realm(this.dom_element()), ak.Vector[DomNode*?]())
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:225 Web::SVG::SVGSVGElement::check_intersection.
pub func svg_svgsvg_element_check_intersection(this: SvgSvgsvgElement*, element: SvgElement*, rect: GeometryDomRectReadOnly*) -> bool:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::check_intersection()")
return false
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:231 Web::SVG::SVGSVGElement::check_enclosure.
pub func svg_svgsvg_element_check_enclosure(this: SvgSvgsvgElement*, element: SvgElement*, rect: GeometryDomRectReadOnly*) -> bool:
svg_svgsvg_element_dbgln_stubbed(this, "SVGSVGElement::check_enclosure()")
return false
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:237 Web::SVG::SVGSVGElement::deselect_all.
pub func svg_svgsvg_element_deselect_all(this: SvgSvgsvgElement*):
# This is equivalent to calling document.getSelection().removeAllRanges() on the document that this ‘svg’ element is in.
if let selection = dom_document_get_selection(dom_node_document(this.dom_node())):
selection_selection_remove_all_ranges(selection)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:244 Web::SVG::SVGSVGElement::create_svg_length.
pub func svg_svgsvg_element_create_svg_length(this: SvgSvgsvgElement*) -> SvgLength*:
# A new, detached SVGLength object whose value is the unitless <number> 0.
return svg_length_create(dom_element_realm(this.dom_element()), (u8)svg_length_svg_lengthtype_number, 0.0, SvgLengthReadOnly.no)
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:250 Web::SVG::SVGSVGElement::create_svg_point.
pub func svg_svgsvg_element_create_svg_point(this: SvgSvgsvgElement*) -> GeometryDomPoint*:
# A new, detached DOMPoint object whose coordinates are all 0.
return geometry_dom_point_from_point(realm_vm(dom_element_realm(this.dom_element())))
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:256 Web::SVG::SVGSVGElement::create_svg_matrix.
pub func svg_svgsvg_element_create_svg_matrix(this: SvgSvgsvgElement*) -> GeometryDomMatrix*:
# A new, detached DOMMatrix object representing the identity matrix.
return geometry_dom_matrix_create(dom_element_realm(this.dom_element()))
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:262 Web::SVG::SVGSVGElement::create_svg_rect.
pub func svg_svgsvg_element_create_svg_rect(this: SvgSvgsvgElement*) -> GeometryDomRect*:
# A new, DOMRect object whose x, y, width and height are all 0.
return geometry_dom_rect_construct_impl(dom_element_realm(this.dom_element()), 0.0, 0.0, 0.0, 0.0) else trap("FIXME: should propagate errors")
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:268 Web::SVG::SVGSVGElement::create_svg_transform.
pub func svg_svgsvg_element_create_svg_transform(this: SvgSvgsvgElement*) -> SvgSvgTransform*:
return svg_svg_transform_create(dom_element_realm(this.dom_element()))
# mark: Natural metrics ==========================================================================
## negotiate_natural_metrics' aspect_ratio lambda.
func svg_svgsvg_element_natural_aspect_ratio(svg_root: SvgSvgsvgElement*, natural_metrics: const CssSizeWithAspectRatio*) -> gfx.CssPixelFraction?:
# 1. If the width and height sizing properties on the ‘svg’ element are both absolute values:
if let width = natural_metrics.width:
if let height = natural_metrics.height:
if width.m_value != 0 and height.m_value != 0:
# 1. return width / height
return width.div(height)
return none
# 2. If an SVG View is active:
if let active_view_element = svg_svgsvg_element_active_view_element(svg_root):
if let view_box = svg_fit_to_view_box_view_box(&active_view_element.svg_fit_to_view_box):
# 1. let viewbox be the viewbox defined by the active SVG View
# 2. return viewbox.width / viewbox.height
if view_box.width != 0.0 or view_box.height != 0.0:
var fraction: gfx.CssPixelFraction = ---
gfx.css_pixel_fraction_construct_f64(&fraction, view_box.width / view_box.height)
return fraction
return none
# 3. If the ‘viewBox’ on the ‘svg’ element is correctly specified:
if let viewbox = svg_fit_to_view_box_view_box(&svg_root.svg_fit_to_view_box):
# 1. let viewbox be the viewbox defined by the ‘viewBox’ attribute on the ‘svg’ element
# 2. return viewbox.width / viewbox.height
let viewbox_width = gfx.css_pixels_nearest_value_for_f64(viewbox.width)
let viewbox_height = gfx.css_pixels_nearest_value_for_f64(viewbox.height)
if viewbox_width.m_value != 0 and viewbox_height.m_value != 0:
return viewbox_width.div(viewbox_height)
return none
# 4. return null
return none
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.cpp:273 Web::SVG::SVGSVGElement::negotiate_natural_metrics.
pub func svg_svgsvg_element_negotiate_natural_metrics(svg_root: SvgSvgsvgElement*) -> CssSizeWithAspectRatio:
# https://www.w3.org/TR/SVG2/coords.html#SizingSVGInCSS
var natural_metrics = CssSizeWithAspectRatio()
# The intrinsic dimensions must also be determined from the width and height sizing properties.
# If either width or height are not specified, the used value is the initial value 'auto'.
# 'auto' and percentage lengths must not be used to determine an intrinsic width or intrinsic height.
if let width = svg_svgsvg_element_width_style_value_from_attribute(svg_root):
if css_style_value_is_length(width):
let length = css_length_style_value_length(css_style_value_as_length(width))
if css_length_is_absolute(&length):
natural_metrics.width = css_length_absolute_length_to_px(&length)
if let height = svg_svgsvg_element_height_style_value_from_attribute(svg_root):
if css_style_value_is_length(height):
let length = css_length_style_value_length(css_style_value_as_length(height))
if css_length_is_absolute(&length):
natural_metrics.height = css_length_absolute_length_to_px(&length)
# The intrinsic aspect ratio must be calculated using the following algorithm. If the algorithm returns null, then there is no intrinsic aspect ratio.
natural_metrics.aspect_ratio = svg_svgsvg_element_natural_aspect_ratio(svg_root, &natural_metrics)
return natural_metrics
# mark: SVGSVGElement.h ==========================================================================
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:30 Web::SVG::SVGSVGElement::requires_svg_container (override).
pub func svg_svgsvg_element_requires_svg_container_impl(base: DomNode*) -> bool:
return false
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:31 Web::SVG::SVGSVGElement::is_svg_container (override).
pub func svg_svgsvg_element_is_svg_container_impl(base: DomNode*) -> bool:
return true
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:35 Web::SVG::SVGSVGElement::set_active_view_element.
pub func svg_svgsvg_element_set_active_view_element(this: SvgSvgsvgElement*, view_element: SvgViewElement*?):
this.m_active_view_element = view_element
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:63 Web::SVG::SVGSVGElement::suspend_redraw.
# Deprecated methods that have no effect when called, but which are kept for compatibility reasons.
pub func svg_svgsvg_element_suspend_redraw(this: SvgSvgsvgElement*, max_wait_milliseconds: u32) -> u32:
# When the suspendRedraw method is called, it must return 1.
return 1
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:69 Web::SVG::SVGSVGElement::unsuspend_redraw.
pub func svg_svgsvg_element_unsuspend_redraw(this: SvgSvgsvgElement*, suspend_handle_id: u32):
return
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:73 Web::SVG::SVGSVGElement::unsuspend_redraw_all.
pub func svg_svgsvg_element_unsuspend_redraw_all(this: SvgSvgsvgElement*):
return
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:74 Web::SVG::SVGSVGElement::force_redraw.
pub func svg_svgsvg_element_force_redraw(this: SvgSvgsvgElement*):
return
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:87 Web::SVG::SVGSVGElement::is_svg_svg_element (override).
pub func svg_svgsvg_element_is_svg_svg_element_impl(base: DomNode*) -> bool:
return true
## Ported from Libraries/LibWeb/SVG/SVGSVGElement.h:89 Web::SVG::SVGSVGElement::active_view_element.
func svg_svgsvg_element_active_view_element(this: SvgSvgsvgElement*) -> SvgViewElement*?:
return this.m_active_view_element