Skip to content

Commit

Permalink
add namespaces to elements
Browse files Browse the repository at this point in the history
  • Loading branch information
therealglazou committed Dec 9, 2013
1 parent 76e3b34 commit 28575c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/components/script/dom/element.rs
Expand Up @@ -30,6 +30,7 @@ use std::ascii::StrAsciiExt;
pub struct Element {
node: Node<ScriptView>,
tag_name: ~str, // TODO: This should be an atom, not a ~str.
namespace: Namespace,
attrs: HashMap<~str, ~[@mut Attr]>,
attrs_insert_order: ~[(~str, Namespace)], // store an order of attributes.
style_attribute: Option<style::PropertyDeclarationBlock>,
Expand Down Expand Up @@ -128,6 +129,10 @@ impl ElementLike for Element {
self.tag_name.as_slice()
}

fn get_namespace<'a>(&'a self) -> ~str {
self.namespace.to_str().unwrap_or(~"")
}

fn get_attr(&self, name: &str) -> Option<~str> {
self.get_attribute(None, name).map(|attr| attr.value.clone())
}
Expand All @@ -146,10 +151,11 @@ impl ElementLike for Element {
}

impl<'self> Element {
pub fn new(type_id: ElementTypeId, tag_name: ~str, document: AbstractDocument) -> Element {
pub fn new(type_id: ElementTypeId, tag_name: ~str, namespace: Namespace, document: AbstractDocument) -> Element {
Element {
node: Node::new(ElementNodeTypeId(type_id), document),
tag_name: tag_name,
namespace: namespace,
attrs: HashMap::new(),
attrs_insert_order: ~[],
attr_list: None,
Expand Down
3 changes: 2 additions & 1 deletion src/components/script/dom/htmlelement.rs
Expand Up @@ -9,6 +9,7 @@ use dom::element::{Element, ElementTypeId, HTMLElementTypeId};
use dom::node::{AbstractNode, Node, ScriptView};
use js::jsapi::{JSContext, JSVal};
use js::JSVAL_NULL;
use dom::namespace;

pub struct HTMLElement {
element: Element
Expand All @@ -17,7 +18,7 @@ pub struct HTMLElement {
impl HTMLElement {
pub fn new_inherited(type_id: ElementTypeId, tag_name: ~str, document: AbstractDocument) -> HTMLElement {
HTMLElement {
element: Element::new(type_id, tag_name, document)
element: Element::new(type_id, tag_name, namespace::HTML, document)
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/components/style/selector_matching.rs
Expand Up @@ -425,7 +425,11 @@ fn matches_simple_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: Ele
element.get_local_name().eq_ignore_ascii_case(name.as_slice())
}
}
NamespaceSelector(_) => false, // TODO, when the DOM supports namespaces on elements.
NamespaceSelector(ref url) => {
do element.with_imm_element_like |element: &E| {
str::eq_slice(element.get_namespace(), *url)
}
}
// TODO: case-sensitivity depends on the document type and quirks mode
// TODO: cache and intern IDs on elements.
IDSelector(ref id) => {
Expand Down Expand Up @@ -533,12 +537,12 @@ fn matches_generic_nth_child<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: E
None => return false
};

let mut local_name = "";
let mut element_local_name = "";
let mut element_namespace = ~"";
if is_of_type {
// FIXME this is wrong
// TODO when the DOM supports namespaces on elements
do element.with_imm_element_like |element: &E| {
local_name = element.get_local_name();
element_local_name = element.get_local_name();
element_namespace = element.get_namespace();
}
}

Expand All @@ -558,10 +562,9 @@ fn matches_generic_nth_child<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: E

if node.is_element() {
if is_of_type {
// FIXME this is wrong
// TODO when the DOM supports namespaces on elements
do node.with_imm_element_like |node: &E| {
if local_name == node.get_local_name() {
if element_local_name == node.get_local_name() &&
element_namespace == node.get_namespace() {
index += 1;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/util/tree.rs
Expand Up @@ -348,6 +348,7 @@ pub trait TreeNode<Ref: TreeNodeRef<Self>> {

pub trait ElementLike {
fn get_local_name<'a>(&'a self) -> &'a str;
fn get_namespace<'a>(&'a self) -> ~str;
fn get_attr(&self, name: &str) -> Option<~str>;
fn get_link(&self) -> Option<~str>;
}

5 comments on commit 28575c2

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at therealglazou@28575c2

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging therealglazou/servo/therealglazou/namespaces = 28575c2 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

therealglazou/servo/therealglazou/namespaces = 28575c2 merged ok, testing candidate = 4fc48f5

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 4fc48f5

Please sign in to comment.