Correct erroneous $^H bitsetting cargocult which originated in autobox
[catagits/Web-Simple.git] / lib / CSS / Declare.pm
CommitLineData
4a610f74 1package CSS::Declare;
2
3use strict;
4use warnings;
5
b134433d 6use Syntax::Keyword::Gather;
4a610f74 7
8my $IN_SCOPE = 0;
9
10sub import {
11 die "Can't import CSS::Declare into a scope when already compiling one that uses it"
12 if $IN_SCOPE;
13 my ($class, @args) = @_;
14 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
15 my $target = $class->_find_target(0, $opts);
16 my $unex = $class->_export_tags_into($target);
17 $class->_install_unexporter($unex);
18 $IN_SCOPE = 1;
19}
20
21sub _find_target {
22 my ($class, $extra_levels, $opts) = @_;
23 return $opts->{into} if defined($opts->{into});
24 my $level = ($opts->{into_level} || 1) + $extra_levels;
25 return (caller($level))[0];
26}
27
28my @properties = qw{
5592a37f 29accelerator
30azimuth
4a610f74 31background
5592a37f 32background_attachment
4a610f74 33background_color
5592a37f 34background_image
35background_position
36background_position_x
37background_position_y
38background_repeat
39behavior
4a610f74 40border
5592a37f 41border_bottom
42border_bottom_color
43border_bottom_style
44border_bottom_width
4a610f74 45border_collapse
5592a37f 46border_color
47border_left
48border_left_color
49border_left_style
50border_left_width
51border_right
52border_right_color
53border_right_style
54border_right_width
55border_spacing
56border_style
4a610f74 57border_top
5592a37f 58border_top_color
59border_top_style
60border_top_width
61border_width
62bottom
63caption_side
64clear
65clip
4a610f74 66color
5592a37f 67content
68counter_increment
69counter_reset
70cue
71cue_after
72cue_before
73cursor
74direction
75display
76elevation
77empty_cells
78filter
4a610f74 79float
5592a37f 80font
4a610f74 81font_family
82font_size
5592a37f 83font_size_adjust
84font_stretch
85font_style
86font_variant
87font_weight
88height
89ime_mode
90include_source
91layer_background_color
92layer_background_image
93layout_flow
94layout_grid
95layout_grid_char
96layout_grid_char_spacing
97layout_grid_line
98layout_grid_mode
99layout_grid_type
100left
101letter_spacing
102line_break
103line_height
104list_style
105list_style_image
106list_style_position
4a610f74 107list_style_type
108margin
5592a37f 109margin_bottom
110margin_left
111margin_right
112margin_top
113marker_offset
114marks
115max_height
116max_width
117min_height
118min_width
119orphans
120outline
121outline_color
122outline_style
123outline_width
124overflow
125overflow_X
126overflow_Y
4a610f74 127padding
5592a37f 128padding_bottom
129padding_left
130padding_right
131padding_top
132page
133page_break_after
134page_break_before
135page_break_inside
136pause
137pause_after
138pause_before
139pitch
140pitch_range
141play_during
142position
143quotes
144_replace
145richness
146right
147ruby_align
148ruby_overhang
149ruby_position
150size
151speak
152speak_header
153speak_numeral
154speak_punctuation
155speech_rate
156stress
157scrollbar_arrow_color
158scrollbar_base_color
159scrollbar_dark_shadow_color
160scrollbar_face_color
161scrollbar_highlight_color
162scrollbar_shadow_color
163scrollbar_3d_light_color
164scrollbar_track_color
165table_layout
166text_align
167text_align_last
168text_decoration
169text_indent
170text_justify
171text_overflow
172text_shadow
173text_transform
174text_autospace
175text_kashida_space
176text_underline_position
177top
178unicode_bidi
179vertical_align
180visibility
181voice_family
182volume
183white_space
184widows
185width
186word_break
187word_spacing
188word_wrap
189writing_mode
190z_index
191zoom
4a610f74 192};
193
194sub _export_tags_into {
195 my ($class, $into) = @_;
196 for my $property (@properties) {
197 my $property_name = $property;
198 $property_name =~ tr/_/-/;
199 no strict 'refs';
200 *{"$into\::$property"} = sub ($) { return ($property_name => $_[0]) };
201 }
202 return sub {
203 foreach my $property (@properties) {
204 no strict 'refs';
205 delete ${"${into}::"}{$property}
206 }
207 $IN_SCOPE = 0;
208 };
209}
210
211sub _install_unexporter {
212 my ($class, $unex) = @_;
6c97ab61 213 $^H |= 0x20000; # localize %^H
4a610f74 214 $^H{'CSS::Declare::Unex'} = bless($unex, 'CSS::Declare::Unex');
215}
216
217sub to_css_string {
218 my @css = @_;
219 return join q{ }, gather {
220 while (my ($selector, $declarations) = splice(@css, 0, 2)) {
221 take "$selector "._generate_declarations($declarations)
222 }
223 };
224}
225
226sub _generate_declarations {
227 my $declarations = shift;
228
229 return '{'.join(q{;}, gather {
230 while (my ($property, $value) = splice(@{$declarations}, 0, 2)) {
231 take "$property:$value"
232 }
233 }).'}';
234}
235
236package CSS::Declare::Unex;
237
238sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
239
2401;