aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmlbaseelement.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/htmlbaseelement.rs')
-rw-r--r--components/script/dom/htmlbaseelement.rs110
1 files changed, 62 insertions, 48 deletions
diff --git a/components/script/dom/htmlbaseelement.rs b/components/script/dom/htmlbaseelement.rs
index fe34e6d77f0..e6568716e1f 100644
--- a/components/script/dom/htmlbaseelement.rs
+++ b/components/script/dom/htmlbaseelement.rs
@@ -1,49 +1,58 @@
/* 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/. */
-
-use dom::attr::Attr;
-use dom::bindings::codegen::Bindings::HTMLBaseElementBinding;
-use dom::bindings::codegen::Bindings::HTMLBaseElementBinding::HTMLBaseElementMethods;
-use dom::bindings::inheritance::Castable;
-use dom::bindings::js::Root;
-use dom::bindings::str::DOMString;
-use dom::document::Document;
-use dom::element::{AttributeMutation, Element};
-use dom::htmlelement::HTMLElement;
-use dom::node::{Node, UnbindContext, document_from_node};
-use dom::virtualmethods::VirtualMethods;
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use crate::dom::attr::Attr;
+use crate::dom::bindings::codegen::Bindings::HTMLBaseElementBinding::HTMLBaseElementMethods;
+use crate::dom::bindings::inheritance::Castable;
+use crate::dom::bindings::root::DomRoot;
+use crate::dom::bindings::str::DOMString;
+use crate::dom::document::Document;
+use crate::dom::element::{AttributeMutation, Element};
+use crate::dom::htmlelement::HTMLElement;
+use crate::dom::node::{document_from_node, BindContext, Node, UnbindContext};
+use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
-use html5ever_atoms::LocalName;
+use html5ever::{LocalName, Prefix};
use servo_url::ServoUrl;
-use style::attr::AttrValue;
#[dom_struct]
pub struct HTMLBaseElement {
- htmlelement: HTMLElement
+ htmlelement: HTMLElement,
}
impl HTMLBaseElement {
- fn new_inherited(local_name: LocalName, prefix: Option<DOMString>, document: &Document) -> HTMLBaseElement {
+ fn new_inherited(
+ local_name: LocalName,
+ prefix: Option<Prefix>,
+ document: &Document,
+ ) -> HTMLBaseElement {
HTMLBaseElement {
- htmlelement: HTMLElement::new_inherited(local_name, prefix, document)
+ htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
}
}
#[allow(unrooted_must_root)]
- pub fn new(local_name: LocalName,
- prefix: Option<DOMString>,
- document: &Document) -> Root<HTMLBaseElement> {
- Node::reflect_node(box HTMLBaseElement::new_inherited(local_name, prefix, document),
- document,
- HTMLBaseElementBinding::Wrap)
+ pub fn new(
+ local_name: LocalName,
+ prefix: Option<Prefix>,
+ document: &Document,
+ ) -> DomRoot<HTMLBaseElement> {
+ Node::reflect_node(
+ Box::new(HTMLBaseElement::new_inherited(local_name, prefix, document)),
+ document,
+ )
}
- /// https://html.spec.whatwg.org/multipage/#frozen-base-url
+ /// <https://html.spec.whatwg.org/multipage/#frozen-base-url>
pub fn frozen_base_url(&self) -> ServoUrl {
- let href = self.upcast::<Element>().get_attribute(&ns!(), &local_name!("href"))
- .expect("The frozen base url is only defined for base elements \
- that have a base url.");
+ let href = self
+ .upcast::<Element>()
+ .get_attribute(&ns!(), &local_name!("href"))
+ .expect(
+ "The frozen base url is only defined for base elements \
+ that have a base url.",
+ );
let document = document_from_node(self);
let base = document.fallback_base_url();
let parsed = base.join(&href.value());
@@ -53,7 +62,7 @@ impl HTMLBaseElement {
/// Update the cached base element in response to binding or unbinding from
/// a tree.
pub fn bind_unbind(&self, tree_in_doc: bool) {
- if !tree_in_doc {
+ if !tree_in_doc || self.upcast::<Node>().containing_shadow_root().is_some() {
return;
}
@@ -67,33 +76,38 @@ impl HTMLBaseElement {
impl HTMLBaseElementMethods for HTMLBaseElement {
// https://html.spec.whatwg.org/multipage/#dom-base-href
fn Href(&self) -> DOMString {
- let document = document_from_node(self);
-
// Step 1.
- if !self.upcast::<Element>().has_attribute(&local_name!("href")) {
- return DOMString::from(document.base_url().as_str());
- }
+ let document = document_from_node(self);
// Step 2.
- let fallback_base_url = document.fallback_base_url();
+ let attr = self
+ .upcast::<Element>()
+ .get_attribute(&ns!(), &local_name!("href"));
+ let value = attr.as_ref().map(|attr| attr.value());
+ let url = value.as_ref().map_or("", |value| &**value);
// Step 3.
- let url = self.upcast::<Element>().get_url_attribute(&local_name!("href"));
-
- // Step 4.
- let url_record = fallback_base_url.join(&*url);
-
- // Step 5, 6.
- DOMString::from(url_record.as_ref().map(|url| url.as_str()).unwrap_or(""))
+ let url_record = document.fallback_base_url().join(url);
+
+ match url_record {
+ Err(_) => {
+ // Step 4.
+ url.into()
+ },
+ Ok(url_record) => {
+ // Step 5.
+ url_record.into_string().into()
+ },
+ }
}
// https://html.spec.whatwg.org/multipage/#dom-base-href
- make_url_setter!(SetHref, "href");
+ make_setter!(SetHref, "href");
}
impl VirtualMethods for HTMLBaseElement {
- fn super_type(&self) -> Option<&VirtualMethods> {
- Some(self.upcast::<HTMLElement>() as &VirtualMethods)
+ fn super_type(&self) -> Option<&dyn VirtualMethods> {
+ Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
@@ -103,9 +117,9 @@ impl VirtualMethods for HTMLBaseElement {
}
}
- fn bind_to_tree(&self, tree_in_doc: bool) {
- self.super_type().unwrap().bind_to_tree(tree_in_doc);
- self.bind_unbind(tree_in_doc);
+ fn bind_to_tree(&self, context: &BindContext) {
+ self.super_type().unwrap().bind_to_tree(context);
+ self.bind_unbind(context.tree_in_doc);
}
fn unbind_from_tree(&self, context: &UnbindContext) {