Subject: | Can't call read() or read_string() with an existing object. |
You can't call read() or read_string() on an existing object. Not only
does this prevent you from calling read() multiple times, but it also
means you can't do something use Tie::IxHash to make the CSS rules
ordered (see https://rt.cpan.org/Ticket/Display.html?id=59037)
The attached patches fix this and add tests for the fix.
Subject: | t-02_main-repeated-read.patch |
Index: t/02_main.t
===================================================================
--- t/02_main.t (revision 12548)
+++ t/02_main.t (working copy)
@@ -8,7 +8,7 @@
$^W = 1;
}
-use Test::More tests => 22;
+use Test::More tests => 25;
use CSS::Tiny;
@@ -88,7 +88,24 @@
# Check the structure of what we read back in
is_deeply( $Trivial, $Read, 'We get back what we wrote out' );
-
+# try creating an object and then reading multiple times mixed with manual manipulation
+my $Mixed = CSS::Tiny->new();
+isa_ok( $Config, 'CSS::Tiny' );
+$Mixed->read( 'test.css' );
+my $mixed_expected = {
+ H1 => { color => 'blue' },
+ H2 => { color => 'red', 'font-height' => '16px' },
+ 'P EM' => { this => 'that' },
+ 'A B' => { foo => 'bar' },
+ 'C D' => { foo => 'bar' },
+ };
+is_deeply( $Mixed, $mixed_expected, '->read for existing object returns expected structure' );
+$Mixed->{H3} = { color => 'yellow' };
+$mixed_expected->{H3} = { color => 'yellow' };
+$Mixed->read_string("H4 { color: gray }\nSPAN { display: none }");
+$mixed_expected->{H4} = { color => 'gray' };
+$mixed_expected->{SPAN} = { display => 'none' };
+is_deeply( $Mixed, $mixed_expected, '->read_string for existing object returns expected structure' );
Subject: | css-tiny-repeated-read.patch |
Index: lib/CSS/Tiny.pm
===================================================================
--- lib/CSS/Tiny.pm (revision 12548)
+++ lib/CSS/Tiny.pm (working copy)
@@ -138,21 +138,21 @@
=cut
sub read {
- my $class = shift;
+ my $self = shift;
# Check the file
- my $file = shift or return $class->_error( 'You did not specify a file name' );
- return $class->_error( "The file '$file' does not exist" ) unless -e $file;
- return $class->_error( "'$file' is a directory, not a file" ) unless -f _;
- return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _;
+ my $file = shift or return $self->_error( 'You did not specify a file name' );
+ return $self->_error( "The file '$file' does not exist" ) unless -e $file;
+ return $self->_error( "'$file' is a directory, not a file" ) unless -f _;
+ return $self->_error( "Insufficient permissions to read '$file'" ) unless -r _;
# Read the file
local $/ = undef;
- open( CSS, $file ) or return $class->_error( "Failed to open file '$file': $!" );
+ open( CSS, $file ) or return $self->_error( "Failed to open file '$file': $!" );
my $contents = <CSS>;
close( CSS );
- $class->read_string( $contents )
+ $self->read_string( $contents )
}
=pod
@@ -166,7 +166,8 @@
=cut
sub read_string {
- my $self = bless {}, shift;
+ my $self = shift;
+ $self = $self->new unless ref $self;
# Flatten whitespace and remove /* comment */ style comments
my $string = shift;