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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/* 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/. */
//! DOM bindings for `CharacterData`.
use dom::bindings::codegen::InheritTypes::CharacterDataDerived;
use dom::bindings::js::JSRef;
use dom::bindings::error::{Fallible, ErrorResult, IndexSize};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{CommentNodeTypeId, Node, NodeTypeId, TextNodeTypeId, ProcessingInstructionNodeTypeId};
use servo_util::str::DOMString;
#[deriving(Encodable)]
pub struct CharacterData {
pub node: Node,
pub data: DOMString,
}
impl CharacterDataDerived for EventTarget {
fn is_characterdata(&self) -> bool {
match self.type_id {
NodeTargetTypeId(TextNodeTypeId) |
NodeTargetTypeId(CommentNodeTypeId) |
NodeTargetTypeId(ProcessingInstructionNodeTypeId) => true,
_ => false
}
}
}
impl CharacterData {
pub fn new_inherited(id: NodeTypeId, data: DOMString, document: &JSRef<Document>) -> CharacterData {
CharacterData {
node: Node::new_inherited(id, document),
data: data
}
}
}
pub trait CharacterDataMethods {
fn Data(&self) -> DOMString;
fn SetData(&mut self, arg: DOMString) -> ErrorResult;
fn Length(&self) -> u32;
fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString>;
fn AppendData(&mut self, arg: DOMString) -> ErrorResult;
fn InsertData(&mut self, _offset: u32, _arg: DOMString) -> ErrorResult;
fn DeleteData(&mut self, _offset: u32, _count: u32) -> ErrorResult;
fn ReplaceData(&mut self, _offset: u32, _count: u32, _arg: DOMString) -> ErrorResult;
}
impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
fn Data(&self) -> DOMString {
self.data.clone()
}
fn SetData(&mut self, arg: DOMString) -> ErrorResult {
self.data = arg;
Ok(())
}
fn Length(&self) -> u32 {
self.data.len() as u32
}
fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString> {
Ok(self.data.slice(offset as uint, count as uint).to_str())
}
fn AppendData(&mut self, arg: DOMString) -> ErrorResult {
self.data.push_str(arg);
Ok(())
}
fn InsertData(&mut self, offset: u32, arg: DOMString) -> ErrorResult {
self.ReplaceData(offset, 0, arg)
}
fn DeleteData(&mut self, offset: u32, count: u32) -> ErrorResult {
self.ReplaceData(offset, count, ~"")
}
fn ReplaceData(&mut self, offset: u32, count: u32, arg: DOMString) -> ErrorResult {
let length = self.data.len() as u32;
if offset > length {
return Err(IndexSize);
}
let count = if offset + count > length {
length - offset
} else {
count
};
let mut data = self.data.slice(0, offset as uint).to_owned();
data.push_str(arg);
data.push_str(self.data.slice((offset + count) as uint, length as uint));
self.data = data;
// FIXME: Once we have `Range`, we should implement step7 to step11
Ok(())
}
}
impl Reflectable for CharacterData {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.node.reflector()
}
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
self.node.mut_reflector()
}
}
|