aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/html/cssparse.rs
blob: e6383599d395ce3c1721b9c93830aa6fabb25cba (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
/* 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::{Load, LoadResponse, Payload, Done, ResourceTask, ProgressMsg};
use extra::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) -> @mut 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<LoadResponse>) -> @mut DataStream {
    let progress_port = input_port.recv().progress_port;
    struct ResourcePortDataStream {
        progress_port: Port<ProgressMsg>,
    };
    impl DataStream for ResourcePortDataStream {
        fn read(&mut self) -> Option<~[u8]> {
            match self.progress_port.recv() {
                Payload(data) => Some(data),
                Done(*) => None
            }
        }
    }
    let stream = @mut ResourcePortDataStream {
        progress_port: progress_port,
    };
    stream as @mut DataStream
}

fn data_to_data_stream(data: ~str) -> @mut DataStream {
    let data_cell = Cell::new(data);
    struct DataDataStream {
        data_cell: Cell<~str>,
    };
    impl DataStream for DataDataStream {
        fn read(&mut self) -> Option<~[u8]> {
            if self.data_cell.is_empty() {
                None
            } else {
                // FIXME: Blech, a copy.
                let data = self.data_cell.take();
                Some(data.as_bytes().to_owned())
            }
        }
    }
    let stream = @mut DataDataStream {
        data_cell: data_cell,
    };
    stream as @mut DataStream
}