gecko-dev/servo/src/components/script/dom/htmldocument.rs
Patrick Walton cf773ab0f2 servo: Merge #1424 - Harden layout (from pcwalton:harden-layout); r=pcwalton
This changeset gets rid of the `FooView` phantom type in favor of a more brute force approach that just whitelists methods that layout is allowed to call. The set is surprisingly small now that layout isn't going to the DOM for much.

If this approach turns out not to scale, we can do something fancier, but I'd rather just have it be safe and secure first and then refactor later for programmer happiness.

r? @kmcallister

Source-Repo: https://github.com/servo/servo
Source-Revision: 824c7ac613ebb80bb432ff6425c5e25c642b6afb
2013-12-17 18:16:05 -08:00

95 lines
2.9 KiB
Rust

/* 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::bindings::codegen::HTMLDocumentBinding;
use dom::bindings::utils::{Reflectable, Reflector, Traceable};
use dom::document::{AbstractDocument, Document, HTML};
use dom::element::HTMLHeadElementTypeId;
use dom::htmlcollection::HTMLCollection;
use dom::node::{AbstractNode, ElementNodeTypeId};
use dom::window::Window;
use js::jsapi::JSTracer;
use std::str::eq_slice;
use style::TElement;
pub struct HTMLDocument {
parent: Document
}
impl HTMLDocument {
pub fn new_inherited(window: @mut Window) -> HTMLDocument {
HTMLDocument {
parent: Document::new_inherited(window, HTML)
}
}
pub fn new(window: @mut Window) -> AbstractDocument {
let document = HTMLDocument::new_inherited(window);
Document::reflect_document(@mut document, window, HTMLDocumentBinding::Wrap)
}
}
impl HTMLDocument {
pub fn GetHead(&self) -> Option<AbstractNode> {
match self.parent.GetDocumentElement() {
None => None,
Some(root) => root.traverse_preorder().find(|child| {
child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId)
})
}
}
pub fn Images(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "img"))
}
pub fn Embeds(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "embed"))
}
pub fn Plugins(&self) -> @mut HTMLCollection {
self.Embeds()
}
pub fn Links(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem|
(eq_slice(elem.tag_name, "a") || eq_slice(elem.tag_name, "area"))
&& elem.get_attr(None, "href").is_some())
}
pub fn Forms(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "form"))
}
pub fn Scripts(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "script"))
}
pub fn Anchors(&self) -> @mut HTMLCollection {
self.parent.createHTMLCollection(|elem|
eq_slice(elem.tag_name, "a") && elem.get_attr(None, "name").is_some())
}
pub fn Applets(&self) -> @mut HTMLCollection {
// FIXME: This should be return OBJECT elements containing applets.
self.parent.createHTMLCollection(|elem| eq_slice(elem.tag_name, "applet"))
}
}
impl Reflectable for HTMLDocument {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.parent.reflector()
}
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
self.parent.mut_reflector()
}
}
impl Traceable for HTMLDocument {
fn trace(&self, tracer: *mut JSTracer) {
self.parent.trace(tracer);
}
}