mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Merge pull request #3088 from brunoabinader/document-links-cache
Implement Document.links cache; r=Ms2ger
This commit is contained in:
commit
7cd57870b4
2 changed files with 52 additions and 22 deletions
|
@ -78,6 +78,7 @@ pub struct Document {
|
||||||
pub is_html_document: bool,
|
pub is_html_document: bool,
|
||||||
url: Untraceable<Url>,
|
url: Untraceable<Url>,
|
||||||
quirks_mode: Untraceable<Cell<QuirksMode>>,
|
quirks_mode: Untraceable<Cell<QuirksMode>>,
|
||||||
|
links: Cell<Option<JS<HTMLCollection>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DocumentDerived for EventTarget {
|
impl DocumentDerived for EventTarget {
|
||||||
|
@ -86,6 +87,13 @@ impl DocumentDerived for EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct LinksFilter;
|
||||||
|
impl CollectionFilter for LinksFilter {
|
||||||
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
(elem.is_htmlanchorelement() || elem.is_htmlareaelement()) && elem.has_attribute("href")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait DocumentHelpers {
|
pub trait DocumentHelpers {
|
||||||
fn url<'a>(&'a self) -> &'a Url;
|
fn url<'a>(&'a self) -> &'a Url;
|
||||||
fn quirks_mode(&self) -> QuirksMode;
|
fn quirks_mode(&self) -> QuirksMode;
|
||||||
|
@ -228,6 +236,7 @@ impl Document {
|
||||||
// http://dom.spec.whatwg.org/#concept-document-encoding
|
// http://dom.spec.whatwg.org/#concept-document-encoding
|
||||||
encoding_name: Traceable::new(RefCell::new("utf-8".to_string())),
|
encoding_name: Traceable::new(RefCell::new("utf-8".to_string())),
|
||||||
is_html_document: is_html_document == HTMLDocument,
|
is_html_document: is_html_document == HTMLDocument,
|
||||||
|
links: Cell::new(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -657,8 +666,6 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
|
|
||||||
fn Images(&self) -> Temporary<HTMLCollection> {
|
fn Images(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
let window = self.window.root();
|
||||||
|
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
struct ImagesFilter;
|
struct ImagesFilter;
|
||||||
impl CollectionFilter for ImagesFilter {
|
impl CollectionFilter for ImagesFilter {
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
@ -671,8 +678,6 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
|
|
||||||
fn Embeds(&self) -> Temporary<HTMLCollection> {
|
fn Embeds(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
let window = self.window.root();
|
||||||
|
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
struct EmbedsFilter;
|
struct EmbedsFilter;
|
||||||
impl CollectionFilter for EmbedsFilter {
|
impl CollectionFilter for EmbedsFilter {
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
@ -684,29 +689,21 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Plugins(&self) -> Temporary<HTMLCollection> {
|
fn Plugins(&self) -> Temporary<HTMLCollection> {
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
self.Embeds()
|
self.Embeds()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Links(&self) -> Temporary<HTMLCollection> {
|
fn Links(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
if self.links.get().is_none() {
|
||||||
|
let window = self.window.root();
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
let root = NodeCast::from_ref(self);
|
||||||
struct LinksFilter;
|
let filter = box LinksFilter;
|
||||||
impl CollectionFilter for LinksFilter {
|
self.links.assign(Some(HTMLCollection::create(&*window, root, filter)));
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
|
||||||
(elem.is_htmlanchorelement() || elem.is_htmlareaelement()) &&
|
|
||||||
elem.get_attribute(Null, "href").is_some()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let filter = box LinksFilter;
|
Temporary::new(self.links.get().get_ref().clone())
|
||||||
HTMLCollection::create(&*window, NodeCast::from_ref(self), filter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Forms(&self) -> Temporary<HTMLCollection> {
|
fn Forms(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
let window = self.window.root();
|
||||||
|
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
struct FormsFilter;
|
struct FormsFilter;
|
||||||
impl CollectionFilter for FormsFilter {
|
impl CollectionFilter for FormsFilter {
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
@ -719,8 +716,6 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
|
|
||||||
fn Scripts(&self) -> Temporary<HTMLCollection> {
|
fn Scripts(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
let window = self.window.root();
|
||||||
|
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
struct ScriptsFilter;
|
struct ScriptsFilter;
|
||||||
impl CollectionFilter for ScriptsFilter {
|
impl CollectionFilter for ScriptsFilter {
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
@ -733,8 +728,6 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
|
||||||
|
|
||||||
fn Anchors(&self) -> Temporary<HTMLCollection> {
|
fn Anchors(&self) -> Temporary<HTMLCollection> {
|
||||||
let window = self.window.root();
|
let window = self.window.root();
|
||||||
|
|
||||||
// FIXME: https://github.com/mozilla/servo/issues/1847
|
|
||||||
struct AnchorsFilter;
|
struct AnchorsFilter;
|
||||||
impl CollectionFilter for AnchorsFilter {
|
impl CollectionFilter for AnchorsFilter {
|
||||||
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool {
|
||||||
|
|
37
src/test/content/test_document_links_cache.html
Normal file
37
src/test/content/test_document_links_cache.html
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="harness.js"></script>
|
||||||
|
<script>
|
||||||
|
var links = document.links;
|
||||||
|
is(links, document.links);
|
||||||
|
is(links.length, 0);
|
||||||
|
|
||||||
|
var anchor = document.createElement("a");
|
||||||
|
anchor.id = "anchor-with-href";
|
||||||
|
anchor.setAttribute("href", "http://www.google.com");
|
||||||
|
document.body.appendChild(anchor);
|
||||||
|
is(links.length, 1);
|
||||||
|
|
||||||
|
anchor = document.createElement("a");
|
||||||
|
anchor.id = "anchor-without-href";
|
||||||
|
document.body.appendChild(anchor);
|
||||||
|
is(links.length, 1);
|
||||||
|
|
||||||
|
anchor.setAttribute("href", "http://www.google.com");
|
||||||
|
is(links.length, 2);
|
||||||
|
|
||||||
|
anchor.removeAttribute("href", "http://www.google.com");
|
||||||
|
is(links.length, 1);
|
||||||
|
|
||||||
|
document.body.removeChild(document.getElementById("anchor-without-href"));
|
||||||
|
is(links.length, 1);
|
||||||
|
|
||||||
|
document.body.removeChild(document.getElementById("anchor-with-href"));
|
||||||
|
is(links, document.links);
|
||||||
|
is(links.length, 0);
|
||||||
|
|
||||||
|
finish();
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue