1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use webcore::value::Reference;
use webapi::event_target::{IEventTarget, EventTarget};
use webapi::node::{INode, Node};
use webapi::element::Element;
use webapi::text_node::TextNode;
use webapi::location::Location;
use webapi::parent_node::IParentNode;
use webapi::non_element_parent_node::INonElementParentNode;
use private::TODO;
/// The `Document` interface represents any web page loaded in the browser and
/// serves as an entry point into the web page's content, which is the DOM tree.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document)
// https://dom.spec.whatwg.org/#document
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "Document")]
#[reference(subclass_of(EventTarget, Node))]
pub struct Document( Reference );
impl IEventTarget for Document {}
impl IParentNode for Document {}
impl INode for Document {}
impl INonElementParentNode for Document {}
/// A global instance of [Document](struct.Document.html).
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document)
pub fn document() -> Document {
unsafe { js!( return document; ).into_reference_unchecked() }.unwrap()
}
impl Document {
/// In an HTML document, the Document.createElement() method creates the HTML
/// element specified by `tag`, or an HTMLUnknownElement if `tag` isn't
/// recognized. In other documents, it creates an element with a null namespace URI.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement)
// https://dom.spec.whatwg.org/#ref-for-dom-document-createelement
pub fn create_element( &self, tag: &str ) -> Result< Element, TODO > {
unsafe {
Ok( js!( return @{self}.createElement( @{tag} ); ).into_reference_unchecked().unwrap() )
}
}
/// Creates a new text node.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/createTextNode)
// https://dom.spec.whatwg.org/#ref-for-dom-document-createtextnode
pub fn create_text_node( &self, text: &str ) -> TextNode {
unsafe {
js!( return @{self}.createTextNode( @{text} ); ).into_reference_unchecked().unwrap()
}
}
/// Returns a [Location](struct.Location.html) object which contains
/// information about the URL of the document and provides methods
/// for changing that URL and loading another URL.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/location)
// https://html.spec.whatwg.org/#the-document-object:dom-document-location
pub fn location( &self ) -> Option< Location > {
unsafe {
js!(
return @{self}.location;
).into_reference_unchecked()
}
}
}