Repository: servo/servo
File: components/layout/replaced.rs (Rust)

Code before:
                            .queue_svg_element_for_serialization(node);
                        return None;
                    },
                    Some(Err(_)) => {
                        // Don't attempt to serialize if previous attempt had errored.
                        return None;
                    },
                    Some(Ok(svg_source)) => svg_source,
                };

                let result = context
                    .image_resolver
                    .get_cached_image_for_url(
                        node.opaque(),
                        svg_source,
                        LayoutImageDestination::BoxTreeConstruction,
                    )
                    .ok();

                let vector_image = result.map(|result| match result {
                    Image::Vector(vector_image) => vector_image,
                    _ => unreachable!("SVG element can't contain a raster image."),
                });

                let rule_cache_conditions = &mut RuleCacheConditions::default();
                let parent_style = &node.selected_style();
                let style_builder = StyleBuilder::new(
                    context.style_context.stylist.device(),
                    Some(context.style_context.stylist),
                    Some(parent_style),
                    None,
                    None,
                    false,
                );

                let to_computed_context = Context::new(
                    style_builder,
                    QuirksMode::Quirks,
                    rule_cache_conditions,
                    ContainerSizeQuery::none(),
                );

                let attr_to_computed = |attr_val: &AttrValue| {
                    if let AttrValue::Length(_, length) = attr_val {
                        length.to_computed_value(&to_computed_context)
                    } else {
                        None
                    }
                };
                let width = svg_data.width.and_then(attr_to_computed);
                let height = svg_data.height.and_then(attr_to_computed);

Review comment (bug):
I'm pretty sure `selected_style` is wrong here, but we should get someone more familiar with layout to look over this code.

Code after:
                }
                let natural_size = NaturalSizes {
                    width: None,
                    // 40px is the height of the controls.
                    // See /components/script/resources/media-controls.css
                    height: Some(Au::from_px(40)),
                    ratio: None,
                };
                (ReplacedContentKind::Audio, natural_size)
            }
        };

        if let ReplacedContentKind::Image(Some(Image::Raster(ref image)), _) = kind {
            context
                .image_resolver
                .handle_animated_image(node.opaque(), image.clone());
        }

        Some(Self {
            kind,
            natural_size,
            base_fragment_info: node.into(),
        })
    }

    fn svg_kind_size(
        svg_data: SVGElementData,
        context: &LayoutContext,
        node: ServoThreadSafeLayoutNode<'_>,
    ) -> Option<(ReplacedContentKind, NaturalSizes)> {
        let rule_cache_conditions = &mut RuleCacheConditions::default();

        let parent_style = node.style(&context.style_context);
        let style_builder = StyleBuilder::new(
            context.style_context.stylist.device(),
            Some(context.style_context.stylist),
            Some(&parent_style),
            None,
            None,
            false,
        );

        let to_computed_context = Context::new(
            style_builder,
            context.style_context.quirks_mode(),
            rule_cache_conditions,
            ContainerSizeQuery::none(),
        );

        let attr_to_computed = |attr_val: &AttrValue| {
            if let AttrValue::Length(_, length) = attr_val {