~ubuntu-branches/ubuntu/quantal/libcss-dom-perl/quantal

« back to all changes in this revision

Viewing changes to lib/CSS/DOM/Rule.pm

  • Committer: Bazaar Package Importer
  • Author(s): Nicholas Bamber
  • Date: 2011-02-10 21:51:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110210215103-vaalgfelzvw4bar4
Tags: upstream-0.14
Import upstream version 0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package CSS::DOM::Rule;
 
2
 
 
3
$VERSION = '0.14';
 
4
 
 
5
use warnings;
 
6
use strict;
 
7
 
 
8
use Carp 'croak';
 
9
use CSS::DOM::Constants;
 
10
use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
 
11
use Exporter 5.57 'import';
 
12
use Scalar::Util 'weaken';
 
13
 
 
14
*EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
 
15
our %EXPORT_TAGS = (all => \our @EXPORT_OK);
 
16
 
 
17
use constant 1.03 our $_const = {
 
18
# Don’t let these conflict with subclasses!
 
19
        prnt => 0,
 
20
        shet => 1,
 
21
        typs => 2, # token types   These two are not used by subclassed,
 
22
        tokn => 3, # tokens        so there’s no chance of a conflict.
 
23
};
 
24
{ no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
 
25
 
 
26
sub new {
 
27
        my $self = bless[],shift;
 
28
        my $parent = shift || return $self;
 
29
        if($parent->isa('CSS::DOM::Rule')) {
 
30
                weaken($$self[shet] = $parent->parentStyleSheet);
 
31
                weaken($$self[prnt] = $parent);
 
32
        }
 
33
        else {
 
34
                weaken($$self[shet] = $parent)
 
35
        }
 
36
        $self;
 
37
}
 
38
 
 
39
sub type { UNKNOWN_RULE }
 
40
 
 
41
# This is used by cssText, both this class’s and subclasses’:
 
42
sub _parse { # This method parses the code passed to it and checks to see
 
43
             # whether the retval is the same class as  $self,  throwing
 
44
             # errors as appropriate.  It returns the new rule resulting
 
45
             # from the parse. Each subclass is responsible for extorting
 
46
             # the rule data from the new rule.
 
47
        my $self = shift;
 
48
        require CSS::DOM::Parser;
 
49
        my $new_rule  =  CSS::DOM::Parser'parse_statement(shift)
 
50
                || die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
 
51
 
 
52
        ref $new_rule eq ref $self or die CSS::DOM::Exception->new(
 
53
                INVALID_MODIFICATION_ERR,
 
54
                "The rule cannot be converted to a different type."
 
55
        );
 
56
        $new_rule;
 
57
};
 
58
 
 
59
sub cssText {
 
60
        my $self = shift;
 
61
        my $old;
 
62
        if(defined wantarray) {
 
63
                $old = join '', @{$self->[tokn]},;
 
64
                $old .= ';' unless $self->[typs] =~ /[;}]\z/;
 
65
                $old .= "\n";
 
66
        }
 
67
        if (@_) {
 
68
                my $new_rule  =  $self->_parse(shift);
 
69
                @$self[typs,tokn] = @$new_rule[typs,tokn];
 
70
        }
 
71
        $old;
 
72
};
 
73
 
 
74
 
 
75
sub parentStyleSheet { shift->[shet]||() }
 
76
sub parentRule       { shift->[prnt]||() }
 
77
 
 
78
sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
 
79
sub _set_parentRule       { weaken(shift->[prnt] = pop) }
 
80
 
 
81
sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
 
82
  
 
83
                              !()__END__()!
 
84
 
 
85
=head1 NAME
 
86
 
 
87
CSS::DOM::Rule - CSS rule class for CSS::DOM
 
88
 
 
89
=head1 VERSION
 
90
 
 
91
Version 0.14
 
92
 
 
93
=head1 SYNOPSIS
 
94
 
 
95
  use CSS::DOM::Rule ':all'; # import constants
 
96
 
 
97
  use CSS::DOM;
 
98
  $sheet = new CSS::DOM;
 
99
  $sheet->insertRule('bla blah blah {}');
 
100
  $rule = $sheet->cssRules->[0];
 
101
  
 
102
  $rule->type; # STYLE_RULE
 
103
  $rule->cssText; # 'bla blah blah {}' or similar
 
104
  $rule->cssText('p { margin: 0 }'); # replace it
 
105
  $rule->parentStyleSheet; # $sheet
 
106
 
 
107
=head1 DESCRIPTION
 
108
 
 
109
This module provides the CSS rule class for L<CSS::DOM>. It implements
 
110
the CSSRule and CSSUnknownRule DOM interfaces.
 
111
 
 
112
=head1 METHODS
 
113
 
 
114
=head2 Constructor
 
115
 
 
116
Only call the constructor on this class to create an 'unknown' rule. You have to
 
117
call the constructor on a particular subclass to get another type. Normally
 
118
you do not need to 
 
119
call this directly anyway. (See L<CSS::DOM>'s 
 
120
C<parse> and C<insertRule> methods.) But just in case you do want to call 
 
121
it, here it
 
122
is:
 
123
 
 
124
  new CSS::DOM::Rule $parent; # unknown rule
 
125
  
 
126
  require CSS::DOM::Rule::Style
 
127
  new CSS::DOM::Rule::Style $parent;
 
128
  # etc.
 
129
 
 
130
C<$parent> is the parent rule, if the rule is nested, or the parent style
 
131
sheet otherwise.
 
132
 
 
133
=head2 Object Methods
 
134
 
 
135
=over 4
 
136
 
 
137
=item type
 
138
 
 
139
Returns one of the constants below indicating the type of rule.
 
140
 
 
141
=item cssText
 
142
 
 
143
Returns this rule's CSS code. If you pass an argument, it will be parsed as
 
144
the new CSS code for this rule (replacing the existing data), and the old
 
145
value will be returned. This method will die if the replacement CSS code
 
146
creates a different type of rule.
 
147
 
 
148
=item parentStyleSheet
 
149
 
 
150
This returns the style sheet to which the rule belongs.
 
151
 
 
152
=item parentRule
 
153
 
 
154
This returns the rule's parent rule, if there is one, or an empty list
 
155
otherwise. There is only a parent rule if this one is nested, e.g., inside
 
156
a media rule.
 
157
 
 
158
=back
 
159
 
 
160
=head1 EXPORTS
 
161
 
 
162
The following constants that indicate the type of rule will be exported on
 
163
request (individually or with the ':all' tag):
 
164
 
 
165
  UNKNOWN_RULE
 
166
  STYLE_RULE    
 
167
  CHARSET_RULE  
 
168
  IMPORT_RULE   
 
169
  MEDIA_RULE    
 
170
  FONT_FACE_RULE
 
171
  PAGE_RULE
 
172
 
 
173
=head1 SEE ALSO
 
174
 
 
175
L<CSS::DOM>
 
176
 
 
177
L<CSS::DOM::Rule::Style>
 
178
 
 
179
L<CSS::DOM::Rule::Media>
 
180
 
 
181
L<CSS::DOM::Rule::Page>
 
182
 
 
183
L<CSS::DOM::Rule::Import>