Move Element::get_attr to a trait defined in util::tree

… and add a get_local_name() method to that trait.
This commit is contained in:
Simon Sapin 2013-10-16 16:47:04 +01:00
parent f38b4ab9bb
commit 62f1f03c16
10 changed files with 32 additions and 13 deletions

View file

@ -9,6 +9,7 @@
use std::str::eq_slice;
use newcss::select::SelectHandler;
use script::dom::node::{AbstractNode, LayoutView};
use servo_util::tree::ElementLike;
pub struct NodeSelectHandler {
node: AbstractNode<LayoutView>,

View file

@ -38,6 +38,7 @@ use script::dom::node::{AbstractNode, LayoutView};
use servo_net::image::holder::ImageHolder;
use servo_net::local_image_cache::LocalImageCache;
use servo_util::range::*;
use servo_util::tree::ElementLike;
use extra::url::Url;
/// Render boxes (`struct RenderBox`) are the leaves of the layout tree. They cannot position

View file

@ -24,7 +24,7 @@ use html::hubbub_html_parser::build_element_from_tag;
use js::jsapi::{JSObject, JSContext, JSVal};
use js::jsapi::{JSTRACE_OBJECT, JSTracer, JS_CallTracer};
use js::glue::RUST_OBJECT_TO_JSVAL;
use servo_util::tree::TreeNodeRef;
use servo_util::tree::{TreeNodeRef, ElementLike};
use std::hashmap::HashMap;

View file

@ -14,6 +14,7 @@ use dom::node::{ElementNodeTypeId, Node, ScriptView, AbstractNode};
use layout_interface::{ContentBoxQuery, ContentBoxResponse, ContentBoxesQuery};
use layout_interface::{ContentBoxesResponse};
use newcss::stylesheet::Stylesheet;
use servo_util::tree::ElementLike;
use js::jsapi::{JSContext, JSObject};
@ -119,17 +120,12 @@ pub enum ElementTypeId {
// Element methods
//
impl<'self> Element {
pub fn new(type_id: ElementTypeId, tag_name: ~str, document: AbstractDocument) -> Element {
Element {
node: Node::new(ElementNodeTypeId(type_id), document),
tag_name: tag_name,
attrs: ~[],
style_attribute: None,
}
impl<'self> ElementLike<'self> for Element {
fn get_local_name(&'self self) -> &'self str {
self.tag_name.as_slice()
}
pub fn get_attr(&'self self, name: &str) -> Option<&'self str> {
fn get_attr(&'self self, name: &str) -> Option<&'self str> {
// FIXME: Need an each() that links lifetimes in Rust.
for attr in self.attrs.iter() {
// FIXME: only case-insensitive in the HTML namespace (as opposed to SVG, etc.)
@ -140,6 +136,17 @@ impl<'self> Element {
}
return None;
}
}
impl<'self> Element {
pub fn new(type_id: ElementTypeId, tag_name: ~str, document: AbstractDocument) -> Element {
Element {
node: Node::new(ElementNodeTypeId(type_id), document),
tag_name: tag_name,
attrs: ~[],
style_attribute: None,
}
}
pub fn set_attr(&mut self,
abstract_self: AbstractNode<ScriptView>,

View file

@ -13,7 +13,7 @@ use dom::window::Window;
use js::jsapi::{JSObject, JSContext, JSTracer};
use servo_util::tree::TreeNodeRef;
use servo_util::tree::{TreeNodeRef, ElementLike};
use std::libc;
use std::ptr;

View file

@ -1,3 +1,4 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@ -11,6 +12,7 @@ use layout_interface::{ContentBoxQuery, ContentBoxResponse};
use servo_net::image_cache_task;
use servo_net::image_cache_task::ImageCacheTask;
use servo_util::url::make_url;
use servo_util::tree::ElementLike;
pub struct HTMLImageElement {
htmlelement: HTMLElement,

View file

@ -26,7 +26,7 @@ use hubbub::hubbub;
use servo_msg::constellation_msg::{ConstellationChan, SubpageId};
use servo_net::image_cache_task::ImageCacheTask;
use servo_net::resource_task::{Load, Payload, Done, ResourceTask};
use servo_util::tree::TreeNodeRef;
use servo_util::tree::{TreeNodeRef, ElementLike};
use servo_util::url::make_url;
use extra::url::Url;
use extra::future::{Future, from_port};

View file

@ -46,7 +46,7 @@ use js::rust::{Compartment, Cx};
use js;
use servo_net::image_cache_task::ImageCacheTask;
use servo_net::resource_task::ResourceTask;
use servo_util::tree::TreeNodeRef;
use servo_util::tree::{TreeNodeRef, ElementLike};
use servo_util::url::make_url;
use extra::url::Url;
use extra::future::{from_value, Future};

View file

@ -11,6 +11,7 @@ use media_queries::{Device, Screen};
use properties::{PropertyDeclaration, PropertyDeclarationBlock};
use script::dom::node::{AbstractNode, ScriptView};
use script::dom::element::Element;
use servo_util::tree::ElementLike;
pub enum StylesheetOrigin {
@ -192,6 +193,7 @@ fn matches_simple_selector(selector: &SimpleSelector, element: &Element) -> bool
match *selector {
// TODO: case-sensitivity depends on the document type
// TODO: intern element names
LocalNameSelector(ref name) => element.tag_name.eq_ignore_ascii_case(name.as_slice()),
NamespaceSelector(_) => false, // TODO, when the DOM supports namespaces on elements.
// TODO: case-sensitivity depends on the document type and quirks mode

View file

@ -324,3 +324,9 @@ pub trait TreeNode<Ref: TreeNodeRef<Self>> {
TreeNodeRef::<Self>::set_next_sibling(self, new_next_sibling)
}
}
pub trait ElementLike<'self> {
fn get_local_name(&'self self) -> &'self str;
fn get_attr(&'self self, name: &str) -> Option<&'self str>;
}