aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/html/hubbub_html_parser.rs
blob: 4b943a4409a1a32533b4c36e4b379349f15f4b2a (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* 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::attr::AttrHelpers;
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::{NodeBase, NodeCast, TextCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLScriptElementCast};
use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable, Root};
use dom::bindings::utils::Reflectable;
use dom::document::{Document, DocumentHelpers};
use dom::element::AttributeHandlers;
use dom::htmlelement::HTMLElement;
use dom::htmlheadingelement::{Heading1, Heading2, Heading3, Heading4, Heading5, Heading6};
use dom::htmlformelement::HTMLFormElement;
use dom::htmlscriptelement::HTMLScriptElementHelpers;
use dom::node::NodeHelpers;
use dom::types::*;
use page::Page;

use encoding::all::UTF_8;
use encoding::types::{Encoding, DecodeReplace};

use hubbub::hubbub;
use hubbub::hubbub::{NullNs, HtmlNs, MathMlNs, SvgNs, XLinkNs, XmlNs, XmlNsNs};
use servo_net::resource_task::{Load, LoadData, Payload, Done, ResourceTask, load_whole_resource};
use servo_msg::constellation_msg::LoadData as MsgLoadData;
use servo_util::str::DOMString;
use servo_util::task::spawn_named;
use std::ascii::StrAsciiExt;
use std::mem;
use std::cell::RefCell;
use std::comm::{channel, Sender, Receiver};
use url::{Url, UrlParser};
use http::headers::HeaderEnum;
use time;
use string_cache::{Atom, Namespace};

macro_rules! handle_element(
    ($document: expr,
     $localName: expr,
     $prefix: expr,
     $string: expr,
     $ctor: ident
     $(, $arg:expr )*) => (
        if $string == $localName.as_slice() {
            return ElementCast::from_temporary($ctor::new($localName, $prefix, $document $(, $arg)*));
        }
    )
)


pub struct JSFile {
    pub data: String,
    pub url: Option<Url>,
}

pub type JSResult = Vec<JSFile>;

pub enum HTMLInput {
    InputString(String),
    InputUrl(Url),
}

enum JSMessage {
    JSTaskNewFile(Url),
    JSTaskNewInlineScript(String, Option<Url>),
    JSTaskExit
}

/// Messages generated by the HTML parser upon discovery of additional resources
pub enum HtmlDiscoveryMessage {
    HtmlDiscoveredScript(JSResult)
}

pub struct HtmlParserResult {
    pub discovery_port: Receiver<HtmlDiscoveryMessage>,
}

trait NodeWrapping<T> {
    unsafe fn to_hubbub_node(&self) -> hubbub::NodeDataPtr;
}

impl<'a, T: NodeBase+Reflectable> NodeWrapping<T> for JSRef<'a, T> {
    unsafe fn to_hubbub_node(&self) -> hubbub::NodeDataPtr {
        mem::transmute(self.deref())
    }
}

unsafe fn from_hubbub_node<T: Reflectable>(n: hubbub::NodeDataPtr) -> Temporary<T> {
    Temporary::new(JS::from_raw(mem::transmute(n)))
}

fn js_script_listener(to_parent: Sender<HtmlDiscoveryMessage>,
                      from_parent: Receiver<JSMessage>,
                      resource_task: ResourceTask) {
    let mut result_vec = vec!();

    loop {
        match from_parent.recv_opt() {
            Ok(JSTaskNewFile(url)) => {
                match load_whole_resource(&resource_task, url.clone()) {
                    Err(_) => {
                        error!("error loading script {:s}", url.serialize());
                    }
                    Ok((metadata, bytes)) => {
                        let decoded = UTF_8.decode(bytes.as_slice(), DecodeReplace).unwrap();
                        result_vec.push(JSFile {
                            data: decoded.to_string(),
                            url: Some(metadata.final_url),
                        });
                    }
                }
            }
            Ok(JSTaskNewInlineScript(data, url)) => {
                result_vec.push(JSFile { data: data, url: url });
            }
            Ok(JSTaskExit) | Err(()) => {
                break;
            }
        }
    }

    assert!(to_parent.send_opt(HtmlDiscoveredScript(result_vec)).is_ok());
}

// Parses an RFC 2616 compliant date/time string, and returns a localized
// date/time string in a format suitable for document.lastModified.
fn parse_last_modified(timestamp: &str) -> String {
    let format = "%m/%d/%Y %H:%M:%S";

    // RFC 822, updated by RFC 1123
    match time::strptime(timestamp, "%a, %d %b %Y %T %Z") {
        Ok(t) => return t.to_local().strftime(format),
        Err(_) => ()
    }

    // RFC 850, obsoleted by RFC 1036
    match time::strptime(timestamp, "%A, %d-%b-%y %T %Z") {
        Ok(t) => return t.to_local().strftime(format),
        Err(_) => ()
    }

    // ANSI C's asctime() format
    match time::strptime(timestamp, "%c") {
        Ok(t) => t.to_local().strftime(format),
        Err(_) => String::from_str("")
    }
}

// Silly macros to handle constructing      DOM nodes. This produces bad code and should be optimized
// via atomization (issue #85).

pub fn build_element_from_tag(tag: DOMString, ns: Namespace, prefix: Option<DOMString>, document: JSRef<Document>) -> Temporary<Element> {
    if ns != ns!(HTML) {
        return Element::new(tag, ns, prefix, document);
    }

    // TODO (Issue #85): use atoms
    handle_element!(document, tag, prefix, "a",         HTMLAnchorElement);
    handle_element!(document, tag, prefix, "abbr",      HTMLElement);
    handle_element!(document, tag, prefix, "acronym",   HTMLElement);
    handle_element!(document, tag, prefix, "address",   HTMLElement);
    handle_element!(document, tag, prefix, "applet",    HTMLAppletElement);
    handle_element!(document, tag, prefix, "area",      HTMLAreaElement);
    handle_element!(document, tag, prefix, "article",   HTMLElement);
    handle_element!(document, tag, prefix, "aside",     HTMLElement);
    handle_element!(document, tag, prefix, "audio",     HTMLAudioElement);
    handle_element!(document, tag, prefix, "b",         HTMLElement);
    handle_element!(document, tag, prefix, "base",      HTMLBaseElement);
    handle_element!(document, tag, prefix, "bdi",       HTMLElement);
    handle_element!(document, tag, prefix, "bdo",       HTMLElement);
    handle_element!(document, tag, prefix, "bgsound",   HTMLElement);
    handle_element!(document, tag, prefix, "big",       HTMLElement);
    handle_element!(document, tag, prefix, "blockquote",HTMLElement);
    handle_element!(document, tag, prefix, "body",      HTMLBodyElement);
    handle_element!(document, tag, prefix, "br",        HTMLBRElement);
    handle_element!(document, tag, prefix, "button",    HTMLButtonElement);
    handle_element!(document, tag, prefix, "canvas",    HTMLCanvasElement);
    handle_element!(document, tag, prefix, "caption",   HTMLTableCaptionElement);
    handle_element!(document, tag, prefix, "center",    HTMLElement);
    handle_element!(document, tag, prefix, "cite",      HTMLElement);
    handle_element!(document, tag, prefix, "code",      HTMLElement);
    handle_element!(document, tag, prefix, "col",       HTMLTableColElement);
    handle_element!(document, tag, prefix, "colgroup",  HTMLTableColElement);
    handle_element!(document, tag, prefix, "data",      HTMLDataElement);
    handle_element!(document, tag, prefix, "datalist",  HTMLDataListElement);
    handle_element!(document, tag, prefix, "dd",        HTMLElement);
    handle_element!(document, tag, prefix, "del",       HTMLModElement);
    handle_element!(document, tag, prefix, "details",   HTMLElement);
    handle_element!(document, tag, prefix, "dfn",       HTMLElement);
    handle_element!(document, tag, prefix, "dir",       HTMLDirectoryElement);
    handle_element!(document, tag, prefix, "div",       HTMLDivElement);
    handle_element!(document, tag, prefix, "dl",        HTMLDListElement);
    handle_element!(document, tag, prefix, "dt",        HTMLElement);
    handle_element!(document, tag, prefix, "em",        HTMLElement);
    handle_element!(document, tag, prefix, "embed",     HTMLEmbedElement);
    handle_element!(document, tag, prefix, "fieldset",  HTMLFieldSetElement);
    handle_element!(document, tag, prefix, "figcaption",HTMLElement);
    handle_element!(document, tag, prefix, "figure",    HTMLElement);
    handle_element!(document, tag, prefix, "font",      HTMLFontElement);
    handle_element!(document, tag, prefix, "footer",    HTMLElement);
    handle_element!(document, tag, prefix, "form",      HTMLFormElement);
    handle_element!(document, tag, prefix, "frame",     HTMLFrameElement);
    handle_element!(document, tag, prefix, "frameset",  HTMLFrameSetElement);
    handle_element!(document, tag, prefix, "h1",        HTMLHeadingElement, Heading1);
    handle_element!(document, tag, prefix, "h2",        HTMLHeadingElement, Heading2);
    handle_element!(document, tag, prefix, "h3",        HTMLHeadingElement, Heading3);
    handle_element!(document, tag, prefix, "h4",        HTMLHeadingElement, Heading4);
    handle_element!(document, tag, prefix, "h5",        HTMLHeadingElement, Heading5);
    handle_element!(document, tag, prefix, "h6",        HTMLHeadingElement, Heading6);
    handle_element!(document, tag, prefix, "head",      HTMLHeadElement);
    handle_element!(document, tag, prefix, "header",    HTMLElement);
    handle_element!(document, tag, prefix, "hgroup",    HTMLElement);
    handle_element!(document, tag, prefix, "hr",        HTMLHRElement);
    handle_element!(document, tag, prefix, "html",      HTMLHtmlElement);
    handle_element!(document, tag, prefix, "i",         HTMLElement);
    handle_element!(document, tag, prefix, "iframe",    HTMLIFrameElement);
    handle_element!(document, tag, prefix, "img",       HTMLImageElement);
    handle_element!(document, tag, prefix, "input",     HTMLInputElement);
    handle_element!(document, tag, prefix, "ins",       HTMLModElement);
    handle_element!(document, tag, prefix, "isindex",   HTMLElement);
    handle_element!(document, tag, prefix, "kbd",       HTMLElement);
    handle_element!(document, tag, prefix, "label",     HTMLLabelElement);
    handle_element!(document, tag, prefix, "legend",    HTMLLegendElement);
    handle_element!(document, tag, prefix, "li",        HTMLLIElement);
    handle_element!(document, tag, prefix, "link",      HTMLLinkElement);
    handle_element!(document, tag, prefix, "main",      HTMLElement);
    handle_element!(document, tag, prefix, "map",       HTMLMapElement);
    handle_element!(document, tag, prefix, "mark",      HTMLElement);
    handle_element!(document, tag, prefix, "marquee",   HTMLElement);
    handle_element!(document, tag, prefix, "meta",      HTMLMetaElement);
    handle_element!(document, tag, prefix, "meter",     HTMLMeterElement);
    handle_element!(document, tag, prefix, "nav",       HTMLElement);
    handle_element!(document, tag, prefix, "nobr",      HTMLElement);
    handle_element!(document, tag, prefix, "noframes",  HTMLElement);
    handle_element!(document, tag, prefix, "noscript",  HTMLElement);
    handle_element!(document, tag, prefix, "object",    HTMLObjectElement);
    handle_element!(document, tag, prefix, "ol",        HTMLOListElement);
    handle_element!(document, tag, prefix, "optgroup",  HTMLOptGroupElement);
    handle_element!(document, tag, prefix, "option",    HTMLOptionElement);
    handle_element!(document, tag, prefix, "output",    HTMLOutputElement);
    handle_element!(document, tag, prefix, "p",         HTMLParagraphElement);
    handle_element!(document, tag, prefix, "param",     HTMLParamElement);
    handle_element!(document, tag, prefix, "pre",       HTMLPreElement);
    handle_element!(document, tag, prefix, "progress",  HTMLProgressElement);
    handle_element!(document, tag, prefix, "q",         HTMLQuoteElement);
    handle_element!(document, tag, prefix, "rp",        HTMLElement);
    handle_element!(document, tag, prefix, "rt",        HTMLElement);
    handle_element!(document, tag, prefix, "ruby",      HTMLElement);
    handle_element!(document, tag, prefix, "s",         HTMLElement);
    handle_element!(document, tag, prefix, "samp",      HTMLElement);
    handle_element!(document, tag, prefix, "script",    HTMLScriptElement);
    handle_element!(document, tag, prefix, "section",   HTMLElement);
    handle_element!(document, tag, prefix, "select",    HTMLSelectElement);
    handle_element!(document, tag, prefix, "small",     HTMLElement);
    handle_element!(document, tag, prefix, "source",    HTMLSourceElement);
    handle_element!(document, tag, prefix, "spacer",    HTMLElement);
    handle_element!(document, tag, prefix, "span",      HTMLSpanElement);
    handle_element!(document, tag, prefix, "strike",    HTMLElement);
    handle_element!(document, tag, prefix, "strong",    HTMLElement);
    handle_element!(document, tag, prefix, "style",     HTMLStyleElement);
    handle_element!(document, tag, prefix, "sub",       HTMLElement);
    handle_element!(document, tag, prefix, "summary",   HTMLElement);
    handle_element!(document, tag, prefix, "sup",       HTMLElement);
    handle_element!(document, tag, prefix, "table",     HTMLTableElement);
    handle_element!(document, tag, prefix, "tbody",     HTMLTableSectionElement);
    handle_element!(document, tag, prefix, "td",        HTMLTableDataCellElement);
    handle_element!(document, tag, prefix, "template",  HTMLTemplateElement);
    handle_element!(document, tag, prefix, "textarea",  HTMLTextAreaElement);
    handle_element!(document, tag, prefix, "th",        HTMLTableHeaderCellElement);
    handle_element!(document, tag, prefix, "time",      HTMLTimeElement);
    handle_element!(document, tag, prefix, "title",     HTMLTitleElement);
    handle_element!(document, tag, prefix, "tr",        HTMLTableRowElement);
    handle_element!(document, tag, prefix, "tt",        HTMLElement);
    handle_element!(document, tag, prefix, "track",     HTMLTrackElement);
    handle_element!(document, tag, prefix, "u",         HTMLElement);
    handle_element!(document, tag, prefix, "ul",        HTMLUListElement);
    handle_element!(document, tag, prefix, "var",       HTMLElement);
    handle_element!(document, tag, prefix, "video",     HTMLVideoElement);
    handle_element!(document, tag, prefix, "wbr",       HTMLElement);

    return ElementCast::from_temporary(HTMLUnknownElement::new(tag, prefix, document));
}

// The url from msg_load_data is ignored here
pub fn parse_html(page: &Page,
                  document: JSRef<Document>,
                  input: HTMLInput,
                  resource_task: ResourceTask,
                  msg_load_data: Option<MsgLoadData>)
                  -> HtmlParserResult {
    debug!("Hubbub: parsing {:?}", input);

    // Spawn a JS parser to receive JavaScript.
    let (discovery_chan, discovery_port) = channel();
    let resource_task2 = resource_task.clone();
    let js_result_chan = discovery_chan.clone();
    let (js_chan, js_msg_port) = channel();
    spawn_named("parse_html:js", proc() {
        js_script_listener(js_result_chan, js_msg_port, resource_task2.clone());
    });

    let (base_url, load_response) = match input {
        InputUrl(ref url) => {
            // Wait for the LoadResponse so that the parser knows the final URL.
            let (input_chan, input_port) = channel();
            let mut load_data = LoadData::new(url.clone());
            msg_load_data.map(|m| {
                load_data.headers = m.headers;
                load_data.method = m.method;
                load_data.data = m.data;
            });
            resource_task.send(Load(load_data, input_chan));

            let load_response = input_port.recv();

            debug!("Fetched page; metadata is {:?}", load_response.metadata);

            load_response.metadata.headers.as_ref().map(|headers| {
                let header = headers.iter().find(|h|
                    h.header_name().as_slice().to_ascii_lower() == "last-modified".to_string()
                );

                match header {
                    Some(h) => document.set_last_modified(
                        parse_last_modified(h.header_value().as_slice())),
                    None => {},
                };
            });

            let base_url = load_response.metadata.final_url.clone();

            {
                // Store the final URL before we start parsing, so that DOM routines
                // (e.g. HTMLImageElement::update_image) can resolve relative URLs
                // correctly.
                *page.mut_url() = Some((base_url.clone(), true));
            }

            (Some(base_url), Some(load_response))
        },
        InputString(_) => {
            match *page.url() {
                Some((ref page_url, _)) => (Some(page_url.clone()), None),
                None => (None, None),
            }
        },
    };

    let mut parser = build_parser(unsafe { document.to_hubbub_node() });
    debug!("created parser");

    let js_chan2 = js_chan.clone();

    let doc_cell = RefCell::new(document);

    let mut tree_handler = hubbub::TreeHandler {
        create_comment: |data: String| {
            debug!("create comment");
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow();
            let tmp = &*tmp_borrow;
            let comment = Comment::new(data, *tmp).root();
            let comment: JSRef<Node> = NodeCast::from_ref(*comment);
            unsafe { comment.to_hubbub_node() }
        },
        create_doctype: |box hubbub::Doctype { name: name, public_id: public_id, system_id: system_id, ..}: Box<hubbub::Doctype>| {
            debug!("create doctype");
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow();
            let tmp = &*tmp_borrow;
            let doctype_node = DocumentType::new(name, public_id, system_id, *tmp).root();
            unsafe {
                doctype_node.to_hubbub_node()
            }
        },
        create_element: |tag: Box<hubbub::Tag>| {
            debug!("create element {}", tag.name);
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow();
            let tmp = &*tmp_borrow;
            let namespace = match tag.ns {
                HtmlNs => ns!(HTML),
                MathMlNs => ns!(MathML),
                SvgNs => ns!(SVG),
                ns => fail!("Not expecting namespace {:?}", ns),
            };
            let element: Root<Element> = build_element_from_tag(tag.name.clone(), namespace, None, *tmp).root();

            debug!("-- attach attrs");
            for attr in tag.attributes.iter() {
                let (namespace, prefix) = match attr.ns {
                    NullNs => (ns!(""), None),
                    XLinkNs => (ns!(XLink), Some("xlink")),
                    XmlNs => (ns!(XML), Some("xml")),
                    XmlNsNs => (ns!(XMLNS), Some("xmlns")),
                    ns => fail!("Not expecting namespace {:?}", ns),
                };
                element.set_attribute_from_parser(Atom::from_slice(attr.name.as_slice()),
                                                  attr.value.clone(),
                                                  namespace,
                                                  prefix.map(|p| p.to_string()));
            }

            unsafe { element.to_hubbub_node() }
        },
        create_text: |data: String| {
            debug!("create text");
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow();
            let tmp = &*tmp_borrow;
            let text = Text::new(data, *tmp).root();
            unsafe { text.to_hubbub_node() }
        },
        ref_node: |_| {},
        unref_node: |_| {},
        append_child: |parent: hubbub::NodeDataPtr, child: hubbub::NodeDataPtr| {
            unsafe {
                debug!("append child {:x} {:x}", parent, child);
                let child: Root<Node> = from_hubbub_node(child).root();
                let parent: Root<Node> = from_hubbub_node(parent).root();
                assert!(parent.AppendChild(*child).is_ok());
            }
            child
        },
        insert_before: |_parent, _child| {
            debug!("insert before");
            0u
        },
        remove_child: |_parent, _child| {
            debug!("remove child");
            0u
        },
        clone_node: |_node, deep| {
            debug!("clone node");
            if deep { error!("-- deep clone unimplemented"); }
            fail!("clone node unimplemented")
        },
        reparent_children: |_node, _new_parent| {
            debug!("reparent children");
            0u
        },
        get_parent: |_node, _element_only| {
            debug!("get parent");
            0u
        },
        has_children: |_node| {
            debug!("has children");
            false
        },
        form_associate: |_form, _node| {
            debug!("form associate");
        },
        add_attributes: |_node, _attributes| {
            debug!("add attributes");
        },
        set_quirks_mode: |mode| {
            debug!("set quirks mode");
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow_mut();
            let tmp = &*tmp_borrow;
            tmp.set_quirks_mode(mode);
        },
        encoding_change: |encname| {
            debug!("encoding change");
            // NOTE: tmp vars are workaround for lifetime issues. Both required.
            let tmp_borrow = doc_cell.borrow_mut();
            let tmp = &*tmp_borrow;
            tmp.set_encoding_name(encname);
        },
        complete_script: |script| {
            unsafe {
                let script = from_hubbub_node::<Node>(script).root();
                let script: Option<JSRef<HTMLScriptElement>> =
                    HTMLScriptElementCast::to_ref(*script);
                let script = match script {
                    Some(script) if script.is_javascript() => script,
                    _ => return,
                };

                let script_element: JSRef<Element> = ElementCast::from_ref(script);
                match script_element.get_attribute(ns!(""), "src").root() {
                    Some(src) => {
                        debug!("found script: {:s}", src.Value());
                        let mut url_parser = UrlParser::new();
                        match base_url {
                            None => (),
                            Some(ref base_url) => {
                                url_parser.base_url(base_url);
                            }
                        };
                        match url_parser.parse(src.value().as_slice()) {
                            Ok(new_url) => js_chan2.send(JSTaskNewFile(new_url)),
                            Err(e) => debug!("Parsing url {:s} failed: {:?}", src.Value(), e)
                        };
                    }
                    None => {
                        let mut data = String::new();
                        let scriptnode: JSRef<Node> = NodeCast::from_ref(script);
                        debug!("iterating over children {:?}", scriptnode.first_child());
                        for child in scriptnode.children() {
                            debug!("child = {:?}", child);
                            let text: JSRef<Text> = TextCast::to_ref(child).unwrap();
                            data.push_str(text.characterdata.data.borrow().as_slice());
                        }

                        debug!("script data = {:?}", data);
                        js_chan2.send(JSTaskNewInlineScript(data, base_url.clone()));
                    }
                }
            }
            debug!("complete script");
        },
        complete_style: |_| {
            // style parsing is handled in element::notify_child_list_changed.
        },
    };
    parser.set_tree_handler(&mut tree_handler);
    debug!("set tree handler");
    debug!("loaded page");
    match input {
        InputString(s) => {
            parser.parse_chunk(s.into_bytes().as_slice());
        },
        InputUrl(url) => {
            let load_response = load_response.unwrap();
            match load_response.metadata.content_type {
                Some((ref t, _)) if t.as_slice().eq_ignore_ascii_case("image") => {
                    let page = format!("<html><body><img src='{:s}' /></body></html>", base_url.as_ref().unwrap().serialize());
                    parser.parse_chunk(page.into_bytes().as_slice());
                },
                _ => loop {
                    match load_response.progress_port.recv() {
                        Payload(data) => {
                            debug!("received data");
                            parser.parse_chunk(data.as_slice());
                        }
                        Done(Err(err)) => {
                            fail!("Failed to load page URL {:s}, error: {:s}", url.serialize(), err);
                        }
                        Done(..) => {
                            break;
                        }
                    }
                }
            }
        },
    }

    debug!("finished parsing");
    js_chan.send(JSTaskExit);

    HtmlParserResult {
        discovery_port: discovery_port,
    }
}

fn build_parser<'a>(node: hubbub::NodeDataPtr) -> hubbub::Parser<'a> {
    let mut parser = hubbub::Parser::new("UTF-8", false);
    parser.set_document_node(node);
    parser.enable_scripting(true);
    parser.enable_styling(true);
    parser
}