mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Use eq() and eq_slice() less, and map_default() more.
This commit is contained in:
parent
ecc07e3b49
commit
e45b7fa22d
7 changed files with 35 additions and 40 deletions
|
@ -241,13 +241,11 @@ impl<'ln> TNode<LayoutElement<'ln>> for LayoutNode<'ln> {
|
||||||
};
|
};
|
||||||
match attr.namespace {
|
match attr.namespace {
|
||||||
SpecificNamespace(ref ns) => {
|
SpecificNamespace(ref ns) => {
|
||||||
match element.get_attr(ns, name) {
|
element.get_attr(ns, name)
|
||||||
Some(value) => test(value),
|
.map_default(false, |attr| test(attr))
|
||||||
None => false,
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1558
|
// FIXME: https://github.com/mozilla/servo/issues/1558
|
||||||
AnyNamespace => return false,
|
AnyNamespace => false,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,6 @@ use js::jsapi::{JSObject, JSContext, JSTracer};
|
||||||
use std::ascii::StrAsciiExt;
|
use std::ascii::StrAsciiExt;
|
||||||
use std::cast;
|
use std::cast;
|
||||||
use std::hashmap::HashMap;
|
use std::hashmap::HashMap;
|
||||||
use std::str::eq_slice;
|
|
||||||
use std::unstable::raw::Box;
|
use std::unstable::raw::Box;
|
||||||
|
|
||||||
#[deriving(Eq)]
|
#[deriving(Eq)]
|
||||||
|
@ -247,7 +246,7 @@ impl Document {
|
||||||
|
|
||||||
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagname
|
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagname
|
||||||
pub fn GetElementsByTagName(&self, tag: DOMString) -> @mut HTMLCollection {
|
pub fn GetElementsByTagName(&self, tag: DOMString) -> @mut HTMLCollection {
|
||||||
self.createHTMLCollection(|elem| eq_slice(elem.tag_name, tag))
|
self.createHTMLCollection(|elem| elem.tag_name == tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagnamens
|
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagnamens
|
||||||
|
@ -467,8 +466,11 @@ impl Document {
|
||||||
|
|
||||||
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname
|
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyname
|
||||||
pub fn GetElementsByName(&self, name: DOMString) -> @mut HTMLCollection {
|
pub fn GetElementsByName(&self, name: DOMString) -> @mut HTMLCollection {
|
||||||
self.createHTMLCollection(|elem|
|
self.createHTMLCollection(|elem| {
|
||||||
elem.get_attribute(Null, "name").is_some() && eq_slice(elem.get_attribute(Null, "name").unwrap().value_ref(), name))
|
elem.get_attribute(Null, "name").map_default(false, |attr| {
|
||||||
|
attr.value_ref() == name
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> @mut HTMLCollection {
|
pub fn createHTMLCollection(&self, callback: |elem: &Element| -> bool) -> @mut HTMLCollection {
|
||||||
|
|
|
@ -26,7 +26,6 @@ use servo_util::str::{DOMString, null_str_as_empty_ref};
|
||||||
|
|
||||||
use std::ascii::StrAsciiExt;
|
use std::ascii::StrAsciiExt;
|
||||||
use std::cast;
|
use std::cast;
|
||||||
use std::str::eq;
|
|
||||||
use std::unstable::raw::Box;
|
use std::unstable::raw::Box;
|
||||||
|
|
||||||
pub struct Element {
|
pub struct Element {
|
||||||
|
@ -181,8 +180,8 @@ impl Element {
|
||||||
match prefix {
|
match prefix {
|
||||||
Some(ref prefix_str) => {
|
Some(ref prefix_str) => {
|
||||||
if (namespace == namespace::Null ||
|
if (namespace == namespace::Null ||
|
||||||
(eq(prefix_str, &~"xml") && namespace != namespace::XML) ||
|
("xml" == prefix_str.as_slice() && namespace != namespace::XML) ||
|
||||||
(eq(prefix_str, &~"xmlns") && namespace != namespace::XMLNS)) {
|
("xmlns" == prefix_str.as_slice() && namespace != namespace::XMLNS)) {
|
||||||
return Err(NamespaceError);
|
return Err(NamespaceError);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,7 +11,6 @@ use servo_util::namespace::Null;
|
||||||
|
|
||||||
use extra::url::Url;
|
use extra::url::Url;
|
||||||
use js::jsapi::JSTracer;
|
use js::jsapi::JSTracer;
|
||||||
use std::str::eq_slice;
|
|
||||||
|
|
||||||
pub struct HTMLDocument {
|
pub struct HTMLDocument {
|
||||||
parent: Document
|
parent: Document
|
||||||
|
@ -32,11 +31,11 @@ impl HTMLDocument {
|
||||||
|
|
||||||
impl HTMLDocument {
|
impl HTMLDocument {
|
||||||
pub fn Images(&self) -> @mut HTMLCollection {
|
pub fn Images(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img"))
|
self.parent.createHTMLCollection(|elem| "img" == elem.tag_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Embeds(&self) -> @mut HTMLCollection {
|
pub fn Embeds(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed"))
|
self.parent.createHTMLCollection(|elem| "embed" == elem.tag_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Plugins(&self) -> @mut HTMLCollection {
|
pub fn Plugins(&self) -> @mut HTMLCollection {
|
||||||
|
@ -44,27 +43,29 @@ impl HTMLDocument {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Links(&self) -> @mut HTMLCollection {
|
pub fn Links(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem|
|
self.parent.createHTMLCollection(|elem| {
|
||||||
(eq_slice(elem.tag_name, "a") || eq_slice(elem.tag_name, "area"))
|
("a" == elem.tag_name || "area" == elem.tag_name) &&
|
||||||
&& elem.get_attribute(Null, "href").is_some())
|
elem.get_attribute(Null, "href").is_some()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Forms(&self) -> @mut HTMLCollection {
|
pub fn Forms(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form"))
|
self.parent.createHTMLCollection(|elem| "form" == elem.tag_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Scripts(&self) -> @mut HTMLCollection {
|
pub fn Scripts(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script"))
|
self.parent.createHTMLCollection(|elem| "script" == elem.tag_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Anchors(&self) -> @mut HTMLCollection {
|
pub fn Anchors(&self) -> @mut HTMLCollection {
|
||||||
self.parent.createHTMLCollection(|elem|
|
self.parent.createHTMLCollection(|elem| {
|
||||||
eq_slice(elem.tag_name, "a") && elem.get_attribute(Null, "name").is_some())
|
"a" == elem.tag_name && elem.get_attribute(Null, "name").is_some()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Applets(&self) -> @mut HTMLCollection {
|
pub fn Applets(&self) -> @mut HTMLCollection {
|
||||||
// FIXME: This should be return OBJECT elements containing applets.
|
// FIXME: This should be return OBJECT elements containing applets.
|
||||||
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet"))
|
self.parent.createHTMLCollection(|elem| "applet" == elem.tag_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,6 @@ use std::cast;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::comm::{Port, SharedChan};
|
use std::comm::{Port, SharedChan};
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
use std::str::eq_slice;
|
|
||||||
use std::str;
|
use std::str;
|
||||||
use style::Stylesheet;
|
use style::Stylesheet;
|
||||||
|
|
||||||
|
@ -37,7 +36,7 @@ macro_rules! handle_element(
|
||||||
$string: expr,
|
$string: expr,
|
||||||
$ctor: ident
|
$ctor: ident
|
||||||
$(, $arg:expr )*) => (
|
$(, $arg:expr )*) => (
|
||||||
if eq_slice($localName, $string) {
|
if $string == $localName {
|
||||||
return $ctor::new($localName, $document $(, $arg)*);
|
return $ctor::new($localName, $document $(, $arg)*);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -49,7 +49,6 @@ use servo_util::task::send_on_failure;
|
||||||
use servo_util::namespace::Null;
|
use servo_util::namespace::Null;
|
||||||
use std::comm::{Port, SharedChan};
|
use std::comm::{Port, SharedChan};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::str::eq_slice;
|
|
||||||
use std::task;
|
use std::task;
|
||||||
use std::util::replace;
|
use std::util::replace;
|
||||||
|
|
||||||
|
@ -796,10 +795,9 @@ impl ScriptTask {
|
||||||
let mut anchors = doc_node.traverse_preorder().filter(|node| node.is_anchor_element());
|
let mut anchors = doc_node.traverse_preorder().filter(|node| node.is_anchor_element());
|
||||||
anchors.find(|node| {
|
anchors.find(|node| {
|
||||||
node.with_imm_element(|elem| {
|
node.with_imm_element(|elem| {
|
||||||
match elem.get_attribute(Null, "name") {
|
elem.get_attribute(Null, "name").map_default(false, |attr| {
|
||||||
Some(name) => eq_slice(name.value_ref(), fragid),
|
attr.value_ref() == fragid
|
||||||
None => false
|
})
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
use extra::arc::Arc;
|
use extra::arc::Arc;
|
||||||
use std::ascii::StrAsciiExt;
|
use std::ascii::StrAsciiExt;
|
||||||
use std::hashmap::HashMap;
|
use std::hashmap::HashMap;
|
||||||
use std::str;
|
|
||||||
use std::to_bytes;
|
use std::to_bytes;
|
||||||
|
|
||||||
use servo_util::namespace;
|
use servo_util::namespace;
|
||||||
|
@ -596,21 +595,20 @@ fn matches_simple_selector<E:TElement,
|
||||||
IDSelector(ref id) => {
|
IDSelector(ref id) => {
|
||||||
*shareable = false;
|
*shareable = false;
|
||||||
element.with_element(|element: &E| {
|
element.with_element(|element: &E| {
|
||||||
match element.get_attr(&namespace::Null, "id") {
|
element.get_attr(&namespace::Null, "id")
|
||||||
Some(attr) => str::eq_slice(attr, *id),
|
.map_default(false, |attr| {
|
||||||
None => false
|
attr == *id
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// TODO: cache and intern class names on elements.
|
// TODO: cache and intern class names on elements.
|
||||||
ClassSelector(ref class) => {
|
ClassSelector(ref class) => {
|
||||||
element.with_element(|element: &E| {
|
element.with_element(|element: &E| {
|
||||||
match element.get_attr(&namespace::Null, "class") {
|
element.get_attr(&namespace::Null, "class")
|
||||||
None => false,
|
.map_default(false, |attr| {
|
||||||
// TODO: case-sensitivity depends on the document type and quirks mode
|
// TODO: case-sensitivity depends on the document type and quirks mode
|
||||||
Some(ref class_attr)
|
attr.split(SELECTOR_WHITESPACE).any(|c| c == class.as_slice())
|
||||||
=> class_attr.split(SELECTOR_WHITESPACE).any(|c| c == class.as_slice()),
|
})
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue