SAC/TestWriter.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 102 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 474 CC 48 SCAR 51.0
CC
48
Cov
0%
stringify_condition
462.0
stringify_selector
156.0
stringify_lexical_unit
72.0
CRAP 163.5
1
2 ###
3 # SAC Test Writer - the writer used in the tests
4 # Robin Berjon <robin@knowscape.com>
5 # 23/04/2001
6 ###
7
8 package CSS::SAC::TestWriter;
9 use strict;
10 use vars qw($VERSION $ident $spacer);
11 $VERSION = '0.01';
12 $spacer = ' ';
13
14 use CSS::SAC::Selector qw(:constants);
15 use CSS::SAC::Condition qw(:constants);
16 use CSS::SAC::LexicalUnit qw(:constants);
17
18
19 #---------------------------------------------------------------------#
20 # build the fields for an array based object
21 #---------------------------------------------------------------------#
22 use Class::ArrayObjects define => {
23 fields => [qw(
24 _nsmap_
25 _ref_
26 )],
27 };
28 #---------------------------------------------------------------------#
29
30
31
32 ### Constructor #######################################################
33 # #
34 # #
35
36
37 #---------------------------------------------------------------------#
38 # CSS::SAC::TestWriter->new(\$stringref)
39 # creates a new sac doc handler
40 #---------------------------------------------------------------------#
41 sub new {
42 my $class = ref($_[0])?ref(shift):shift;
43 my $ref = shift;
44
45 # prepare the object and the namespace map
46 $ident = 1;
47 my $self = [];
48 $self->[_nsmap_] = {};
49 $self->[_ref_] = $ref;
50
51 return bless $self, $class;
52 }
53 #---------------------------------------------------------------------#
54
55
56 # #
57 # #
58 ### Constructor #######################################################
59
60
61
62 ### Callbacks #########################################################
63 # #
64 # #
65
66
67 #---------------------------------------------------------------------#
68 # start_document
69 #---------------------------------------------------------------------#
70 sub start_document {
71 my $dh = shift;
72 $dh->[_ref_] .= $spacer x $ident . "Stylesheet:\n";
73 $ident++;
74 }
75 #---------------------------------------------------------------------#
76
77
78 #---------------------------------------------------------------------#
79 # end_document
80 #---------------------------------------------------------------------#
81 sub end_document {
82 my $dh = shift;
83 $ident--;
84 $dh->[_ref_] .= $spacer x $ident . "End.\n";
85 }
86 #---------------------------------------------------------------------#
87
88
89 #---------------------------------------------------------------------#
90 # start_selector($sel_list)
91 #---------------------------------------------------------------------#
92 sub start_selector {
93 my $dh = shift;
94 my $sel_list = shift;
95
96 $dh->[_ref_] .= $spacer x $ident . "Style Rule:\n";
97 $ident++;
98 $dh->[_ref_] .= $spacer x $ident . "Selector:\n";
99 $ident++;
100 $dh->[_ref_] .= $spacer x $ident . "Chain:\n";
101 $ident++;
102
103 my @sel_strings;
104 for my $sel (@$sel_list) {
105 $dh->[_ref_] .= $spacer x $ident . $dh->stringify_selector($sel) . "\n";
106 }
107 }
108 #---------------------------------------------------------------------#
109
110
111 #---------------------------------------------------------------------#
112 # end_selector($sel_list)
113 #---------------------------------------------------------------------#
114 sub end_selector {
115 my $dh = shift;
116 my $sel_list = shift;
117 $dh->[_out_]->($dh, "\n}\n");
118 }
119 #---------------------------------------------------------------------#
120
121
122 #---------------------------------------------------------------------#
123 # property($name,$lu,$important)
124 #---------------------------------------------------------------------#
125 sub property {
126 my $dh = shift;
127 my $name = shift;
128 my $lu = shift;
129 my $important = shift;
130
131 $dh->[_out_]->($dh, "\n\t$name:\t");
132 while (@$lu) {
133 my $val = shift @$lu;
134 $dh->[_out_]->($dh, $dh->stringify_lexical_unit($val));
135
136 if ($lu->[0]) {
137 if ($lu->[0]->is_type(OPERATOR_COMMA)) {
138 shift @$lu;
139 $dh->[_out_]->($dh, ', ');
140 }
141 elsif ($lu->[0]->is_type(OPERATOR_SLASH)) {
142 shift @$lu;
143 $dh->[_out_]->($dh, '/');
144 }
145 else {
146 $dh->[_out_]->($dh, ' ');
147 }
148 }
149 }
150 $dh->[_out_]->($dh, (($important)?' !important':'') . ';');
151 }
152 #---------------------------------------------------------------------#
153
154
155 #---------------------------------------------------------------------#
156 # ignorable_at_rule($at_rule)
157 #---------------------------------------------------------------------#
158 sub ignorable_at_rule {
159 my $dh = shift;
160 my $at_rule = shift;
161
162 $dh->[_out_]->($dh, "\n/* $at_rule */\n");
163 }
164 #---------------------------------------------------------------------#
165
166
167 #---------------------------------------------------------------------#
168 # import_style($uri,\@media)
169 #---------------------------------------------------------------------#
170 sub import_style {
171 my $dh = shift;
172 my $uri = shift;
173 my $media = shift;
174
175 $dh->[_out_]->($dh, "\n\@import url($uri) " . join(', ', @$media) . ";\n");
176 }
177 #---------------------------------------------------------------------#
178
179
180 #---------------------------------------------------------------------#
181 # namespace_declaration($prefix,$uri)
182 #---------------------------------------------------------------------#
183 sub namespace_declaration {
184 my $dh = shift;
185 my $prefix = shift;
186 my $uri = shift;
187
188 # we need to provide a global ns map here
189 if (defined $prefix) {
190 $dh->[_nsmap_]->{$uri} = $prefix;
191 }
192
193 $dh->[_out_]->($dh, "\n\@namespace" . ((defined $prefix)?" $prefix ":' ') . "url($uri);\n");
194 }
195 #---------------------------------------------------------------------#
196
197
198 #---------------------------------------------------------------------#
199 # start_media(\@media)
200 #---------------------------------------------------------------------#
201 sub start_media {
202 my $dh = shift;
203 my $media = shift;
204
205 $dh->[_out_]->($dh, "\n\@media " . join(', ', @$media) . " {\n");
206 }
207 #---------------------------------------------------------------------#
208
209
210 #---------------------------------------------------------------------#
211 # end_media(\@media)
212 #---------------------------------------------------------------------#
213 sub end_media {
214 my $dh = shift;
215 my $media = shift;
216
217 $dh->[_out_]->($dh, "\n}\n");
218 }
219 #---------------------------------------------------------------------#
220
221
222 #---------------------------------------------------------------------#
223 # comment($comment)
224 #---------------------------------------------------------------------#
225 sub comment {
226 my $dh = shift;
227 my $comment = shift;
228
229 $dh->[_out_]->($dh, "/* $comment */");
230 }
231 #---------------------------------------------------------------------#
232
233
234 #---------------------------------------------------------------------#
235 # charset($charset)
236 #---------------------------------------------------------------------#
237 sub charset {
238 my $dh = shift;
239 my $charset = shift;
240
241 $dh->[_out_]->($dh, "\@charset '$charset';\n");
242 }
243 #---------------------------------------------------------------------#
244
245
246 #---------------------------------------------------------------------#
247 # start_font_face
248 #---------------------------------------------------------------------#
249 sub start_font_face {
250 my $dh = shift;
251
252 $dh->[_out_]->($dh, "\n\@font-face {\n");
253 }
254 #---------------------------------------------------------------------#
255
256
257 #---------------------------------------------------------------------#
258 # end_font_face
259 #---------------------------------------------------------------------#
260 sub end_font_face {
261 my $dh = shift;
262 $dh->[_out_]->($dh, "\n}\n");
263 }
264 #---------------------------------------------------------------------#
265
266
267 #---------------------------------------------------------------------#
268 # start_page($name,$pseudo_page)
269 #---------------------------------------------------------------------#
270 sub start_page {
271 my $dh = shift;
272 my $name = shift;
273 my $pseudo_page = shift;
274
275 $dh->[_out_]->($dh, "\n\@page " . ((defined $name)?"$name ":'') .
276 ((defined $pseudo_page)?":$pseudo_page ":'') . "{\n");
277 }
278 #---------------------------------------------------------------------#
279
280
281 #---------------------------------------------------------------------#
282 # end_page($name,$pseudo_page)
283 #---------------------------------------------------------------------#
284 sub end_page {
285 my $dh = shift;
286 my $name = shift;
287 my $pseudo_page = shift;
288
289 $dh->[_out_]->($dh, "\n}\n");
290 }
291 #---------------------------------------------------------------------#
292
293
294 # #
295 # #
296 ### Callbacks #########################################################
297
298
299
300 ### Helpers ###########################################################
301 # #
302 # #
303
304
305 #---------------------------------------------------------------------#
306 # stringify_selector($sel)
307 # returns a string of that selector
308 #---------------------------------------------------------------------#
309 sub stringify_selector {
310 my $dh = shift;
311 my $sel = shift;
312
313 # child
314 if ($sel->is_type(CHILD_SELECTOR)) {
315 return $dh->stringify_selector($sel->AncestorSelector)
316 . ' > ' .
317 $dh->stringify_selector($sel->SimpleSelector);
318 }
319
320 # descendant
321 elsif ($sel->is_type(DESCENDANT_SELECTOR)) {
322 return $dh->stringify_selector($sel->AncestorSelector)
323 . ' ' .
324 $dh->stringify_selector($sel->SimpleSelector);
325 }
326
327 # direct adjacent
328 elsif ($sel->is_type(DIRECT_ADJACENT_SELECTOR)) {
329 return $dh->stringify_selector($sel->Selector)
330 . ' + ' .
331 $dh->stringify_selector($sel->SiblingSelector);
332 }
333
334 # indirect adjacent
335 elsif ($sel->is_type(INDIRECT_ADJACENT_SELECTOR)) {
336 return $dh->stringify_selector($sel->Selector)
337 . ' ~ ' .
338 $dh->stringify_selector($sel->SiblingSelector);
339 }
340
341 # conditional
342 elsif ($sel->is_type(CONDITIONAL_SELECTOR)) {
343 return $dh->stringify_selector($sel->SimpleSelector)
344 .
345 $dh->stringify_condition($sel->Condition);
346 }
347
348 # negative
349 elsif ($sel->is_type(NEGATIVE_SELECTOR)) {
350 return ':not(' . $dh->stringify_selector($sel->SimpleSelector) . ')';
351 }
352
353 # element
354 elsif ($sel->is_type(ELEMENT_NODE_SELECTOR)) {
355 my $string;
356 if (defined $sel->NamespaceURI) {
357 if (length $sel->NamespaceURI) {
358 $string = $dh->[_nsmap_]->{$sel->NamespaceURI} . '|';
359 } # else we don't put anything and it's in the default ns
360 }
361 else {
362 $string = '*|';
363 }
364 $string .= (defined $sel->LocalName)?$sel->LocalName:'*';
365
366 return $string;
367 }
368
369 # pseudo element
370 elsif ($sel->is_type(PSEUDO_ELEMENT_SELECTOR)) {
371 return '::' . $sel->LocalName;
372 }
373
374 # error ?
375 else {
376 warn "unknown selector type";
377 }
378 }
379 #---------------------------------------------------------------------#
380
381
382
383 #---------------------------------------------------------------------#
384 # stringify_condition($sel)
385 # returns a string of that condition
386 #---------------------------------------------------------------------#
387 sub stringify_condition {
388 my $dh = shift;
389 my $cond = shift;
390
391 # and
392 if ($cond->is_type(AND_CONDITION)) {
393 return $dh->stringify_condition($cond->FirstCondition)
394 .
395 $dh->stringify_condition($cond->SecondCondition);
396 }
397
398 # attr
399 elsif (
400 $cond->is_type(ATTRIBUTE_CONDITION) or
401 $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) or
402 $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) or
403 $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) or
404 $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) or
405 $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION)
406 ) {
407 my $string = '[';
408
409 # the name
410 if (defined $cond->NamespaceURI) {
411 if (length $cond->NamespaceURI) {
412 $string .= $dh->[_nsmap_]->{$cond->NamespaceURI} . '|';
413 }
414 }
415 else {
416 $string .= '*|';
417 }
418 $string .= (defined $cond->LocalName)?$cond->LocalName:'*';
419
420 # the value
421 if ($cond->Specified) {
422 my $op = '=';
423 $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) and $op = '|=';
424 $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) and $op = '~=';
425 $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) and $op = '^=';
426 $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) and $op = '$=';
427 $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION) and $op = '*=';
428
429 # find the right op depending on the attr type
430
431 $string .= "$op'" . $cond->Value . "'";
432 }
433
434 $string .= ']';
435
436 return $string;
437 }
438
439 # class
440 elsif ($cond->is_type(CLASS_CONDITION)) {
441 return '.' . $cond->Value;
442 }
443
444 # content
445 elsif ($cond->is_type(CONTENT_CONDITION)) {
446 return ":contains('" . $cond->Data . "')";
447 }
448
449 # id
450 elsif ($cond->is_type(ID_CONDITION)) {
451 return '#' . $cond->Value;
452 }
453
454 # lang
455 elsif ($cond->is_type(LANG_CONDITION)) {
456 return ":lang(" . $cond->Lang . ")";
457 }
458
459 # negative
460 elsif ($cond->is_type(NEGATIVE_CONDITION)) {
461 return ":not(" . $dh->stringify_condition($cond->Condition) . ")";
462 }
463
464 # only child
465 elsif ($cond->is_type(ONLY_CHILD_CONDITION)) {
466 return ':only-child';
467 }
468
469 # only of type
470 elsif ($cond->is_type(ONLY_TYPE_CONDITION)) {
471 return ':only-of-type';
472 }
473
474 # root
475 elsif ($cond->is_type(IS_ROOT_CONDITION)) {
476 return ':root';
477 }
478
479 # empty
480 elsif ($cond->is_type(IS_EMPTY_CONDITION)) {
481 return ':empty';
482 }
483
484 # pseudo-class
485 elsif ($cond->is_type(PSEUDO_CLASS_CONDITION)) {
486 return ':' . $cond->Value;
487 }
488
489 # positional
490 elsif ($cond->is_type(POSITIONAL_CONDITION)) {
491 my $string;
492
493 # the second part right
494 if ($cond->Type) {
495 $string = 'of-type';
496 }
497 else {
498 $string = 'child';
499 }
500
501 # get the first part right
502 if ($cond->Position == 1) {
503 return ':first-' . $string;
504 }
505 elsif ($cond->Position == -1) {
506 return ':last-' . $string;
507 }
508 else {
509 $string = ':nth-' . $string;
510 }
511
512 # add the expression
513 $string .= '(' . $cond->Position . ')';
514
515 return $string;
516 }
517 }
518 #---------------------------------------------------------------------#
519
520
521 #---------------------------------------------------------------------#
522 # stringify_lexical_unit($sel)
523 # returns a string of that lexical unit
524 #---------------------------------------------------------------------#
525 sub stringify_lexical_unit {
526 my $dh = shift;
527 my $lu = shift;
528
529 # dimensions
530 if (
531 $lu->is_type(CENTIMETER) or $lu->is_type(DEGREE) or
532 $lu->is_type(DIMENSION) or $lu->is_type(EM) or
533 $lu->is_type(EX) or $lu->is_type(GRADIAN) or
534 $lu->is_type(HERTZ) or $lu->is_type(INCH) or
535 $lu->is_type(KILOHERTZ) or $lu->is_type(MILLIMETER) or
536 $lu->is_type(MILLISECOND) or $lu->is_type(PERCENTAGE) or
537 $lu->is_type(PICA) or $lu->is_type(PIXEL) or
538 $lu->is_type(POINT) or $lu->is_type(RADIAN) or
539 $lu->is_type(SECOND)
540 ) {
541 return $lu->Value . $lu->DimensionUnitText;
542 }
543
544 # functions
545 elsif (
546 $lu->is_type(ATTR) or $lu->is_type(COUNTER_FUNCTION) or
547 $lu->is_type(URI) or $lu->is_type(COUNTERS_FUNCTION) or
548 $lu->is_type(FUNCTION) or $lu->is_type(RECT_FUNCTION)
549 ) {
550 return $lu->FunctionName . '(' . $lu->Value . ')';
551 }
552
553 # inherit
554 elsif ($lu->is_type(INHERIT)) {
555 return 'inherit';
556 }
557
558 # ident, number, unicoderange
559 elsif ($lu->is_type(IDENT) or $lu->is_type(INTEGER) or
560 $lu->is_type(REAL) or $lu->is_type(UNICODERANGE)) {
561 return $lu->Value;
562 }
563
564 # string
565 elsif ($lu->is_type(STRING_VALUE)) {
566 return "'" . $lu->Value . "'";
567 }
568
569 # rgbcolor
570 elsif ($lu->is_type(RGBCOLOR)) {
571 if ($lu->FunctionName eq 'rgb') {
572 return 'rgb(' . $lu->Value . ')';
573 }
574 else {
575 return '#' . $lu->Value;
576 }
577 }
578 }
579 #---------------------------------------------------------------------#
580
581
582 # #
583 # #
584 ### Helpers ###########################################################
585
586
587
588 ### Error Callbacks ###################################################
589 # #
590 # #
591
592
593 #---------------------------------------------------------------------#
594 # warning($warning)
595 #---------------------------------------------------------------------#
596 sub warning {
597 my $eh = shift;
598 my $warning = shift;
599
600 warn "[WARN] $warning\n";
601 }
602 #---------------------------------------------------------------------#
603
604
605 #---------------------------------------------------------------------#
606 # error($error)
607 #---------------------------------------------------------------------#
608 sub error {
609 my $eh = shift;
610 my $error = shift;
611
612 warn "[ERROR] $error\n";
613 }
614 #---------------------------------------------------------------------#
615
616
617 #---------------------------------------------------------------------#
618 # fatal_error($error)
619 #---------------------------------------------------------------------#
620 sub fatal_error {
621 my $eh = shift;
622 my $error = shift;
623
624 die "[FATAL] $error\n";
625 }
626 #---------------------------------------------------------------------#
627
628
629 # #
630 # #
631 ### Error Callbacks ###################################################
632
633 1;
634
635 =pod
636
637 =head1 SYNOPSIS
638
639 use CSS::SAC qw();
640 use CSS::SAC::Writer ();
641
642 ### create a doc handler using the writer
643 # options can also be ioref and string (given a stringref) in which
644 # case it'll write to the filehandle or to the string.
645 # Yes, it also works as an ErrorHandler (though not a good one)
646
647 my $doc_h = CSS::SAC::Writer->new({ filename => 'out.css' });
648 my $sac = CSS::SAC->new({
649 DocumentHandler => $doc_h,
650 ErrorHandler => $doc_h,
651 });
652
653 # generate a stream of events
654 $sac->parse({ filename => 'foo.css' });
655
656 =head1 DESCRIPTION
657
658 This is a simplistic SAC handler that demonstrates how one may use
659 CSS::SAC. More useful ones will follow. Obviously, it isn't documented
660 much, given that its value resides mostly in the source code :)
661
662 You can of course still use it as a way to write CSS from a SAC stream.
663
664 =head1 AUTHOR
665
666 Robin Berjon <robin@knowscape.com>
667
668 This module is licensed under the same terms as Perl itself.
669
670 =cut
671