Yamaantaka.pm

filter: Statementstmt 94.3% 286 / 303 Branchbran 76.8% 83 / 108 Conditioncond 53.8% 14 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 36 / 36 Podpod 21.7% 5 / 23 totaltotal 83.4% 427 / 512 CC 65 SCAR 16.3
CC
65
Cov
88%
new
16.7
transform
13.6
reorderBorderRadiusPart
10.0
CRAP 5.1
↑ Summary Janus.pm »
1 #-*- perl -*-
2 #-*- coding: us-ascii -*-
3  
4 =head1 NAME
5  
6 CSS::Yamaantaka - Converts direction of Cascading Style Sheet (CSS)
7  
8 =head1 SYNOPSIS
9  
10 use CSS::Yamaantaka;
11
12 $ya = CSS::Yamaantaka->new('lr_tb' => 'tb_rl');
13 $css_source_vertical_rl = $ya->transform($css_source);
14  
15 =head1 DESCRIPTION
16  
17 As YamE<257>ntaka has many legs, texts can run in various directions:
18 left to right and right to left horizontally; vertically with lines
19 extending right to left and left to right.
20  
21 CSS::Yamaantaka replaces things directed to "left" or "horizontal-tb" in a
22 Cascading Style Sheet (CSS) file such as float, padding, margin with
23 values directed to "right" or "vertical-rl", and so on.
24  
25 =cut
26  
27 65 ▶use 5.005; # qr{} and $10 are required.
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 package CSS::Yamaantaka;
30  
31 65 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 #use warnings;
33 65 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 65 ▶use CSS::Yamaantaka::Consts;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # To be compatible with Perl 5.5.
37 65 ▶use vars qw($VERSION $BASE_REVISION);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 34 $VERSION = '0.04_02';
39 34 $BASE_REVISION = 'http://cssjanus.googlecode.com/svn/trunk@31';
40  
41 =head2 Constructor
42  
43 =over 4
44  
45 =item new ( SRC =E<gt> DEST, [ options... ] )
46  
47 =item new ( C<'adaptor'> =E<gt> ADAPTOR, [ options... ] )
48  
49 Creates new CSS::Yamaantaka object.
50  
51 In first form, SRC and DEST are the original and resulting directions.
52 Available directions are
53 C<'lr_tb'>, C<'rl_tb'>, C<'tb_lr'> and C<'tb_rl'>.
54 Their synonyms are C<'ltr'>, C<'rtl'>, C<'vertical-lr'> and C<'vertical-rl'>,
55 respectively.
56  
57 Following options are available.
58  
59 =over 4
60  
61 =item flip_bg =E<gt> 0|1
62  
63 Fixes background positions properties.
64 Default is C<1>, will fix.
65  
66 =item flip_cursor =E<gt> 0|1
67  
68 Fixes positions "n"/"e"/"s"/"w" and so on within cursor properties.
69 Default is C<1>, will fix.
70  
71 =item flip_url =E<gt> 0|1
72  
73 Fixes "top"/"right"/"bottom"/"left" string within URLs.
74 Default is C<0>, won't fix.
75  
76 =item ignore_bad_bgp =E<gt> 0|1
77  
78 Ignores unmirrorable background-position values.
79 Default is C<1>, WILL ignore and won't croak it.
80  
81 =item swap_ltr_rtl_in_url =E<gt> 0|1
82  
83 Fixes "ltr"/"rtl" string within URLs, if needed.
84 Default is C<0>, won't fix.
85  
86 =back
87  
88 In second form, ADAPTOR is a name of package or an object.
89 package will be automatically loaded.
90 See L</Adaptors> about standard adaptors.
91  
92 =back
93  
94 =cut
95  
96 34 my %defaults = (
97 'flip_bg' => 1,
98 'flip_cursor' => 1,
99 'flip_url' => 0,
100 'ignore_bad_bgp' => 1,
101 'swap_ltr_rtl_in_url' => 0,
102 );
103  
104 34 my %dir_synonym = (
105 'ltr' => 'lr_tb',
106 'rtl' => 'rl_tb',
107 'vertical-lr' => 'tb_lr',
108 'vertical-rl' => 'tb_rl',
109 );
110  
111 34 my %adaptor = (
112 "lr_tb$;rl_tb" => 'CSS::Yamaantaka::MirrorH',
113 "rl_tb$;lr_tb" => 'CSS::Yamaantaka::MirrorH',
114 "lr_tb$;tb_lr" => 'CSS::Yamaantaka::MirrorTL_BR',
115 "tb_lr$;lr_tb" => 'CSS::Yamaantaka::MirrorTL_BR',
116 "lr_tb$;tb_rl" => 'CSS::Yamaantaka::RotateR',
117 "tb_rl$;lr_tb" => 'CSS::Yamaantaka::RotateL',
118 "rl_tb$;tb_lr" => 'CSS::Yamaantaka::RotateL',
119 "tb_lr$;rl_tb" => 'CSS::Yamaantaka::RotateR',
120 "rl_tb$;tb_rl" => 'CSS::Yamaantaka::MirrorTR_BL',
121 "tb_rl$;rl_tb" => 'CSS::Yamaantaka::MirrorTR_BL',
122 "tb_lr$;tb_rl" => 'CSS::Yamaantaka::MirrorV',
123 "tb_rl$;tb_lr" => 'CSS::Yamaantaka::MirrorV',
124 );
125  
126 34 my %body_direction = (
127 'lr_tb' => 'ltr',
128 'rl_tb' => 'rtl',
129 'tb_lr' => 'ltr',
130 'tb_rl' => 'rtl',
131 );
132  
133 34 my %writing_mode = (
134 'lr_tb' => 'horizontal-tb',
135 'rl_tb' => 'horizontal-tb',
136 'tb_lr' => 'vertical-lr',
137 'tb_rl' => 'vertical-rl',
138 );
139  
140 34 my %text_orientation = (
141 "rl_tb$;tb_lr" => 'sideways-left',
142 "rl_tb$;tb_rl" => 'sideways-left',
143 );
144  
145 602 ▶sub new {
Subroutine new: called CC=14 SCAR=28.2
Pod documented
146 603 my $pkg = shift;
147 602 my $self = {@_};
148  
149 602 my ($src) = grep {/^((lr|rl)_tb|tb_(lr|rl)|ltr|rtl|vertical-(lr|rl))$/}
150 keys %$self;
151 602 my $dest;
152 602 ▶ if ($src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src) 472 105 577
153 497 $dest = $self->{$src};
154 497 ▶ if ($dest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest) 472 0 472
155 497 ▶ $src = $dir_synonym{$src} || $src;
Condition33%1/3
$dir_synonym{$src} || $src
l!l&&r!l&&!r
04720
MC/DC0%0/2
$dir_synonym{$src} || $src
$dir_synonym{$src} $src
Truth table33%1/3
A || B
A= $dir_synonym{$src}B= $src
ABresult
1X1
011
000
156 497 ▶ $dest = $dir_synonym{$dest} || $dest;
Condition33%1/3
$dir_synonym{$dest} || $dest
l!l&&r!l&&!r
04720
MC/DC0%0/2
$dir_synonym{$dest} || $dest
$dir_synonym{$dest} $dest
Truth table33%1/3
A || B
A= $dir_synonym{$dest}B= $dest
ABresult
1X1
011
000
157 497 $self->{'body_direction'} = $body_direction{$dest};
158 497 $self->{'writing_mode'} = $writing_mode{$dest};
159 497 $self->{'text_orientation'} = $text_orientation{$src, $dest};
160 497 $self->{'adaptor'} = $adaptor{$src, $dest};
161 }
162 }
163 602 ▶ unless ($src and $dest and $src eq $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($src and $dest and $src eq $dest) 577 0 577
Condition66%4/6
$src and $dest and $src eq $dest
!ll&&!rl&&r
1054720
$src and $dest
!ll&&!rl&&r
1050472
MC/DC0%0/3
$src and $dest and $src eq $dest
$src $dest $src eq $dest
Truth table50%2/4
A and B and C
A= $srcB= $destC= $src eq $dest
ABCresult
0XX0
10X0
1100
1111
164 croak 'available adaptor not found'
165 602 ▶ unless $self->{'adaptor'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"adaptor"} 0 577 577
166 }
167 590 ▶ if ($self->{'adaptor'} and !ref $self->{'adaptor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"adaptor"} and not ref $self->{"adaptor"}) 577 0 577
Condition33%1/3
$self->{"adaptor"} and not ref $self->{"adaptor"}
!ll&&!rl&&r
00577
MC/DC0%0/2
$self->{"adaptor"} and not ref $self->{"adaptor"}
$self->{"adaptor"} not ref $self->{"adaptor"}
Truth table33%1/3
A and not(B)
A= $self->{"adaptor"}B= not ref $self->{"adaptor"}
ABresult
0X0
100
111
168 65 ▶ eval "use $self->{'adaptor'}";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
169 590 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 577 577
170 }
171  
172 # apply default
173 590 foreach my $o (keys %defaults) {
174 2898 ▶ $self->{$o} = $defaults{$o} unless defined $self->{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$o} 2692 193 2885
175 }
176  
177 590 bless $self => $pkg;
178 }
179  
180 # Substituttion of CSS gradients which cannot be performed only by regexp
181 # because they can contain nested parentheses.
182  
183 34 my $GRADIENT_RE = qr<$IDENT[\.-]gradient\s*\(>i;
184  
185 643 ▶sub substituteGradient {
Subroutine substituteGradient: called CC=6 SCAR=17.9
Pod undocumented
186 631 my $self = shift;
187 631 my $match_function = shift;
188 631 my $input_string = shift;
189  
190 631 pos($input_string) = 0;
191 631 my $output = '';
192 631 my ($other, $match, $paren_count);
193  
194 631 ▶ while ($input_string =~ m{\G(.*?)($GRADIENT_RE)}cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_string =~ /\G(.*?)($GRADIENT_RE)/cg) 2 618 620
195 15 ($other, $match) = ($1, $2);
196  
197 15 $paren_count = 1;
198 15 ▶ while ($paren_count and $input_string =~ m{\G(\(|\)|[^()]+)}cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paren_count and $input_string =~ /\G(\(|\)|[^()]+)/cg) 11 2 13
Condition66%2/3
$paren_count and $input_string =~ /\G(\(|\)|[^()]+)/cg
!ll&&!rl&&r
2011
MC/DC50%1/2
$paren_count and $input_string =~ /\G(\(|\)|[^()]+)/cg
$paren_count $input_string =~ /\G(\(|\)|[^()]+)/cg
Truth table66%2/3
A and B
A= $paren_countB= $input_string =~ /\G(\(|\)|[^()]+)/cg
ABresult
0X0
100
111
199 24 ▶ if ($1 eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq '(') { } 2 9 11
elsif ($1 eq ')') { } 4 5 9
200 15 $paren_count++;
201 } elsif ($1 eq ')') {
202 17 $paren_count--;
203 }
204 24 $match .= $1;
205 }
206  
207 # pos() is at last closing parenthesis (or end of text).
208 15 $output .= $other . &$match_function($match);
209 }
210 629 return $output . substr($input_string, pos($input_string));
211 }
212  
213 # fixBodyDirectionLtrAndRtl ($line)
214 #
215 # Replaces ltr with rtl and vice versa ONLY in the body direction:
216 # 'body { direction:ltr }' => 'body { direction:rtl }'
217  
218 643 ▶sub fixBodyDirectionLtrAndRtl {
Subroutine fixBodyDirectionLtrAndRtl: called CC=2 SCAR=6.9
Pod undocumented
219 629 my $self = shift;
220 629 my $line = shift;
221 629 my $adaptor = $self->{'adaptor'};
222  
223 629 ▶ return $line
Branch: 1 branch
BranchTrueFalseTotal
unless $adaptor->willReverseGlobalDirection 184 434 618
224 unless $adaptor->willReverseGlobalDirection;
225  
226 445 $line =~ s{$BODY_DIRECTION_LTR_RE}{$1$2$3~TMP~}g;
227 445 $line =~ s{$BODY_DIRECTION_RTL_RE}{$1$2$3ltr}g;
228 445 $line =~ s{~TMP~}{rtl}g;
229  
230 445 return $line;
231 }
232  
233 # fixSingleBorderRadius ($line)
234  
235 49 ▶sub fixSingleBorderRadiusName {
Subroutine fixSingleBorderRadiusName: called CC=4 SCAR=13.9
Pod undocumented
236 35 my $adaptor = shift;
237 35 my @m = @_;
238  
239 35 ▶ if (defined $m[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m[0]) { } 12 12 24
240 23 ▶ unless ($adaptor->willSwapHorizontalVertical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptor->willSwapHorizontalVertical) { } 8 4 12
241 15 return 'border-' . $adaptor->fixBoxDirectionPart($m[0]) . '-' .
242 $adaptor->fixBoxDirectionPart($m[1]) . '-radius';
243 } else {
244 19 return 'border-' . $adaptor->fixBoxDirectionPart($m[1]) . '-' .
245 $adaptor->fixBoxDirectionPart($m[0]) . '-radius';
246 }
247 } else {
248 20 ▶ unless ($adaptor->willSwapHorizontalVertical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptor->willSwapHorizontalVertical) { } 8 4 12
249 12 return 'border-radius-' . $adaptor->fixBoxDirectionPart($m[2]) .
250 $adaptor->fixBoxDirectionPart($m[3]);
251 } else {
252 16 return 'border-radius-' . $adaptor->fixBoxDirectionPart($m[3]) .
253 $adaptor->fixBoxDirectionPart($m[2]);
254 }
255 }
256 }
257  
258 643 ▶sub fixSingleBorderRadius {
Subroutine fixSingleBorderRadius: called CC=3 SCAR=11.0
Pod undocumented
259 625 my $self = shift;
260 625 my $line = shift;
261 625 my $adaptor = $self->{'adaptor'};
262  
263 625 $line =~ s{$SINGLE_BORDER_RADIUS_RE}{
264 31 ▶ if (defined $7) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $7) { } 12 12 24
265 19 ▶ unless ($adaptor->willSwapHorizontalVertical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptor->willSwapHorizontalVertical) { } 8 4 12
266 11 $1 . fixSingleBorderRadiusName($adaptor, $2, $3, $4, $5) .
267 "$6$7 $8";
268 } else {
269 15 $1 . fixSingleBorderRadiusName($adaptor, $2, $3, $4, $5) .
270 "$6$8 $7";
271 }
272 } else {
273 19 $1 . fixSingleBorderRadiusName($adaptor, $2, $3, $4, $5) .
274 "$6$8";
275 }
276 }eg;
277  
278 625 return $line;
279 }
280  
281 # fixBoxDirection ($line)
282 #
283 # Replaces left with right and vice versa in line, e,g,:
284 # 'padding-left: 2px; margin-right: 1px;' =>
285 # 'padding-right: 2px; margin-left: 1px;'
286 #
287 # Note: Old name is fixLeftAndRight().
288  
289 631 ▶sub fixBoxDirection {
Subroutine fixBoxDirection: called CC=3 SCAR=11.0
Pod undocumented
290 625 my $self = shift;
291 625 my $line = shift;
292 625 my $adaptor = $self->{'adaptor'};
293  
294 625 $line =~ s{$BOX_DIRECTIONS_RE}{
295 214 ▶ if (defined $4) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $4) { } 159 48 207
elsif ($adaptor->willSwapHorizontalVertical) { } 32 16 48
296 166 $adaptor->fixBoxDirectionPart($4);
297 } elsif ($adaptor->willSwapHorizontalVertical) {
298 39 $adaptor->fixBoxDirectionPart($3) . $2 .
299 $adaptor->fixBoxDirectionPart($1);
300 } else {
301 23 $adaptor->fixBoxDirectionPart($1) . $2 .
302 $adaptor->fixBoxDirectionPart($3);
303 }
304 }eg;
305  
306 625 return $line;
307 }
308  
309 # fixBoxDirectionInUrl ($line)
310 #
311 # Replaces left with right and vice versa within background URLs, e.g.:
312 # 'background:url(right.png)' => 'background:url(left.png)'
313 #
314 # Note: Old name is fixLeftAndRightInUrl().
315  
316 79 ▶sub fixBoxDirectionInUrl {
Subroutine fixBoxDirectionInUrl: called CC=1 SCAR=0.0
Pod undocumented
317 73 my $self = shift;
318 73 my $line = shift;
319 73 my $adaptor = $self->{'adaptor'};
320  
321 73 $line =~ s{$BOX_DIRECTION_IN_URL_RE}{
322 37 $adaptor->fixBoxDirectionPart($1);
323 }eg;
324  
325 73 return $line;
326 }
327  
328 # fixLtrAndRtlInUrl ($line)
329 #
330 # Replaces ltr with rtl and vice versa within background URLs, e.g.:
331 # 'background:url(rtl.png)' => 'background:url(ltr.png)'
332  
333 79 ▶sub fixLtrAndRtlInUrl {
Subroutine fixLtrAndRtlInUrl: called CC=2 SCAR=6.9
Pod undocumented
334 73 my $self = shift;
335 73 my $line = shift;
336 73 my $adaptor = $self->{'adaptor'};
337  
338 73 ▶ return $line
Branch: 1 branch
BranchTrueFalseTotal
unless $adaptor->willReverseGlobalDirection 22 44 66
339 unless $adaptor->willReverseGlobalDirection;
340  
341 51 $line =~ s{$LTR_IN_URL_RE}{~TMP~}g;
342 51 $line =~ s{$RTL_IN_URL_RE}{ltr}g;
343 51 $line =~ s{~TMP~}{rtl}g;
344  
345 51 return $line;
346 }
347  
348 61 ▶sub fixCursorDirection {
Subroutine fixCursorDirection: called CC=1 SCAR=0.0
Pod undocumented
349 54 my $adaptor = shift;
350 54 my $direction = shift;
351  
352 54 $direction = $adaptor->fixCursorPositions($direction);
353 48 $direction =~ s/^([ew])([ns])/$2$1/;
354 48 $direction =~ s/([ew])([ns])$/$2$1/;
355 48 $direction =~ s/^(s[ew])(n[ew])$/$2$1/;
356  
357 48 $direction;
358 }
359  
360 # fixCursorProperties ($line)
361 #
362 # Changes directional CSS cursor properties:
363 # 'cursor: ne-resize' => 'cursor: nw-resize'
364  
365 631 ▶sub fixCursorProperties {
Subroutine fixCursorProperties: called CC=1 SCAR=0.0
Pod undocumented
366 618 my $self = shift;
367 618 my $line = shift;
368 618 my $adaptor = $self->{'adaptor'};
369  
370 618 $line =~ s{$CURSOR_DIRECTION_RE}{
371 48 fixCursorDirection($adaptor, $1) . '-resize';
372 }eg;
373  
374 618 return $line;
375 }
376  
377 # fixBorderRadius ($line)
378 #
379 # Changes border-radius and its browser-specific variants, e.g.:
380 # 'border-radius: 1px 2px 3px 4px / 5px 6px 7px' =>
381 # 'border-radius: 2px 1px 4px 3px / 6px 5px 6px 7px'
382  
383 631 ▶sub fixBorderRadius {
Subroutine fixBorderRadius: called CC=1 SCAR=0.0
Pod undocumented
384 618 my $self = shift;
385 618 my $line = shift;
386 618 my $adaptor = $self->{'adaptor'};
387  
388 618 $line =~ s{$BORDER_RADIUS_RE}{
389 28 $self->reorderBorderRadius($&, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
390 }eg;
391  
392 618 return $line;
393 }
394  
395 # fixFourPartNotation ($line)
396 #
397 # Fixes the second and fourth positions in four-part CSS notation, e.g.:
398 # 'padding: 1px 2px 3px 4px' => 'padding: 1px 4px 3px 2px'
399  
400 631 ▶sub fixFourPartNotation {
Subroutine fixFourPartNotation: called CC=1 SCAR=0.0
Pod undocumented
401 618 my $self = shift;
402 618 my $line = shift;
403 618 my $adaptor = $self->{'adaptor'};
404  
405 618 $line =~ s{$FOUR_NOTATION_QUANTITY_RE}{
406 36 join(' ', $adaptor->reorderFourPartNotation($1, $2, $3, $4))
407 }eg;
408 618 $line =~ s{$FOUR_NOTATION_COLOR_RE}{
409 0 $1 . join(' ', $adaptor->reorderFourPartNotation($2, $3, $4, $5))
410 }eg;
411  
412 618 return $line;
413 }
414  
415 # fixBackgroundPosition ($line)
416 #
417 # METHOD. Changes horizontal background values in line.
418 #
419 # If value is not replaceable, croak it (by default) or carp it (if
420 # 'ignore_bad_bgp' option is set).
421  
422 629 ▶sub fixBackgroundPosition {
Subroutine fixBackgroundPosition: called CC=2 SCAR=6.9
Pod undocumented
423 618 my $self = shift;
424 618 my $line = shift;
425  
426 618 ▶ my $adaptor = $self->{'adaptor'} || return $line;
Condition50%1/2
$self->{'adaptor'} || (return $line)
l!l
6180
MC/DC0%0/1
$self->{'adaptor'} || (return $line)
$self->{'adaptor'}
Truth table50%1/2
A
A= $self->{'adaptor'}
Aresult
11
00
427 # return $line
428 # unless $adaptor->willReverseGlobalDirection;
429  
430 618 $line =~ s{$BG_QUANTITY_RE}{
431 21 $self->calculateNewBackgroundQuantityPosition(
432 $&, $1, $2, $3, $4, $5, $6
433 )
434 }eg;
435 617 $line =~ s{$BG_HORIZONTAL_PERCENTAGE_X_RE}{
436 1 $self->calculateNewBackgroundPositionX($&, $1, $2)
437 }eg;
438 # $line =~ s{$BG_HORIZONTAL_LENGTH_RE}{
439 # $self->calculateNewBackgroundLengthPosition($&, $1, $2, $3, $4, $5)
440 # }eg;
441 617 $line =~ s{$BG_HORIZONTAL_LENGTH_X_RE}{
442 2 $self->calculateNewBackgroundLengthPositionX($&, $1, $2)
443 }eg;
444  
445 617 return $line;
446 }
447  
448 # Takes a list of zero to four border radius parts and returns a string of
449 # them reordered for bidi mirroring.
450  
451 73 ▶sub reorderBorderRadiusPart {
Subroutine reorderBorderRadiusPart: called CC=10 SCAR=23.0
Pod undocumented
452 62 my $adaptor = shift;
453 62 my @part = @_;
454  
455 # Remove any piece which may be 'None'
456 62 ▶ @part = grep { defined $_ and length $_ } @part;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
570185
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
457  
458 62 ▶ return join ' ', @part
Branch: 1 branch
BranchTrueFalseTotal
unless $adaptor 0 62 62
459 unless $adaptor;
460  
461 62 ▶ if (scalar @part == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @part == 0) 4 58 62
462 4 return '';
463 }
464 58 ▶ if (scalar @part == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @part == 1) 8 50 58
465 8 $part[1] = $part[0];
466 }
467 58 ▶ if (scalar @part == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @part == 2) 16 42 58
468 16 $part[2] = $part[0];
469 }
470 58 ▶ if (scalar @part == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @part == 3) 23 35 58
471 23 $part[3] = $part[1];
472 }
473  
474 58 @part = $adaptor->reorderBorderRadiusSubparts(@part);
475  
476 58 ▶ if ($part[3] eq $part[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part[3] eq $part[1]) 18 40 58
477 18 pop @part;
478 18 ▶ if ($part[2] eq $part[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part[2] eq $part[0]) 16 2 18
479 16 pop @part;
480 16 ▶ if ($part[1] eq $part[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part[1] eq $part[0]) 8 8 16
481 8 pop @part;
482 }
483 }
484 }
485 58 return join ' ', @part;
486 }
487  
488 # Receives a match object for a border-radius element and reorders it pieces.
489 40 ▶sub reorderBorderRadius {
Subroutine reorderBorderRadius: called CC=3 SCAR=11.0
Pod undocumented
490 29 my $self = shift;
491 29 my @m = @_;
492 29 my $adaptor = $self->{'adaptor'};
493  
494 29 my $first_group = reorderBorderRadiusPart($adaptor, @m[3 .. 6]);
495 29 my $second_group = reorderBorderRadiusPart($adaptor, @m[7 .. $#m]);
496 29 ▶ if ($second_group eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($second_group eq '') { } 4 25 29
elsif ($adaptor->willSwapHorizontalVertical) { } 16 9 25
497 4 return sprintf '%sborder-radius%s%s', $_[1], $_[2], $first_group;
498 } elsif ($adaptor->willSwapHorizontalVertical) {
499 16 return sprintf '%sborder-radius%s%s / %s', $_[1], $_[2],
500 $second_group, $first_group;
501 } else {
502 9 return sprintf '%sborder-radius%s%s / %s', $_[1], $_[2],
503 $first_group, $second_group;
504 }
505 }
506  
507 ## calculateNewBackgroundPosition ($&, $1, $2, $3, $4, $5)
508 ##
509 ## Changes horizontal background-position percentages, e.g.:
510 ## 'background-position: 75% 50%' => 'background-position: 25% 50%'
511 #
512 #sub calculateNewBackgroundPosition {
513 # my $self = shift;
514 # my @m = @_;
515 # my $new_x;
516 # my $position_string;
517 #
518 # # The flipped value is the offset from 100%
519 # $new_x = 100 - int($m[4]);
520 #
521 # # Since m.group(1) may very well be None type and we need a string..
522 # if ($m[1]) {
523 # $position_string = $m[1];
524 # } else {
525 # $position_string = '';
526 # }
527 #
528 # return sprintf 'background%s%s%s%s%%%s',
529 # $position_string, $m[2], $m[3], $new_x, $m[5];
530 #}
531  
532 # calculateNewBackgroundPositionX ($&, $1, $2)
533 #
534 # Fixes percent based background-position-x, e.g.:
535 # 'background-position-x: 75%' => 'background-position-x: 25%'
536  
537 12 ▶sub calculateNewBackgroundPositionX {
Subroutine calculateNewBackgroundPositionX: called CC=1 SCAR=0.0
Pod undocumented
538 1 my $self = shift;
539 1 my @m = @_;
540 1 my $new_x;
541  
542 # The flipped value is the offset from 100%
543 1 $new_x = 100 - int($m[2]);
544  
545 1 return sprintf 'background-position-x%s%s%%', $m[1], $new_x;
546 }
547  
548 34 my $BACKGROUND_POSITION_ERROR_MESSAGE =
549 "Unmirrorable position value \"%s\": %s\n";
550  
551 12 ▶sub warnForBackgroundPosition {
Subroutine warnForBackgroundPosition: called CC=2 SCAR=7.5
Pod undocumented
552 1 my $self = shift;
553 1 my $bad_length = shift;
554 1 my $whole_value = shift;
555  
556 1 my $msg = sprintf $BACKGROUND_POSITION_ERROR_MESSAGE, $bad_length,
557 $whole_value;
558 1 ▶ if ($self->{'ignore_bad_bgp'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ignore_bad_bgp'}) { } 0 1 1
559 0 $@ = $msg;
560 0 carp $msg;
561 } else {
562 1 croak $msg;
563 }
564 }
565  
566 ## calculateNewBackgroundLengthPosition ($&, $1, $2, $3, $4, $5)
567 ##
568 ## Changes horizontal background-position lengths, e.g.:
569 ## 'background-position: 0px 10px' => 'background-position: 100% 10px'
570 ##
571 ## If value is not replaceable, croak it (by default) or carp it (if
572 ## 'ignore_bad_bgp' option is set).
573 #
574 #sub calculateNewBackgroundLengthPosition {
575 # my $self = shift;
576 # my @m = @_;
577 # my $position_string;
578 #
579 # # croak if the length is not zero-valued
580 # unless ($m[4] =~ m{^$ZERO_LENGTH}) {
581 # $self->warnForBackgroundPosition($m[4], $m[0]);
582 # return $m[0];
583 # }
584 #
585 # if (defined $m[1] and length $m[1]) {
586 # $position_string = $m[1];
587 # } else {
588 # $position_string = '';
589 # }
590 #
591 # return sprintf 'background%s%s%s100%%%s',
592 # $position_string, $m[2], $m[3], $m[5];
593 #}
594  
595 # calculateNewBackgroundLengthPositionX ($&, $1, $2)
596 #
597 # Fixes background-position-x lengths, e.g.:
598 # 'background-position-x: 0' => 'background-position-x: 100%'
599 #
600 # If value is not replaceable, croak it (by default) or carp it (if
601 # 'ignore_bad_bgp' option is set).
602  
603 10 ▶sub calculateNewBackgroundLengthPositionX {
Subroutine calculateNewBackgroundLengthPositionX: called CC=2 SCAR=7.9
Pod undocumented
604 2 my $self = shift;
605 2 my @m = @_;
606  
607 # croak if the length is not zero-valued
608 2 ▶ unless ($m[2] =~ m{^$ZERO_LENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m[2] =~ /^$CSS::Yamaantaka::ZERO_LENGTH/) 0 2 2
609 0 $self->warnForBackgroundPosition($m[2], $m[0]);
610 0 return $m[0];
611 }
612  
613 2 return sprintf 'background-position-x%s100%%', $m[1];
614 }
615  
616 # calculateNewBackgroundQuantityPosition ($&, $1, $2, $3, $4, $5, $6)
617 #
618 # Changes background-position percentages, e.g.:
619 # 'background-position: 75% 50%' => 'background-position: 25% 50%'
620  
621 28 ▶sub calculateNewBackgroundQuantityPosition {
Subroutine calculateNewBackgroundQuantityPosition: called CC=9 SCAR=22.2
Pod undocumented
622 21 my $self = shift;
623 21 my @m = @_;
624 21 my $adaptor = $self->{'adaptor'};
625 21 my $position_string;
626  
627 21 my @pos = ($m[6], undef, undef, $m[4]);
628 # The flipped value is the offset from 100%
629 21 ▶ if ($pos[3] =~ m{^($NUM)\%$}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pos[3] =~ /^($CSS::Yamaantaka::NUM)\%$/) { } 9 12 21
elsif ($pos[3] =~ /^$CSS::Yamaantaka::ZERO_LENGTH/) { } 11 1 12
elsif ($pos[3] =~ /auto|inherit/) { } 0 1 1
630 9 $pos[1] = (100 - int($1)) . '%';
631 } elsif ($pos[3] =~ m{^$ZERO_LENGTH}) {
632 11 $pos[1] = '100%';
633 } elsif ($pos[3] =~ m{auto|inherit}) {
634 0 $pos[1] = $pos[3];
635 }
636 21 ▶ if ($pos[0] =~ m{^($NUM)\%$}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pos[0] =~ /^($CSS::Yamaantaka::NUM)\%$/) { } 15 6 21
elsif ($pos[0] =~ /^$CSS::Yamaantaka::ZERO_LENGTH/) { } 4 2 6
elsif ($pos[0] =~ /auto|inherit/) { } 2 0 2
637 15 $pos[2] = (100 - int($1)) . '%';
638 } elsif ($pos[0] =~ m{^$ZERO_LENGTH}) {
639 4 $pos[2] = '100%';
640 } elsif ($pos[0] =~ m{auto|inherit}) {
641 2 $pos[2] = $pos[0];
642 }
643  
644 21 @pos = $adaptor->reorderFourPartNotation(@pos);
645  
646 21 ▶ unless (defined $pos[0] and defined $pos[3]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos[0] and defined $pos[3]) 1 20 21
Condition66%2/3
defined $pos[0] and defined $pos[3]
!ll&&!rl&&r
0120
MC/DC50%1/2
defined $pos[0] and defined $pos[3]
defined $pos[0] defined $pos[3]
Truth table66%2/3
A and B
A= defined $pos[0]B= defined $pos[3]
ABresult
0X0
100
111
647 1 $self->warnForBackgroundPosition("$m[4]$m[5]$m[6]", $m[0]);
648 0 return $m[0];
649 }
650  
651 20 return sprintf 'background%s%s%s%s%s%s',
652 $m[1], $m[2], $m[3], $pos[3], $m[5], $pos[0];
653 }
654  
655 =head2 Methods
656  
657 =over 4
658  
659 =item body_direction
660  
661 Get direction property or dir attribute of body element thought to be
662 appropriate.
663 Returns C<'ltr'>, C<'rtl'> or undef (unknown).
664  
665 =back
666  
667 =cut
668  
669 7 ▶sub body_direction {
Subroutine body_direction: called CC=2 SCAR=17.9
Pod documented
670 0 ▶ shift->{'body_direction'} || undef;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"body_direction"} or undef 0 0 0
671 }
672  
673 =over 4
674  
675 =item text_orientation
676  
677 Get text-orientation property of texts assumed.
678 Returns C<'sideways-left'> or undef (upright or sideways-right is assumed).
679  
680 =back
681  
682 =cut
683  
684 7 ▶sub text_orientation {
Subroutine text_orientation: called CC=2 SCAR=17.9
Pod documented
685 0 ▶ shift->{'text_orientation'} || undef;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"text_orientation"} or undef 0 0 0
686 }
687  
688 =over 4
689  
690 =item transform ( $lines, [ options... ] )
691  
692 Runs the fixing functions against CSS source.
693  
694 $lines is a string.
695 Following options are available.
696  
697 =over 4
698  
699 =item flip_bg =E<gt> 0|1
700  
701 =item flip_cursor =E<gt> 0|1
702  
703 =item flip_url =E<gt> 0|1
704  
705 =item swap_ltr_rtl_in_url =E<gt> 0|1
706  
707 Overrides these flags if params are set.
708  
709 =back
710  
711 Returns same lines directions are changed.
712  
713 =back
714  
715 =cut
716  
717 625 ▶sub transform {
Subroutine transform: called CC=13 SCAR=26.1
Pod documented
718 618 my $self = shift;
719 618 my $line = shift;
720 618 my %opts = @_;
721  
722 618 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 618 618
723 618 ▶ return $line unless $self->{'adaptor'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"adaptor"} 0 618 618
724  
725 # Possibly override flags with params.
726 618 my $swap_ltr_rtl_in_url = $opts{'swap_ltr_rtl_in_url'};
727 618 my $flip_url = $opts{'flip_url'};
728 618 my $flip_cursor = $opts{'flip_cursor'};
729 618 my $flip_bg = $opts{'flip_bg'};
730  
731 # compat.
732 618 ▶ if (defined $opts{'swap_left_right_in_url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"swap_left_right_in_url"}) 0 618 618
733 0 $flip_url = $opts{'swap_left_right_in_url'};
734 }
735  
736 618 ▶ unless (defined $swap_ltr_rtl_in_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $swap_ltr_rtl_in_url) 618 0 618
737 618 $swap_ltr_rtl_in_url = $self->{'swap_ltr_rtl_in_url'};
738 }
739 618 ▶ unless (defined $flip_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flip_url) 618 0 618
740 618 $flip_url = $self->{'flip_url'};
741 }
742 618 ▶ unless (defined $flip_cursor) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flip_cursor) 618 0 618
743 618 $flip_cursor = $self->{'flip_cursor'};
744 }
745 618 ▶ unless (defined $flip_bg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flip_bg) 618 0 618
746 618 $flip_bg = $self->{'flip_bg'};
747 }
748  
749 618 my @originals = ();
750  
751 # Tokenize tokens tokenizer can be confused.
752 618 $line =~ s{(~[A-Z_\d]+~)}{
753 0 push @originals, $1;
754 0 '~X_' . (scalar @originals) . '~'
755 }eg;
756  
757 # Tokenize any single line rules with the /* noflip */ annotation.
758 618 $line =~ s{$NOFLIP_SINGLE_RE}{
759 4 push @originals, $1;
760 4 '~NOFLIP_SINGLE_' . (scalar @originals) . '~'
761 }eg;
762  
763 # Tokenize any class rules with the /* noflip */ annotation.
764 618 $line =~ s{$NOFLIP_CLASS_RE}{
765 5 push @originals, $1;
766 5 '~NOFLIP_CLASS_' . (scalar @originals) . '~'
767 }eg;
768  
769 # Tokenize the comments so we can preserve them through the changes.
770 618 $line =~ s{$COMMENT_RE}{
771 8 push @originals, $1;
772 8 '~C_' . (scalar @originals) . '~'
773 }eg;
774  
775 # Tokenize gradients since we don't want to mirror the values inside
776 $line = $self->substituteGradient(
777 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
778 2 push @originals, shift;
779 2 '~GRADIENT_' . (scalar @originals) . '~';
780 },
781 618 $line
782 );
783  
784 # Tokenize line-relative properties if any, because
785 # direction of line-relative properties should not be modified
786 # except true ltr-rtl swapping.
787 618 ▶ unless ($self->{'adaptor'}->willReverseLineRelativeDirection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"adaptor"}->willReverseLineRelativeDirection) 472 146 618
788 472 $line =~ s{$LINE_RELATIVE_DIRECTION_RE}{
789 72 push @originals, $1;
790 72 '~LINE_RELATIVE_' . (scalar @originals) . '~'
791 }eg;
792 }
793  
794 # Tokenize properties including "right"/"left" not to be changed.
795 618 $line =~ s{$PROHIBITED_DIRECTION_RE}{
796 0 push @originals, $1;
797 0 '~PROHIBITED_DIRECTION_' . (scalar @originals) . '~'
798 }eg;
799  
800 # Here start the various direction fixes.
801  
802 618 $line = $self->fixBodyDirectionLtrAndRtl($line);
803  
804 618 ▶ if ($flip_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flip_url) 66 552 618
805 66 $line = $self->fixBoxDirectionInUrl($line);
806 }
807  
808 618 ▶ if ($swap_ltr_rtl_in_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swap_ltr_rtl_in_url) 66 552 618
809 66 $line = $self->fixLtrAndRtlInUrl($line);
810 }
811  
812 618 $line = $self->fixSingleBorderRadius($line);
813  
814 # Since BoxDirection conflicts with SingleBorderRadius, we tokenize
815 # border-<corner>-radius properties here.
816 618 $line =~ s{$SINGLE_BORDER_RADIUS_TOKENIZER_RE}{
817 24 push @originals, $1;
818 24 '~SINGLE_BORDER_RADIUS_' . (scalar @originals) . '~'
819 }eg;
820 618 $line = $self->fixBoxDirection($line);
821 618 $line =~ s{~SINGLE_BORDER_RADIUS_(\d+)~}{$originals[$1 - 1]}eg;
822  
823 618 ▶ if ($flip_cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flip_cursor) 618 0 618
824 618 $line = $self->fixCursorProperties($line);
825 }
826  
827 618 $line = $self->fixBorderRadius($line);
828  
829 # Since FourPartNotation conflicts with BorderRadius, we tokenize
830 # border-radius properties here.
831 618 $line =~ s{$BORDER_RADIUS_TOKENIZER_RE}{
832 28 push @originals, $1;
833 28 '~BORDER_RADIUS_' . (scalar @originals) . '~'
834 }eg;
835 618 $line = $self->fixFourPartNotation($line);
836 618 $line =~ s{~BORDER_RADIUS_(\d+)~}{$originals[$1 - 1]}eg;
837  
838 618 ▶ if ($flip_bg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flip_bg) 618 0 618
839 618 $line = $self->fixBackgroundPosition($line);
840 }
841  
842 # DeTokenize properties including "right"/"left" not to be fixed
843 0 $line =~ s{~PROHIBITED_DIRECTION_(\d+)~}{$originals[$1 - 1]}eg;
844  
845 # DeTokenize line-relative properties, if any
846 617 $line =~ s{~LINE_RELATIVE_(\d+)~}{$originals[$1 - 1]}eg;
847  
848 # DeTokenize gradients
849 617 $line =~ s{~GRADIENT_(\d+)~}{$originals[$1 - 1]}eg;
850  
851 # DeTokenize the single line noflips.
852 617 $line =~ s{~NOFLIP_SINGLE_(\d+)~}{$originals[$1 - 1]}eg;
853  
854 # DeTokenize the class-level noflips.
855 617 $line =~ s{~NOFLIP_CLASS_(\d+)~}{$originals[$1 - 1]}eg;
856  
857 # DeTokenize the comments.
858 617 $line =~ s{~C_(\d+)~}{$originals[$1 - 1]}eg;
859  
860 # Detokenize tokens tokenizer can be confused.
861 0 $line =~ s{~X_(\d+)~}{$originals[$1 - 1]}eg;
862  
863 617 return $line;
864 }
865  
866 =over 4
867  
868 =item writing_mode
869  
870 Get writing-mode property of texts thought to be appropriate.
871 Returns C<'horizontal-tb'>, C<'vertical-lr'>, C<'vertical-rl'> or undef
872 (unknown).
873  
874 =back
875  
876 =cut
877  
878 7 ▶sub writing_mode {
Subroutine writing_mode: called CC=2 SCAR=17.9
Pod documented
879 0 ▶ shift->{'writing_mode'} || undef;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"writing_mode"} or undef 0 0 0
880 }
881  
882 =head2 Adaptors
883  
884 This module supports four directions of documents:
885  
886 =over 4
887  
888 =item lr-tb
889  
890 The direction specified by
891 C<{ direction: ltr; writing-mode: horizontal-tb; }>.
892 For example, most Western writing systems employ it.
893  
894 =item rl-tb
895  
896 The direction specified by
897 C<{ direction: rtl; writing-mode: horizontal-tb; }>.
898 For example, some Middle Eastern writing systems employ it.
899  
900 =item tb-lr
901  
902 The direction specified by
903 C<{ writing-mode: vertical-lr; }>.
904 For example, several North Asian writing systems employ it.
905  
906 =item lr-tb
907  
908 The direction specified by
909 C<{ writing-mode: vertical-rl; }>.
910 East Asian writing systems with vertical layout employ it.
911  
912 =back
913  
914 This module chooses adaptors by source & resulting directions:
915  
916 table 1. Choosing adaptors
917 +-----------+-------------+-------------+-------------+--------------+
918 | from \ to | lr-tb : rl-tb : tb-lr : tb-rl |
919 +-----------+-------------+-------------+-------------+--------------+
920 | lr-tb | - : MirrorH : MirrorTL_BR : RotateR |
921 | rl-tb | MirrorH : - : RotateL* : MirrorTR_BL* |
922 | tb-lr | MirrorTL_BR : RotateR : - : MirrorV |
923 | tb-rl | RotateL : MirrorTR_BL : MirrorV : - |
924 +-----------+-------------+-------------+-------------+--------------+
925 * Assumed text-orientation: sideways-left.
926
927 n.b.: Prefixing "CSS::Yamaantaka::" are omitted.
928  
929 Each adaptor will or won't change following "directions" of CSS properties.
930  
931 =over 4
932  
933 =item line-relative box directions
934  
935 "right" / "left" of text-align, float and clear.
936 "top" / "bottom" of vertical-align.
937  
938 =item physical box directions
939  
940 "top" / "right" / "bottom" / "left".
941  
942 =item global directions
943  
944 Directions specified by body element, "ltr" / "rtl".
945  
946 =item direction swapping
947  
948 Horizontal and vertical orientation.
949  
950 =back
951  
952 table 2. Feature of adaptors
953 +-------------+-----------+-------------------------+---------+------+
954 | | line-rel. | box directions : global : h/v |
955 +-------------+-----------+-------------------------+---------+------+
956 | MirrorH | reverse h.: reverse horizontally : reverse : - |
957 | MirrorV | - : reverse horizontally : reverse : - |
958 | RotateR | - : rotate clockwise : reverse : swap |
959 | RotateL | - : rotate counter-clockwise: reverse : swap |
960 | MirrorTL_BR | - : reverse with tl-br axis : - : swap |
961 | MirrorTR_BL | - : reverse with tr-bl axis : - : swap |
962 +-------------+-----------+-------------------------+---------+------+
963  
964 Any adaptors listed above won't fix line-relative text directions
965 ("rtl" / "ltr").
966  
967 =head1 VERSION
968  
969 Consult C<$VERSION> variable.
970  
971 =head1 SEE ALSO
972  
973 L<CSS::Janus>
974  
975 Extended CSSJanus supporting vertical-rl writing-mode:
976 L<http://www.epubcafe.jp/download>
977  
978 L<cssflip(1)>
979  
980 =head1 AUTHOR
981  
982 Hatuka*nezumi - IKEDA Soji <hatuka(at)nezumi.nu>.
983  
984 =head1 COPYRIGHT
985  
986 Copyright (C) 2013 Hatuka*nezumi - IKEDA Soji.
987  
988 This program is free software; you can redistribute
989 it and/or modify it under the same terms as Perl itself.
990  
991 =cut
992  
993 34 1;
↑ Summary Janus.pm »