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

Code before:
                );
                handler.handle_element(
                    anonymous_info,
                    display_inline,
                    Contents::Replaced(contents),
                    anonymous_info.node.box_slot(),
                )
            },
        }
    }
}

impl Contents {
    /// Returns true iff the `try_from` impl below would return `Err(_)`
    pub fn is_replaced(&self) -> bool {
        matches!(self, Contents::Replaced(_))
    }

    pub(crate) fn for_element(
        node: ServoThreadSafeLayoutNode<'_>,
        context: &LayoutContext,
    ) -> Self {
        // If `content` is a single image URL, the box gets replaced with a
        // replaced image.
        if let Content::Items(GenericContentItems { items, .. }) =
            node.style(&context.style_context).clone_content()
        {
            if let [GenericContentItem::Image(img)] = items.as_slice() {
                match ReplacedContents::from_image(node, context, img) {
                    Some(replaced) => return Self::Replaced(replaced),
                    // Invalid images are treated as zero-sized.
                    None => {
                        return Self::Replaced(ReplacedContents::zero_sized_invalid_image(node));
                    },
                }
            }
        }

        if let Some(replaced) = ReplacedContents::for_element(node, context) {
            return Self::Replaced(replaced);
        }
        let is_widget = matches!(
            node.type_id(),
            Some(LayoutNodeType::Element(
                LayoutElementType::HTMLInputElement |
                    LayoutElementType::HTMLSelectElement |
                    LayoutElementType::HTMLTextAreaElement
            ))
        );
        if is_widget {
            Self::Widget(NonReplacedContents::OfElement)

Review comment (refactor):
This means that `content: <image>` will take precedence on elements that would otherwise be widgets or be naturally replaced.

But other browsers agree that it should only take precedence is it was going to be an image:


```html
<!DOCTYPE html>
<base href="https://www.software.hixie.ch/utilities/js/live-dom-viewer/"></base>
<style>body > * { content: url('image') }</style>
<img src="null">
<video poster="null"></video>
<iframe src="null"></iframe>
<svg>
  <image href="null">
</svg>
<canvas></canvas>
<input>
<script>
var image = new Image();
image.src = "null";
image.addEventListener("load", () => document.querySelector("canvas").getContext("2d").drawImage(image, 0, 0, 100, 100));
</script>
```

So I think you need to move this logic into `ReplacedContents::for_element` to handle it correctly.

Code after:
    ) -> Self {
        if let Some(replaced) = ReplacedContents::for_element(node, context) {
            return Self::Replaced(replaced);
        }
        let is_widget = matches!(
            node.type_id(),
            Some(LayoutNodeType::Element(
                LayoutElementType::HTMLInputElement |
                    LayoutElementType::HTMLSelectElement |
                    LayoutElementType::HTMLTextAreaElement
            ))
        );
        if is_widget {
            Self::Widget(NonReplacedContents::OfElement)
        } else {
            Self::NonReplaced(NonReplacedContents::OfElement)
        }
    }

    pub(crate) fn for_pseudo_element(contents: Vec<PseudoElementContentItem>) -> Self {
        Self::NonReplaced(NonReplacedContents::OfPseudoElement(contents))
    }

    pub(crate) fn non_replaced_contents(self) -> Option<NonReplacedContents> {
        match self {
            Self::NonReplaced(contents) | Self::Widget(contents) => Some(contents),
            Self::Replaced(_) => None,
        }
    }
}

impl NonReplacedContents {
    pub(crate) fn traverse<'dom>(
        self,
        context: &LayoutContext,
        info: &NodeAndStyleInfo<'dom>,
        handler: &mut impl TraversalHandler<'dom>,
    ) {
        match self {
            NonReplacedContents::OfElement => traverse_children_of(info, context, handler),
            NonReplacedContents::OfPseudoElement(items) => {
                traverse_pseudo_element_contents(info, context, handler, items)
            },
        }
    }
}

fn get_quote_from_pair<I, S>(item: &ContentItem<I>, opening: &S, closing: &S) -> String
where
    S: ToString + ?Sized,
{