aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/html/cssparse.rs
blob: 0a04daf15aea249cc41efe9fa4b8ae08bf9bf233 (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
/* 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/. */

/// Some little helpers for hooking up the HTML parser with the CSS parser.

use std::cell::Cell;
use std::comm;
use std::comm::Port;
use std::task;
use newcss::stylesheet::Stylesheet;
use newcss::util::DataStream;
use servo_net::resource_task::{ResourceTask, ProgressMsg, Load, Payload, Done};
use extra::net::url::Url;

/// Where a style sheet comes from.
pub enum StylesheetProvenance {
    UrlProvenance(Url),
    InlineProvenance(Url, ~str),
}

pub fn spawn_css_parser(provenance: StylesheetProvenance,
                        resource_task: ResourceTask)
                     -> Port<Stylesheet> {
    let (result_port, result_chan) = comm::stream();

    let provenance_cell = Cell::new(provenance);
    do task::spawn {
        let url = do provenance_cell.with_ref |p| {
            match *p {
                UrlProvenance(ref the_url) => (*the_url).clone(),
                InlineProvenance(ref the_url, _) => (*the_url).clone()
            }
        };

        let sheet = Stylesheet::new(url, data_stream(provenance_cell.take(),
                                                     resource_task.clone()));
        result_chan.send(sheet);
    }

    return result_port;
}

fn data_stream(provenance: StylesheetProvenance, resource_task: ResourceTask) -> DataStream {
    match provenance {
        UrlProvenance(url) => {
            debug!("cssparse: loading style sheet at %s", url.to_str());
            let (input_port, input_chan) = comm::stream();
            resource_task.send(Load(url, input_chan));
            resource_port_to_data_stream(input_port)
        }
        InlineProvenance(_, data) => {
            data_to_data_stream(data)
        }
    }
}

fn resource_port_to_data_stream(input_port: Port<ProgressMsg>) -> DataStream {
    return || {
        match input_port.recv() {
            Payload(data) => Some(data),
            Done(*) => None
        }
    }
}

fn data_to_data_stream(data: ~str) -> DataStream {
    let data_cell = Cell::new(data);
    return || {
        if data_cell.is_empty() {
            None
        } else {
            // FIXME: Blech, a copy.
            let data = data_cell.take();
            Some(data.as_bytes().to_owned())
        }
    }
}