Skip to content

Commit 10f58f7

Browse files
committed
servo: Merge #4815 - Lowercase DOM getters at compile time, fixes #4728 (from Swatinem:lowercasegetters); r=Manishearth
The implementation was copied directly from rust-lang/rust#16636 and updated for rust changes, so the credit goes to Manishearth Source-Repo: https://github.com/servo/servo Source-Revision: 902c16497c40684930819693a7e90f0862eb7f56 UltraBlame original commit: 1b9c7a69c746676dcf68b519877913b5b2186a56
1 parent 212fbc2 commit 10f58f7

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

servo/components/plugins/casing.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
3+
4+
5+
use syntax::ext::base::ExtCtxt;
6+
use syntax::ext::build::AstBuilder;
7+
use syntax::codemap::Span;
8+
use syntax::ast;
9+
use syntax::ext::base;
10+
use syntax::parse::token;
11+
12+
pub fn expand_lower<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
13+
-> Box<base::MacResult + 'cx> {
14+
expand_cased(cx, sp, tts, |c| { c.to_lowercase() })
15+
}
16+
17+
pub fn expand_upper<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
18+
-> Box<base::MacResult + 'cx> {
19+
expand_cased(cx, sp, tts, |c| { c.to_uppercase() })
20+
}
21+
22+
fn expand_cased<'cx, T>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree], transform: T)
23+
-> Box<base::MacResult + 'cx>
24+
where T: Fn(char) -> char
25+
{
26+
let es = match base::get_exprs_from_tts(cx, sp, tts) {
27+
Some(e) => e,
28+
None => return base::DummyResult::expr(sp)
29+
};
30+
31+
let mut it = es.iter();
32+
let res = if let Some(expr) = it.next() {
33+
if let ast::ExprLit(ref lit) = expr.node {
34+
if let ast::LitStr(ref s, _) = lit.node {
35+
Some((s, lit.span))
36+
} else {
37+
cx.span_err(expr.span, "expected a string literal");
38+
None
39+
}
40+
} else {
41+
cx.span_err(expr.span, "expected a string literal");
42+
None
43+
}
44+
} else {
45+
cx.span_err(sp, "expected 1 argument, found 0");
46+
None
47+
};
48+
match (res, it.count()) {
49+
(Some((s, span)), 0) => {
50+
let new_s = s.get().chars().map(transform).collect::<String>();
51+
base::MacExpr::new(cx.expr_str(span, token::intern_and_get_ident(new_s.as_slice())))
52+
}
53+
(_, rest) => {
54+
if rest > 0 {
55+
cx.span_err(sp, format!("expected 1 argument, found {}", rest+1).as_slice());
56+
}
57+
base::DummyResult::expr(sp)
58+
}
59+
}
60+
}

servo/components/plugins/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ pub mod reflector;
4040
pub mod lints;
4141

4242
pub mod utils;
43+
pub mod casing;
4344

4445
#[plugin_registrar]
4546
pub fn plugin_registrar(reg: &mut Registry) {
4647
reg.register_syntax_extension(intern("dom_struct"), Modifier(box jstraceable::expand_dom_struct));
4748
reg.register_syntax_extension(intern("jstraceable"), Decorator(box jstraceable::expand_jstraceable));
4849
reg.register_syntax_extension(intern("_generate_reflector"), Decorator(box reflector::expand_reflector));
50+
reg.register_macro("to_lower", casing::expand_lower);
51+
reg.register_macro("to_upper", casing::expand_upper);
4952
reg.register_lint_pass(box lints::transmute_type::TransmutePass as LintPassObject);
5053
reg.register_lint_pass(box lints::unrooted_must_root::UnrootedPass as LintPassObject);
5154
reg.register_lint_pass(box lints::privatize::PrivatizePass as LintPassObject);

servo/components/script/dom/macros.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ macro_rules! make_getter(
1111
#[allow(unused_imports)]
1212
use std::ascii::AsciiExt;
1313
let element: JSRef<Element> = ElementCast::from_ref(self);
14-
element.get_string_attribute(&Atom::from_slice($htmlname.to_ascii_lowercase().as_slice()))
14+
element.get_string_attribute(&Atom::from_slice($htmlname))
1515
}
1616
);
1717
($attr:ident) => {
18-
make_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice());
18+
make_getter!($attr, to_lower!(stringify!($attr)));
1919
}
2020
);
2121

@@ -33,7 +33,7 @@ macro_rules! make_bool_getter(
3333
}
3434
);
3535
($attr:ident) => {
36-
make_bool_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice());
36+
make_bool_getter!($attr, to_lower!(stringify!($attr)));
3737
}
3838
);
3939

@@ -51,7 +51,7 @@ macro_rules! make_uint_getter(
5151
}
5252
);
5353
($attr:ident) => {
54-
make_uint_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice());
54+
make_uint_getter!($attr, to_lower!(stringify!($attr)));
5555
}
5656
);
5757

@@ -70,7 +70,7 @@ macro_rules! make_url_getter(
7070
);
7171
($attr:ident) => {
7272
// FIXME(pcwalton): Do this at compile time, not runtime.
73-
make_url_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice());
73+
make_url_getter!($attr, to_lower!(stringify!($attr)));
7474
}
7575
);
7676

@@ -94,7 +94,7 @@ macro_rules! make_url_or_base_getter(
9494
}
9595
);
9696
($attr:ident) => {
97-
make_url_or_base_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice());
97+
make_url_or_base_getter!($attr, to_lower!(stringify!($attr)));
9898
}
9999
);
100100

@@ -118,7 +118,7 @@ macro_rules! make_enumerated_getter(
118118
}
119119
);
120120
($attr:ident, $default:expr, $(($choices: pat))|+) => {
121-
make_enumerated_getter!($attr, stringify!($attr).to_ascii_lowercase().as_slice(), $default, $(($choices))|+);
121+
make_enumerated_getter!($attr, to_lower!(stringify!($attr)).as_slice(), $default, $(($choices))|+);
122122
}
123123
);
124124

0 commit comments

Comments
 (0)