Skip to content

Commit

Permalink
Remove recursiveness from directionality search
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Feb 24, 2020
1 parent 7e2107b commit edb940e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
7 changes: 5 additions & 2 deletions components/script/dom/element.rs
Expand Up @@ -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()
Expand Down
17 changes: 8 additions & 9 deletions components/script/dom/htmlelement.rs
Expand Up @@ -774,29 +774,29 @@ 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" {
if let Some(directionality) = self
.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());
}
}

Expand All @@ -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
}
}

Expand Down
10 changes: 4 additions & 6 deletions components/script/dom/htmlformelement.rs
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
27 changes: 18 additions & 9 deletions components/script/dom/node.rs
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit edb940e

Please sign in to comment.