diff --git a/components/script/dom/htmloptionelement.rs b/components/script/dom/htmloptionelement.rs index 800e88f0758..a61564d3c5b 100644 --- a/components/script/dom/htmloptionelement.rs +++ b/components/script/dom/htmloptionelement.rs @@ -152,25 +152,6 @@ impl HTMLOptionElement { } } -// FIXME(ajeffrey): Provide a way of buffering DOMStrings other than using Strings -fn collect_text(element: &Element, value: &mut String) { - let svg_script = - *element.namespace() == ns!(svg) && element.local_name() == &local_name!("script"); - let html_script = element.is::(); - if svg_script || html_script { - return; - } - - for child in element.upcast::().children() { - if child.is::() { - let characterdata = child.downcast::().unwrap(); - value.push_str(&characterdata.Data()); - } else if let Some(element_child) = child.downcast() { - collect_text(element_child, value); - } - } -} - impl HTMLOptionElementMethods for HTMLOptionElement { /// fn Option( @@ -216,8 +197,28 @@ impl HTMLOptionElementMethods for HTMLOptionElement { /// fn Text(&self) -> DOMString { - let mut content = String::new(); - collect_text(self.upcast(), &mut content); + let mut content = DOMString::new(); + + let mut iterator = self.upcast::().traverse_preorder(ShadowIncluding::No); + while let Some(node) = iterator.peek() { + if let Some(element) = node.downcast::() { + let html_script = element.is::(); + let svg_script = *element.namespace() == ns!(svg) && + element.local_name() == &local_name!("script"); + if html_script || svg_script { + iterator.next_skipping_children(); + continue; + } + } + + if node.is::() { + let characterdata = node.downcast::().unwrap(); + content.push_str(&characterdata.Data()); + } + + iterator.next(); + } + DOMString::from(str_join(split_html_space_chars(&content), " ")) } diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 1ff2e25efae..691b3a38f19 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -2035,6 +2035,10 @@ impl TreeIterator { self.current = None; Some(current) } + + pub(crate) fn peek(&self) -> Option<&DomRoot> { + self.current.as_ref() + } } impl Iterator for TreeIterator {