-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathminidom.rs
254 lines (219 loc) · 7.63 KB
/
minidom.rs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 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 josephine::CanAccess;
use josephine::CanAlloc;
use josephine::Compartment;
use josephine::InCompartment;
use josephine::JSContext;
use josephine::JSInitializable;
use josephine::JSManaged;
use josephine::JSLifetime;
use josephine::JSString;
use josephine::SOMEWHERE;
use fake_codegen::ConsoleInitializer;
use fake_codegen::ConsoleMethods;
use fake_codegen::DocumentInitializer;
use fake_codegen::DocumentMethods;
use fake_codegen::ElementInitializer;
use fake_codegen::ElementMethods;
use fake_codegen::WindowInitializer;
use fake_codegen::WindowMethods;
// -------------------------------------------------------------------
// TODO: the contents are pub so that codegen can get at it, this should be fixed!
// https://github.com/asajeffrey/josephine/issues/27
#[derive(Copy, Clone, Debug, Eq, PartialEq, JSTraceable, JSLifetime, JSCompartmental)]
pub struct Window<'a, C> (pub JSManaged<'a, C, NativeWindow<'a, C>>);
#[derive(JSTraceable, JSLifetime, JSCompartmental)]
pub struct NativeWindow<'a, C> {
console: Console<'a, C>,
document: Document<'a, C>,
}
impl<'a, C> JSInitializable for NativeWindow<'a, C> {
type Init = WindowInitializer;
}
impl<'a> Window<'a, SOMEWHERE> {
pub fn new<S>(cx: &'a mut JSContext<S>) -> Window<'a, SOMEWHERE> where
S: CanAccess + CanAlloc,
{
let mut cx = cx.create_compartment();
let ref mut console_root = cx.new_root();
let ref mut document_root = cx.new_root();
let console = Console::new(&mut cx).in_root(console_root);
let document = Document::new(&mut cx).in_root(document_root);
let cx = cx.global_manage(NativeWindow {
console: console,
document: document,
});
Window(cx.global().forget_compartment())
}
}
impl<'a, C> WindowMethods<'a, C> for Window<'a, C> where C: 'a {
fn Console<S>(self, cx: &'a JSContext<S>) -> Console<'a, C> where
S: CanAccess,
C: Compartment,
{
self.0.borrow(cx).console
}
fn Document<S>(self, cx: &'a JSContext<S>) -> Document<'a, C> where
S: CanAccess,
C: Compartment,
{
self.0.borrow(cx).document
}
fn Window<S>(self, _cx: &'a JSContext<S>) -> Window<'a, C> {
self
}
}
// -------------------------------------------------------------------
#[derive(Copy, Clone, JSTraceable, JSLifetime, JSCompartmental)]
pub struct Console<'a, C> (pub JSManaged<'a, C, NativeConsole>);
#[derive(JSTraceable, JSLifetime, JSCompartmental)]
pub struct NativeConsole(());
impl JSInitializable for NativeConsole {
type Init = ConsoleInitializer;
}
impl<'a, C> Console<'a, C> {
fn new<S>(cx: &'a mut JSContext<S>) -> Console<'a, C> where
S: CanAlloc + InCompartment<C>,
C: Compartment,
{
Console(cx.manage(NativeConsole(())))
}
}
impl<'a, C> ConsoleMethods<'a, C> for Console<'a, C> {
fn Log<S>(self, _cx: &mut JSContext<S>, arg: JSString<'a, C>) {
debug!("Logging");
println!("{}", arg);
}
}
// -------------------------------------------------------------------
#[derive(Copy, Clone, Debug, Eq, PartialEq, JSTraceable, JSLifetime, JSCompartmental)]
pub struct Document<'a, C> (pub JSManaged<'a, C, NativeDocument<'a, C>>);
#[derive(JSTraceable, JSLifetime, JSCompartmental)]
pub struct NativeDocument<'a, C> {
body: Element<'a, C>,
}
impl<'a, C> JSInitializable for NativeDocument<'a, C> {
type Init = DocumentInitializer;
}
impl<'a, C:'a> Document<'a, C> {
pub fn new<S>(cx: &'a mut JSContext<S>) -> Document<'a, C> where
S: CanAlloc + InCompartment<C>,
C: Compartment,
{
let ref mut root1 = cx.new_root();
let ref mut root2 = cx.new_root();
let name = JSString::from_str(cx, "body").in_root(root1);
let body = Element::new(cx, name).in_root(root2);
Document(cx.manage(NativeDocument {
body: body,
}))
}
}
impl<'a, C> DocumentMethods<'a, C> for Document<'a, C> where C: 'a {
fn Body<S>(self, cx: &'a mut JSContext<S>) -> Element<'a, C> where
S: CanAccess,
C: Compartment,
{
self.0.borrow(cx).body
}
}
// -------------------------------------------------------------------
#[derive(Copy, Clone, Debug, Eq, PartialEq, JSTraceable, JSLifetime, JSCompartmental)]
pub struct Element<'a, C> (pub JSManaged<'a, C, NativeElement<'a, C>>);
#[derive(JSTraceable, JSLifetime, JSCompartmental)]
pub struct NativeElement<'a, C> {
name: JSString<'a, C>,
parent: Option<Element<'a, C>>,
children: Vec<Element<'a, C>>,
}
impl<'a, C> JSInitializable for NativeElement<'a, C> {
type Init = ElementInitializer;
}
impl<'a, C:'a> Element<'a, C> {
pub fn new<S>(cx: &'a mut JSContext<S>, name: JSString<C>) -> Element<'a, C> where
S: CanAlloc + InCompartment<C>,
C: Compartment,
{
Element(cx.manage(NativeElement {
name: name,
parent: None,
children: Vec::new(),
}))
}
fn shallow_clone<S, D:'a>(self, cx: &'a mut JSContext<S>) -> Element<'a, D> where
S: CanAccess + CanAlloc + InCompartment<D>,
C: Compartment,
D: Compartment,
{
let ref mut root1 = cx.new_root();
let ref mut root2 = cx.new_root();
let name = self.0.borrow(cx).name.in_root(root1).clone_in(cx).in_root(root2);
Element::new(cx, name)
}
fn clone_children_from<S, D:'a>(self, cx: &mut JSContext<S>, element: Element<D>) where
S: CanAccess + CanAlloc + InCompartment<C>,
C: Compartment,
D: Compartment,
{
let mut i = 0;
loop {
let ref mut root = cx.new_root();
let child = match element.0.borrow(cx).children.get(i).cloned().in_root(root) {
None => return,
Some(child) => child,
};
self.append_clone(cx, child);
i = i+1;
}
}
fn append_clone<S, D:'a>(self, cx: &mut JSContext<S>, child: Element<D>) where
S: CanAccess + CanAlloc + InCompartment<C>,
C: Compartment,
D: Compartment,
{
let ref mut root = cx.new_root();
let clone = child.shallow_clone(cx).in_root(root);
clone.clone_children_from(cx, child);
self.append_child(cx, clone);
}
fn append_child<S>(self, cx: &'a mut JSContext<S>, child: Element<'a, C>) where
S: CanAccess + CanAlloc + InCompartment<C>,
C: Compartment,
{
self.0.borrow_mut(cx).children.push(child);
child.0.borrow_mut(cx).parent = Some(self);
}
fn in_compartment<S, D>(self, cx: &JSContext<S>) -> Option<Element<'a, D>> where
S: InCompartment<D>,
{
self.0.in_compartment(cx).map(Element)
}
}
impl<'a, C> ElementMethods<'a, C> for Element<'a, C> where C: 'a {
fn Append<S, D>(self, cx: &'a mut JSContext<S>, child: Element<'a, D>) where
S: CanAccess + CanAlloc,
C: Compartment,
D: Compartment,
{
let ref mut cx = cx.enter_known_compartment(self.0);
if let Some(child) = child.in_compartment(cx) {
self.append_child(cx, child);
} else {
self.append_clone(cx, child);
}
}
fn Parent<S>(self, cx: &'a mut JSContext<S>) -> Option<Element<'a, C>> where
S: CanAccess,
C: Compartment,
{
self.0.borrow(cx).parent
}
fn TagName<S>(self, cx: &'a mut JSContext<S>) -> JSString<'a, C> where
S: CanAccess,
C: Compartment,
{
self.0.borrow(cx).name
}
}