Skip to content

Commit

Permalink
servo: Merge #7046 - Implement a base_url getter and use it for style…
Browse files Browse the repository at this point in the history
… attributes (from Ms2ger:base-url); r=dzbarsky

Source-Repo: https://github.com/servo/servo
Source-Revision: 530d4547c945fbf120d546d6e9a31b8f3bacc78f
  • Loading branch information
Ms2ger committed Aug 8, 2015
1 parent 4963436 commit b7fff47
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
49 changes: 48 additions & 1 deletion servo/components/script/dom/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLAreaElementDerived, HTMLEmbedElem
use dom::bindings::codegen::InheritTypes::{HTMLFormElementDerived, HTMLImageElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived, HTMLTitleElementDerived};
use dom::bindings::codegen::InheritTypes::ElementDerived;
use dom::bindings::codegen::InheritTypes::HTMLBaseElementCast;
use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::error::Error::{NotSupported, InvalidCharacter, Security};
Expand All @@ -45,6 +46,7 @@ use dom::element::{ElementTypeId, ActivationElementHelpers, FocusElementHelpers}
use dom::event::{Event, EventBubbles, EventCancelable, EventHelpers};
use dom::eventtarget::{EventTarget, EventTargetTypeId, EventTargetHelpers};
use dom::htmlanchorelement::HTMLAnchorElement;
use dom::htmlbaseelement::HTMLBaseElement;
use dom::htmlcollection::{HTMLCollection, CollectionFilter};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::htmlheadelement::HTMLHeadElement;
Expand Down Expand Up @@ -153,6 +155,8 @@ pub struct Document {
current_parser: MutNullableHeap<JS<ServoHTMLParser>>,
/// When we should kick off a reflow. This happens during parsing.
reflow_timeout: Cell<Option<u64>>,
/// The cached first `base` element with an `href` attribute.
base_element: MutNullableHeap<JS<HTMLBaseElement>>,
}

impl PartialEq for Document {
Expand Down Expand Up @@ -231,7 +235,16 @@ pub trait DocumentHelpers<'a> {
fn encoding_name(self) -> Ref<'a, DOMString>;
fn is_html_document(self) -> bool;
fn is_fully_active(self) -> bool;
/// https://dom.spec.whatwg.org/#concept-document-url
fn url(self) -> Url;
/// https://html.spec.whatwg.org/multipage/#fallback-base-url
fn fallback_base_url(self) -> Url;
/// https://html.spec.whatwg.org/multipage/#document-base-url
fn base_url(self) -> Url;
/// Returns the first `base` element in the DOM that has an `href` attribute.
fn base_element(self) -> Option<Root<HTMLBaseElement>>;
/// Refresh the cached first base element in the DOM.
fn refresh_base_element(self);
fn quirks_mode(self) -> QuirksMode;
fn set_quirks_mode(self, mode: QuirksMode);
fn set_encoding_name(self, name: DOMString);
Expand Down Expand Up @@ -335,11 +348,44 @@ impl<'a> DocumentHelpers<'a> for &'a Document {
true
}

// https://dom.spec.whatwg.org/#dom-document-url
// https://dom.spec.whatwg.org/#concept-document-url
fn url(self) -> Url {
self.url.clone()
}

// https://html.spec.whatwg.org/multipage/#fallback-base-url
fn fallback_base_url(self) -> Url {
// Step 1: iframe srcdoc (#4767).
// Step 2: about:blank with a creator browsing context.
// Step 3.
self.url()
}

// https://html.spec.whatwg.org/multipage/#document-base-url
fn base_url(self) -> Url {
match self.base_element() {
// Step 1.
None => self.fallback_base_url(),
// Step 2.
Some(base) => base.frozen_base_url(),
}
}

/// Returns the first `base` element in the DOM that has an `href` attribute.
fn base_element(self) -> Option<Root<HTMLBaseElement>> {
self.base_element.get().map(Root::from_rooted)
}

/// Refresh the cached first base element in the DOM.
fn refresh_base_element(self) {
let base = NodeCast::from_ref(self)
.traverse_preorder()
.filter_map(HTMLBaseElementCast::to_root)
.filter(|element| ElementCast::from_ref(&**element).has_attribute(&atom!("href")))
.next();
self.base_element.set(base.map(|element| JS::from_ref(&*element)));
}

fn quirks_mode(self) -> QuirksMode {
self.quirks_mode.get()
}
Expand Down Expand Up @@ -1089,6 +1135,7 @@ impl Document {
loader: DOMRefCell::new(doc_loader),
current_parser: Default::default(),
reflow_timeout: Cell::new(None),
base_element: Default::default(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion servo/components/script/dom/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ impl<'a> VirtualMethods for &'a Element {
&atom!("style") => {
// Modifying the `style` attribute might change style.
let doc = document_from_node(*self);
let base_url = doc.r().url();
let base_url = doc.r().base_url();
let value = attr.value();
let style = Some(parse_style_attribute(&value, &base_url));
*self.style_attribute.borrow_mut() = style;
Expand Down
69 changes: 66 additions & 3 deletions servo/components/script/dom/htmlbaseelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
* 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, AttrHelpers};
use dom::bindings::codegen::Bindings::HTMLBaseElementBinding;
use dom::bindings::codegen::InheritTypes::ElementCast;
use dom::bindings::codegen::InheritTypes::HTMLBaseElementDerived;
use dom::bindings::codegen::InheritTypes::HTMLElementCast;
use dom::bindings::js::Root;
use dom::document::Document;
use dom::document::{Document, DocumentHelpers};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::element::ElementTypeId;
use dom::element::{ElementTypeId, AttributeHandlers};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeTypeId};
use dom::node::{Node, NodeTypeId, document_from_node};
use dom::virtualmethods::VirtualMethods;
use util::str::DOMString;

use url::{Url, UrlParser};

#[dom_struct]
pub struct HTMLBaseElement {
htmlelement: HTMLElement
Expand Down Expand Up @@ -39,5 +45,62 @@ impl HTMLBaseElement {
let element = HTMLBaseElement::new_inherited(localName, prefix, document);
Node::reflect_node(box element, document, HTMLBaseElementBinding::Wrap)
}

/// https://html.spec.whatwg.org/multipage/#frozen-base-url
pub fn frozen_base_url(&self) -> Url {
let href = ElementCast::from_ref(self).get_attribute(&ns!(""), &atom!("href"))
.expect("The frozen base url is only defined for base elements \
that have a base url.");
let base = document_from_node(self).fallback_base_url();
let parsed = UrlParser::new().base_url(&base).parse(&href.value());
parsed.unwrap_or(base)
}

/// Update the cached base element in response to adding or removing an
/// attribute.
pub fn add_remove_attr(&self, attr: &Attr) {
if *attr.local_name() == atom!("href") {
let document = document_from_node(self);
document.refresh_base_element();
}
}

/// 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 {
return;
}

if ElementCast::from_ref(self).has_attribute(&atom!("href")) {
let document = document_from_node(self);
document.refresh_base_element();
}
}
}

impl<'a> VirtualMethods for &'a HTMLBaseElement {
fn super_type<'b>(&'b self) -> Option<&'b VirtualMethods> {
Some(HTMLElementCast::from_borrowed_ref(self) as &VirtualMethods)
}

fn after_set_attr(&self, attr: &Attr) {
self.super_type().unwrap().after_set_attr(attr);
self.add_remove_attr(attr);
}

fn before_remove_attr(&self, attr: &Attr) {
self.super_type().unwrap().before_remove_attr(attr);
self.add_remove_attr(attr);
}

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 unbind_from_tree(&self, tree_in_doc: bool) {
self.super_type().unwrap().unbind_from_tree(tree_in_doc);
self.bind_unbind(tree_in_doc);
}
}
5 changes: 5 additions & 0 deletions servo/components/script/dom/virtualmethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use dom::bindings::codegen::InheritTypes::ElementCast;
use dom::bindings::codegen::InheritTypes::HTMLAnchorElementCast;
use dom::bindings::codegen::InheritTypes::HTMLAreaElementCast;
use dom::bindings::codegen::InheritTypes::HTMLAppletElementCast;
use dom::bindings::codegen::InheritTypes::HTMLBaseElementCast;
use dom::bindings::codegen::InheritTypes::HTMLBodyElementCast;
use dom::bindings::codegen::InheritTypes::HTMLButtonElementCast;
use dom::bindings::codegen::InheritTypes::HTMLCanvasElementCast;
Expand Down Expand Up @@ -138,6 +139,10 @@ pub fn vtable_for<'a>(node: &'a &'a Node) -> &'a (VirtualMethods + 'a) {
let element = HTMLAreaElementCast::to_borrowed_ref(node).unwrap();
element as &'a (VirtualMethods + 'a)
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBaseElement)) => {
let element = HTMLBaseElementCast::to_borrowed_ref(node).unwrap();
element as &'a (VirtualMethods + 'a)
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBodyElement)) => {
let element = HTMLBodyElementCast::to_borrowed_ref(node).unwrap();
element as &'a (VirtualMethods + 'a)
Expand Down

0 comments on commit b7fff47

Please sign in to comment.