File: Import.pm

package info (click to toggle)
libcss-dom-perl 0.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: perl: 7,310; makefile: 2
file content (167 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package CSS::DOM::Rule::Import;

$VERSION = '0.17';

use warnings; no warnings qw 'utf8 parenthesis';
use strict;

use CSS::DOM;
use CSS::DOM::Exception qw/ SYNTAX_ERR /;
 use        CSS::DOM::Rule;

our @ISA = 'CSS::DOM::Rule';

use constant 1.03 our $_const = {
# Don't let this conflict with the superclass.
	hrfe => 2,
	medi => 3,
	shet => 4,
	urlt => 5, # url token
};
{ no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }


# overrides:
sub type { CSS::DOM::Rule::IMPORT_RULE }
sub cssText {
	my $self = shift;
	my $old;
	if(defined wantarray) {
		$old = "\@import $self->[urlt][1]" . (
			$self->[medi] ? ' '.$self->[medi]->mediaText : ''
		) . ";\n";
	}
	if (@_) {
		@$self[hrfe,medi,shet,urlt] =
			@{$self->_parse(shift)}[hrfe,medi,shet,urlt];
#use DDS; Dump $self;
	}
	$old;
};


# CSSImportRule interface:

sub href {
	my $self =shift;
	$self->[hrfe] ||= do {
                require CSS::DOM::Parser;
		if($self->[urlt][00] eq 'u') {
			# ~~~ I probably ought to put things like this in a
			#     Parser::token_val function.
			my $url = $self->[urlt][1];
			$url =~ s/^url\([ \t\r\n\f]*//;
			$url =~ s/[ \t\r\n\f]*\)\z//;
			$url =~ s/^['"]// and chop $url;
                        CSS::DOM::Parser::unescape($url);
		}
		else {
                        CSS::DOM::Parser::unescape(
				substr $$self[urlt][1], 1, -1
			)
		}
	}
}
sub _set_url_token {
	for(shift) {
		delete $_->[hrfe];
		$_->[urlt] = \@_;
	}
}

sub media {
	wantarray ? @{$_[0]->[medi]||return} :
		($_[0]->[medi] ||= (
			require CSS::DOM::MediaList,
			CSS::DOM::MediaList->new
		))
}

sub styleSheet{
	# I use 0 instead of undef for a non-existent style sheet. undef is
	# used to mean that we haven’t even  considered  loading  it  yet.
	# Using existence of the element as the criterion makes the code
	# too unmaintainable.
	my $self = shift;
	unless( defined($self->[shet])) {
		my $fetcher = $self->parentStyleSheet->url_fetcher;
		# ~~~ What do we do about the charset?
		my($css,@args);
		($css,@args) = $fetcher->($self->href) if defined $fetcher;
		defined $css or $self->[shet]=0, return;		
		require CSS::DOM::Parser;
		for(($self->[shet] =
		         eval{CSS::DOM::Parser::parse($css,@args)}||0)
		    || return){
			$_->_set_ownerRule($self);
			if(my$parent=$self->parentStyleSheet){
				$_->_set_parentStyleSheet($parent);
				$_->url_fetcher($parent->url_fetcher);
			}
			return $_;
		}
	}
	return $self->[shet]||();
}

                              !()__END__()!

=head1 NAME

CSS::DOM::Rule::Import - CSS @import rule class for CSS::DOM

=head1 VERSION

Version 0.17

=head1 SYNOPSIS

  use CSS::DOM;
  my $import_rule = CSS::DOM->parse(
      '@import "print.css" print;',
      url_fetcher => sub { 
          # ... code to get the url in $_[0] ...
      }
  )->cssRules->[0];

  $import_rule->href;  # 'print.css'
  $import_rule->media; # a CSS::DOM::MediaList (array ref)
  $import_rule->styleSheet; # a CSS::DOM object

=head1 DESCRIPTION

This module implements CSS C<@import> rules for L<CSS::DOM>. It inherits 
from
L<CSS::DOM::Rule> and implements
the CSSImportRule DOM interface.

=head1 METHODS

=over 4

=item href

Returns the @import rule's URL.

=item media

Returns the MediaList associated with the @import rule (or a plain list in
list context). This defaults to an
empty list. You can pass a comma-delimited string to the MediaList's
C<mediaText> method to set it.

=item styleSheet

This returns the style sheet object, if available. Otherwise it returns an
empty list (this occurs if C<url_fetcher> is not provided or if it returns
undef).

=back

=head1 SEE ALSO

L<CSS::DOM>

L<CSS::DOM::Rule>

L<CSS::DOM::MediaList>