mirror of
https://github.com/servo/servo.git
synced 2025-06-21 07:38:59 +01:00
Add document.createElement and document.createTextNode.
This re-uses the parser's node creation code. That could probably be put somewhere nicer. Suggestions welcome!
This commit is contained in:
parent
2d556303ca
commit
227bb95213
4 changed files with 34 additions and 6 deletions
|
@ -45,10 +45,10 @@ interface Document /*: Node*/ { //XXXjdm Requires servo/#623
|
|||
[Creator, Throws]
|
||||
Element createElementNS(DOMString? namespace, DOMString qualifiedName);
|
||||
/*[Creator]
|
||||
DocumentFragment createDocumentFragment();
|
||||
DocumentFragment createDocumentFragment();*/
|
||||
[Creator]
|
||||
Text createTextNode(DOMString data);
|
||||
[Creator]
|
||||
/*[Creator]
|
||||
Comment createComment(DOMString data);
|
||||
[Creator, Throws]
|
||||
ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);*/
|
||||
|
|
|
@ -17,7 +17,7 @@ use dom::text::Text;
|
|||
use dom::window::Window;
|
||||
use dom::windowproxy::WindowProxy;
|
||||
use dom::htmltitleelement::HTMLTitleElement;
|
||||
|
||||
use html::hubbub_html_parser::build_element_from_tag;
|
||||
use js::jsapi::{JS_AddObjectRoot, JS_RemoveObjectRoot, JSObject, JSContext, JSVal};
|
||||
use js::glue::RUST_OBJECT_TO_JSVAL;
|
||||
use servo_util::tree::TreeNodeRef;
|
||||
|
@ -213,6 +213,11 @@ impl Document {
|
|||
Some(self.root)
|
||||
}
|
||||
|
||||
fn get_cx(&self) -> *JSContext {
|
||||
let win = self.window.get_ref();
|
||||
unsafe {(*win.page).js_info.get_ref().js_compartment.cx.ptr}
|
||||
}
|
||||
|
||||
fn get_scope_and_cx(&self) -> (*JSObject, *JSContext) {
|
||||
let win = self.window.get_ref();
|
||||
let cx = unsafe {(*win.page).js_info.get_ref().js_compartment.cx.ptr};
|
||||
|
@ -239,14 +244,20 @@ impl Document {
|
|||
None
|
||||
}
|
||||
|
||||
pub fn CreateElement(&self, _local_name: &DOMString, _rv: &mut ErrorResult) -> AbstractNode<ScriptView> {
|
||||
fail!("stub")
|
||||
pub fn CreateElement(&self, local_name: &DOMString, _rv: &mut ErrorResult) -> AbstractNode<ScriptView> {
|
||||
let cx = self.get_cx();
|
||||
build_element_from_tag(cx, local_name.to_str())
|
||||
}
|
||||
|
||||
pub fn CreateElementNS(&self, _namespace: &DOMString, _qualified_name: &DOMString, _rv: &mut ErrorResult) -> AbstractNode<ScriptView> {
|
||||
fail!("stub")
|
||||
}
|
||||
|
||||
pub fn CreateTextNode(&self, data: &DOMString) -> AbstractNode<ScriptView> {
|
||||
let cx = self.get_cx();
|
||||
unsafe { Node::as_abstract_node(cx, @Text::new(data.to_str())) }
|
||||
}
|
||||
|
||||
pub fn CreateEvent(&self, _interface: &DOMString, _rv: &mut ErrorResult) -> @mut Event {
|
||||
fail!("stub")
|
||||
}
|
||||
|
|
|
@ -209,7 +209,7 @@ fn js_script_listener(to_parent: SharedChan<HtmlDiscoveryMessage>,
|
|||
// Silly macros to handle constructing DOM nodes. This produces bad code and should be optimized
|
||||
// via atomization (issue #85).
|
||||
|
||||
fn build_element_from_tag(cx: *JSContext, tag: &str) -> AbstractNode<ScriptView> {
|
||||
pub fn build_element_from_tag(cx: *JSContext, tag: &str) -> AbstractNode<ScriptView> {
|
||||
// TODO (Issue #85): use atoms
|
||||
handle_element!(cx, tag, "a", HTMLAnchorElementTypeId, HTMLAnchorElement, []);
|
||||
handle_element!(cx, tag, "applet", HTMLAppletElementTypeId, HTMLAppletElement, []);
|
||||
|
|
17
src/test/html/content/test_create_element.html
Normal file
17
src/test/html/content/test_create_element.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
<script src="harness.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
var elem = document.createElement("foo");
|
||||
is(elem.tagName, "FOO");
|
||||
var elem = document.createElement("p");
|
||||
is(elem instanceof HTMLParagraphElement, true);
|
||||
var text = document.createTextNode("hello");
|
||||
is(text instanceof Text, true);
|
||||
finish();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue