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

Code before:
    }
}

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(image)] = items.as_slice() {
                match ReplacedContents::from_image(node, context, image) {
                    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)
        } else {
            Self::NonReplaced(NonReplacedContents::OfElement)
        }
    }

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


Review comment (suggestion):
A bit shorter:

```suggestion
                // Invalid images are treated as zero-sized.
                let replaced_contents = ReplacedContents::from_image(node, context, image)
                    .unwrap_or_else(|| ReplacedContents::zero_sized_invalid_image(node));
                return Self::Replaced(replaced_contents);
```

Code after:
                    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,
{
    match item {
        ContentItem::OpenQuote => opening.to_string(),
        ContentItem::CloseQuote => closing.to_string(),
        _ => unreachable!("Got an unexpected ContentItem type when processing quotes."),
    }
}

/// <https://www.w3.org/TR/CSS2/generate.html#propdef-content>
fn generate_pseudo_element_content(