mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Auto merge of #7901 - Ms2ger:option-text, r=nox
Cleanup the HTMLOptionElement#text implementation. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7901) <!-- Reviewable:end -->
This commit is contained in:
commit
84d8f65d22
2 changed files with 29 additions and 15 deletions
|
@ -12,7 +12,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLOptionElementDerived};
|
||||||
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived};
|
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived};
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::Root;
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::element::{AttributeMutation, ElementTypeId};
|
use dom::element::{Element, AttributeMutation, ElementTypeId};
|
||||||
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
||||||
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
||||||
use dom::node::{Node, NodeTypeId};
|
use dom::node::{Node, NodeTypeId};
|
||||||
|
@ -60,20 +60,19 @@ impl HTMLOptionElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_text(node: &&Node, value: &mut DOMString) {
|
fn collect_text(element: &Element, value: &mut DOMString) {
|
||||||
let elem = ElementCast::to_ref(*node).unwrap();
|
let svg_script = *element.namespace() == ns!(SVG) && element.local_name() == &atom!("script");
|
||||||
let svg_script = *elem.namespace() == ns!(SVG) && elem.local_name() == &atom!("script");
|
let html_script = element.is_htmlscriptelement();
|
||||||
let html_script = node.is_htmlscriptelement();
|
|
||||||
if svg_script || html_script {
|
if svg_script || html_script {
|
||||||
return;
|
return;
|
||||||
} else {
|
}
|
||||||
for child in node.children() {
|
|
||||||
if child.r().is_text() {
|
for child in NodeCast::from_ref(element).children() {
|
||||||
let characterdata = CharacterDataCast::to_ref(child.r()).unwrap();
|
if child.r().is_text() {
|
||||||
value.push_str(&characterdata.Data());
|
let characterdata = CharacterDataCast::to_ref(child.r()).unwrap();
|
||||||
} else {
|
value.push_str(&characterdata.Data());
|
||||||
collect_text(&child.r(), value);
|
} else if let Some(element_child) = ElementCast::to_ref(&*child) {
|
||||||
}
|
collect_text(element_child, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,9 +89,9 @@ impl HTMLOptionElementMethods for HTMLOptionElement {
|
||||||
|
|
||||||
// https://www.whatwg.org/html/#dom-option-text
|
// https://www.whatwg.org/html/#dom-option-text
|
||||||
fn Text(&self) -> DOMString {
|
fn Text(&self) -> DOMString {
|
||||||
let node = NodeCast::from_ref(self);
|
let element = ElementCast::from_ref(self);
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
collect_text(&node, &mut content);
|
collect_text(element, &mut content);
|
||||||
str_join(split_html_space_chars(&content), " ")
|
str_join(split_html_space_chars(&content), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,4 +74,19 @@ test(function() {
|
||||||
option.appendChild(document.createTextNode("text"));
|
option.appendChild(document.createTextNode("text"));
|
||||||
assert_equals(option.text, "text");
|
assert_equals(option.text, "text");
|
||||||
}, "option.text should work if the option is in a MathML script element");
|
}, "option.text should work if the option is in a MathML script element");
|
||||||
|
|
||||||
|
test(function() {
|
||||||
|
var option = document.createElement("option");
|
||||||
|
option.appendChild(document.createTextNode("te"));
|
||||||
|
option.appendChild(document.createComment("comment"));
|
||||||
|
option.appendChild(document.createTextNode("xt"));
|
||||||
|
assert_equals(option.text, "text");
|
||||||
|
}, "option.text should ignore comment children");
|
||||||
|
test(function() {
|
||||||
|
var option = document.createElement("option");
|
||||||
|
option.appendChild(document.createTextNode("te"));
|
||||||
|
option.appendChild(document.createProcessingInstruction("target", "data"));
|
||||||
|
option.appendChild(document.createTextNode("xt"));
|
||||||
|
assert_equals(option.text, "text");
|
||||||
|
}, "option.text should ignore PI children");
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue