Remove recursiveness from directionality search

This commit is contained in:
Dmitry Kolupaev 2020-02-16 19:14:40 +03:00
parent 7e2107b1a5
commit edb940e613
4 changed files with 35 additions and 26 deletions

View file

@ -543,8 +543,11 @@ impl Element {
// https://html.spec.whatwg.org/multipage/#the-directionality
pub fn directionality(&self) -> String {
if let Some(html_element) = self.downcast::<HTMLElement>() {
html_element.directionality()
if let Some(directionality) = self
.downcast::<HTMLElement>()
.and_then(|html_element| html_element.directionality())
{
directionality
} else {
let node = self.upcast::<Node>();
node.parent_directionality()

View file

@ -774,17 +774,17 @@ impl HTMLElement {
.count() as u32
}
pub fn directionality(&self) -> String {
println!("HTMLElement#directionality");
// returns Some if can infer direction by itself or from child nodes
// returns None if requires to go up to parent
pub fn directionality(&self) -> Option<String> {
let element_direction: &str = &self.Dir();
println!("HTMLElement#element_direction={}", element_direction);
if element_direction == "ltr" {
return "ltr".to_owned();
return Some("ltr".to_owned());
}
if element_direction == "rtl" {
return "rtl".to_owned();
return Some("rtl".to_owned());
}
if element_direction == "auto" {
@ -792,11 +792,11 @@ impl HTMLElement {
.downcast::<HTMLInputElement>()
.and_then(|input| input.auto_directionality())
{
return directionality;
return Some(directionality);
}
if let Some(area) = self.downcast::<HTMLTextAreaElement>() {
return area.auto_directionality();
return Some(area.auto_directionality());
}
}
@ -806,8 +806,7 @@ impl HTMLElement {
// (i.e. it is not present or has an invalid value)
// Requires bdi element implementation (https://html.spec.whatwg.org/multipage/#the-bdi-element)
let node = self.upcast::<Node>();
node.parent_directionality()
None
}
}

View file

@ -952,11 +952,10 @@ impl HTMLFormElement {
let input = child.downcast::<HTMLInputElement>().unwrap();
data_set.append(&mut input.form_datums(submitter, encoding));
let input_html_element = child.downcast::<HTMLElement>().unwrap();
let input_element = child.downcast::<Element>().unwrap();
let dirname: DOMString = input.DirName();
if !dirname.is_empty() {
let directionality =
DOMString::from(input_html_element.directionality());
let directionality = DOMString::from(input_element.directionality());
data_set.push(FormDatum {
ty: input.Type().clone(),
name: dirname.clone(),
@ -989,11 +988,10 @@ impl HTMLFormElement {
});
}
let area_html_element = child.downcast::<HTMLElement>().unwrap();
let area_element = child.downcast::<Element>().unwrap();
let dirname: DOMString = textarea.DirName();
if !dirname.is_empty() {
let directionality =
DOMString::from(area_html_element.directionality());
let directionality = DOMString::from(area_element.directionality());
data_set.push(FormDatum {
ty: textarea.Type().clone(),
name: dirname.clone(),

View file

@ -434,16 +434,25 @@ impl Node {
}
pub fn parent_directionality(&self) -> String {
match self.GetParentNode() {
Some(parent) => {
if let Some(parent_html) = parent.downcast::<Element>() {
parent_html.directionality()
} else {
parent.parent_directionality()
}
},
None => "ltr".to_owned(),
let mut current = self.GetParentNode();
loop {
match current {
Some(parent) => {
if let Some(directionality) = parent
.downcast::<HTMLElement>()
.and_then(|html_element| html_element.directionality())
{
return directionality;
} else {
current = parent.GetParentNode();
}
},
None => break,
}
}
"ltr".to_owned()
}
}