forked from servo/rust-css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.rs
More file actions
155 lines (131 loc) · 4.8 KB
/
select.rs
File metadata and controls
155 lines (131 loc) · 4.8 KB
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
/* 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/. */
/*!
Selector matching
Select matching is performed on generic node types. Client-specific details
about the DOM are encapsulated in the `SelectHandler` type which the `SelectCtx`
uses to query various DOM and UA properties.
*/
use stylesheet::Stylesheet;
use computed::ComputedStyle;
use util::VoidPtrLike;
use wapcaplet::LwcString;
use lwcstr_from_rust_str = wapcaplet::from_rust_string;
use n::u::{rust_str_to_net_qname, net_qname_to_rust_str};
use types::StylesheetOrigin;
use n;
pub struct SelectCtx {
inner: n::s::CssSelectCtx
}
/**
The SelectCtx, used for performing selector matching.
The `SelectCtx` takes ownership of any number of `Stylesheet` objects,
encapsulates the cascade. Individual node styles can be requested with
the `select_style` method.
*/
pub impl SelectCtx {
fn new() -> SelectCtx {
SelectCtx {
inner: n::s::css_select_ctx_create()
}
}
/**
Add `Stylesheet`s to the selection context, where they will participate in the cascade
during future selector matching
*/
fn append_sheet(&mut self, sheet: Stylesheet, origin: StylesheetOrigin) {
let sheet = match sheet {
Stylesheet { inner: inner } => inner
};
self.inner.append_sheet(sheet, origin.to_net(), n::ll::t::CSS_MEDIA_SCREEN)
}
/**
Select the style for a single node. `handler` is used to query the client for
a wide range of client-specific details like node relationships, names, and UA
defaults.
*/
fn select_style<N: VoidPtrLike, H: SelectHandler<N>>(&self, node: &N, handler: &H) -> SelectResults {
let inner_handler = SelectHandlerWrapper {
inner: ptr::to_unsafe_ptr(handler)
};
SelectResults {
inner: self.inner.select_style::<N, SelectHandlerWrapper<N, H>>(node, n::ll::t::CSS_MEDIA_SCREEN, None, &inner_handler)
}
}
}
/**
Represents the 'style' of a single node, including it's pseudo-elements.
*/
pub struct SelectResults {
inner: n::s::CssSelectResults
}
pub impl<'self> SelectResults {
/** Retrieve the computed style of a single pseudo-element */
fn computed_style(&'self self) -> ComputedStyle<'self> {
ComputedStyle {
inner: self.inner.computed_style(n::s::CssPseudoElementNone)
}
}
}
/**
Callbacks used to query the implementation-specific DOM
*/
pub trait SelectHandler<N> {
fn with_node_name<R>(&self, node: &N, f: &fn(&str) -> R) -> R;
fn with_node_id<R>(&self, node: &N, f: &fn(Option<&str>) -> R) -> R;
fn named_parent_node(&self, node: &N, name: &str) -> Option<N>;
fn parent_node(&self, node: &N) -> Option<N>;
fn node_has_id(&self, node: &N, &str) -> bool;
fn named_ancestor_node(&self, node: &N, name: &str) -> Option<N>;
fn node_is_root(&self, node: &N) -> bool;
}
/** Used to convert the netsurfcss CssSelectHandler callbacks to out SelectHandler callbacks */
struct SelectHandlerWrapper<N, H> {
// FIXME: Can't encode region variables
inner: *H
}
priv impl<'self, N, H: SelectHandler<N>> SelectHandlerWrapper<N, H> {
priv fn inner_ref(&self) -> &'self H {
unsafe { &*self.inner }
}
}
impl<N, H: SelectHandler<N>> n::s::CssSelectHandler<N> for SelectHandlerWrapper<N, H> {
fn node_name(&self, node: &N) -> n::t::CssQName {
do self.inner_ref().with_node_name(node) |name| {
rust_str_to_net_qname(name)
}
}
fn node_id(&self, node: &N) -> Option<LwcString> {
do self.inner_ref().with_node_id(node) |node_id_opt| {
node_id_opt.map(|s| lwcstr_from_rust_str(*s))
}
}
fn named_parent_node(&self, node: &N, qname: &n::t::CssQName) -> Option<N> {
self.inner_ref().named_parent_node(node, net_qname_to_rust_str(qname))
}
fn parent_node(&self, node: &N) -> Option<N> {
self.inner_ref().parent_node(node)
}
fn node_has_id(&self, node: &N, name: LwcString) -> bool {
self.inner_ref().node_has_id(node, name.to_str_slice())
}
fn named_ancestor_node(&self, node: &N, qname: &n::t::CssQName) -> Option<N> {
self.inner_ref().named_ancestor_node(node, net_qname_to_rust_str(qname))
}
fn node_is_root(&self, node: &N) -> bool {
self.inner_ref().node_is_root(node)
}
fn node_is_link(&self, _node: &N) -> bool {
// FIXME
warn_unimpl("node_is_link");
false
}
fn ua_default_for_property(&self, property: n::p::CssProperty) -> n::h::CssHint {
warn!("not specifiying ua default for property %?", property);
n::h::CssHintDefault
}
}
fn warn_unimpl(what: &str) {
warn!("unimplemented select handler: %?", what);
}