mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Auto merge of #7046 - Ms2ger:base-url, r=dzbarsky
Implement a base_url getter and use it for style attributes. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7046) <!-- Reviewable:end -->
This commit is contained in:
commit
530d4547c9
5 changed files with 120 additions and 8 deletions
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -1089,6 +1135,7 @@ impl Document {
|
|||
loader: DOMRefCell::new(doc_loader),
|
||||
current_parser: Default::default(),
|
||||
reflow_timeout: Cell::new(None),
|
||||
base_element: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue