Skip to content
Snippets Groups Projects
Commit e7ae4a4d authored by Apertis package maintainers's avatar Apertis package maintainers
Browse files

Import Upstream version 1.20

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #499557 skipped
Changes 0 → 100644
Revision history for Perl extension CSS-Tiny
1.20 2016-03-10 by Alexandr Ciornii
- Require newer perl
- RT#87261: Support properties that start with asterisk (MADDAVE)
1.19 Fri 3 Sep 2010
- No functional changes
- Resolved RT #59037: CSS::Tiny reorders rules
- The use of Tie::IxHash to achieve ordering is now documented
1.18 Fri 3 Sep 2010
- Resolved RT #60776: Parsing of multiple whitespace selectors
- Added test script specifically for RT regression tests
1.17 Sun 4 Jul 2010
- Removed the use of Module::Install
- Allow read-string to be rerun again on an existing object,
and thus also thus on tied objects (MPETERS)
- Moved POD after __END__ to reduce number of disk reads when parsing.
- Added support for running xt tests
1.16 Fri 4 Jul 2008
- Upgrading to Module::Install 0.76
- Localising $@ during evals
1.15 Thu 1 Nov 2007
- Moving from Module::Install to the tinier EU:MM
- Changed use 5.005 to BEGIN { require 5.004 }
1.14 Mon 4 Sep 2006
- Upgrading to Module::Install 0.64
1.12 Thu 9 Jun 2006
- Moved from old CVS to new SVN repository
- Removed uses of UNIVERSAL::isa
- Upgraded to Module::Install 0.63
1.11 Sat Oct 22 2005
- Fixed a bug in the inline ->clone method that only runs
when Clone is unavailable.
- Dialed back the File::Spec dep a little to prevent 5.6 people
needing to upgraded when not needed.
1.10 Thu May 26 2005
- Added the ->clone method
1.09 Thu Mar 24 2005
- Lars Thegler noted that 3-argument open is not supported by 5.005.
Added a small fix to change it to 2-argument open.
1.08 Fri Feb 18 2005
- Adding XHTML support with the ->xhtml method
- Cleaning up the POD a little
1.07 Tue Feb 15 2005
- Removing braindead Build.PL
1.06 Sun Jan 16 2005
- Added ->html to generate CSS to dump on a HTML page
- Modernised the test suite and makefile
1.05 Wed Jul 14 2004
- Switched from open( FILE, ">filename" ) to open( FILE, '>', 'filename' )
- Using the -X _ method to file-test via cached stat information
- Added support for merging differently named properties in multiply listed styles
- Upgraded test script to hopefully work on Win32 now
1.04 Wed Jan 7 2004
- Changed property parser to no longer require exactly one space
after the colon seperate. Now allows zero or more spaces.
1.02 Wed Aug 13 16:12:51 2003
- Applied a series of small changes to reduce the code overhead
by a K or so, without changing function
1.01 Mon Jul 15 00:28:12 2003
- Changed write_string to sort in reverse order. This fixes
a problem with A:hover described at the url
http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
Fix supplied by Blaise Barrelet
1.0 Sat Dec 21 12:31:51 2002
- Removed locking code, and reliance on Fcntl
- Removed support for write modes
- Tightened up the code
- Added the read_string() method
- Simplified the file generator to save ops
0.3 Mon Dec 09 00:27:01 2002
- Upgraded tests to use Test::More and compare structs
0.2 Thu Nov 28 16:53:21 2002
- Regex to remove C style comments was broken
Fix supplied by Bjorn Svensson.
0.1 Tue Nov 26 20:53:23 2002
- original version
MANIFEST 0 → 100644
Changes
lib/CSS/Tiny.pm
Makefile.PL
MANIFEST This list of files
t/01_compile.t
t/02_main.t
t/03_inline_clone.t
t/04_rt.t
t/05_asterisk.t
test.css
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
{
"abstract" : "Read/Write .css files with as little code as possible",
"author" : [
"Adam Kennedy <adamk@cpan.org>"
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 6.78, CPAN::Meta::Converter version 2.150005",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "CSS-Tiny",
"no_index" : {
"directory" : [
"t",
"inc"
]
},
"prereqs" : {
"build" : {
"requires" : {
"Test::More" : "0.47"
}
},
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {
"perl" : "5.005"
}
}
},
"release_status" : "stable",
"resources" : {
"repository" : {
"url" : "https://github.com/chorny/CSS-Tiny"
}
},
"version" : "1.20",
"x_serialization_backend" : "JSON version 2.90"
}
META.yml 0 → 100644
---
abstract: Read/Write .css files with as little code as possible
author:
- 'Adam Kennedy <adamk@cpan.org>'
build_requires:
Test::More: 0.47
configure_requires:
ExtUtils::MakeMaker: 0
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 6.78, CPAN::Meta::Converter version 2.150005'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: CSS-Tiny
no_index:
directory:
- t
- inc
requires:
perl: 5.005
resources:
repository: https://github.com/chorny/CSS-Tiny
version: 1.20
x_serialization_backend: YAML version 0.73
use strict;
use vars qw{$VERSION};
BEGIN {
require 5.005;
}
use ExtUtils::MakeMaker;
WriteMakefile(
MIN_PERL_VERSION => '5.005',
META_MERGE => {
resources => {
repository => 'https://github.com/chorny/CSS-Tiny',
},
},
NAME => 'CSS::Tiny',
ABSTRACT_FROM => 'lib/CSS/Tiny.pm',
VERSION_FROM => 'lib/CSS/Tiny.pm',
PREREQ_PM => {
},
BUILD_REQUIRES => {
'Test::More' => '0.47',
},
AUTHOR => 'Adam Kennedy <adamk@cpan.org>',
LICENSE => 'perl',
(-d 'xt' and $ENV{AUTOMATED_TESTING} || $ENV{RELEASE_TESTING}) ? (
test => {
TESTS => 't/*.t xt/*.t',
},
) : (),
$^O =~/win/i ? (
dist => {
TAR => 'ptar',
TARFLAGS => '-c -C -f',
},
) : (),
);
sub WriteMakefile { #Written by Alexandr Ciornii, version 0.21. Added by eumm-upgrade.
my %params=@_;
my $eumm_version=$ExtUtils::MakeMaker::VERSION;
$eumm_version=eval $eumm_version;
if ( $params{BUILD_REQUIRES} and $eumm_version < 6.5503 ) {
#EUMM 6.5502 has problems with BUILD_REQUIRES
$params{PREREQ_PM} = {
%{$params{PREREQ_PM} || {}},
%{$params{BUILD_REQUIRES}}
};
delete $params{BUILD_REQUIRES};
}
delete $params{MIN_PERL_VERSION} if $eumm_version < 6.48;
delete $params{META_MERGE} if $eumm_version < 6.46;
delete $params{LICENSE} if $eumm_version < 6.31;
delete $params{AUTHOR} if $] < 5.005;
ExtUtils::MakeMaker::WriteMakefile(%params);
}
package CSS::Tiny;
# See POD at end for docs
use strict;
BEGIN {
require 5.005;
$CSS::Tiny::VERSION = '1.20';
$CSS::Tiny::errstr = '';
}
# Create an empty object
sub new { bless {}, shift }
# Create an object from a file
sub read {
my $class = 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 _;
# Read the file
local $/ = undef;
open( CSS, $file ) or return $class->_error( "Failed to open file '$file': $!" );
my $contents = <CSS>;
close( CSS );
$class->read_string( $contents )
}
# Create an object from a string
sub read_string {
my $self = ref $_[0] ? shift : bless {}, shift;
# Flatten whitespace and remove /* comment */ style comments
my $string = shift;
$string =~ tr/\n\t/ /;
$string =~ s!/\*.*?\*\/!!g;
# Split into styles
foreach ( grep { /\S/ } split /(?<=\})/, $string ) {
unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) {
return $self->_error( "Invalid or unexpected style data '$_'" );
}
# Split in such a way as to support grouped styles
my $style = $1;
my $properties = $2;
$style =~ s/\s{2,}/ /g;
my @styles = grep { s/\s+/ /g; 1; } grep { /\S/ } split /\s*,\s*/, $style;
foreach ( @styles ) { $self->{$_} ||= {} }
# Split into properties
foreach ( grep { /\S/ } split /\;/, $properties ) {
unless ( /^\s*(\*?[\w._-]+)\s*:\s*(.*?)\s*$/ ) {
return $self->_error( "Invalid or unexpected property '$_' in style '$style'" );
}
foreach ( @styles ) { $self->{$_}->{lc $1} = $2 }
}
}
$self
}
# Copy an object, using Clone.pm if available
BEGIN { local $@; eval "use Clone 'clone';"; eval <<'END_PERL' if $@; }
sub clone {
my $self = shift;
my $copy = ref($self)->new;
foreach my $key ( keys %$self ) {
my $section = $self->{$key};
$copy->{$key} = {};
foreach ( keys %$section ) {
$copy->{$key}->{$_} = $section->{$_};
}
}
$copy;
}
END_PERL
# Save an object to a file
sub write {
my $self = shift;
my $file = shift or return $self->_error( 'No file name provided' );
# Write the file
open( CSS, '>'. $file ) or return $self->_error( "Failed to open file '$file' for writing: $!" );
print CSS $self->write_string;
close( CSS );
}
# Save an object to a string
sub write_string {
my $self = shift;
# Iterate over the styles
# Note: We use 'reverse' in the sort to avoid a special case related
# to A:hover even though the file ends up backwards and looks funny.
# See http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
my $contents = '';
foreach my $style ( reverse sort keys %$self ) {
$contents .= "$style {\n";
foreach ( sort keys %{ $self->{$style} } ) {
$contents .= "\t" . lc($_) . ": $self->{$style}->{$_};\n";
}
$contents .= "}\n";
}
return $contents;
}
# Generate a HTML fragment for the CSS
sub html {
my $css = $_[0]->write_string or return '';
return "<style type=\"text/css\">\n<!--\n${css}-->\n</style>";
}
# Generate an xhtml fragment for the CSS
sub xhtml {
my $css = $_[0]->write_string or return '';
return "<style type=\"text/css\">\n/* <![CDATA[ */\n${css}/* ]]> */\n</style>";
}
# Error handling
sub errstr { $CSS::Tiny::errstr }
sub _error { $CSS::Tiny::errstr = $_[1]; undef }
1;
__END__
=pod
=head1 NAME
CSS::Tiny - Read/Write .css files with as little code as possible
=head1 SYNOPSIS
# In your .css file
H1 { color: blue }
H2 { color: red; font-family: Arial }
.this, .that { color: yellow }
# In your program
use CSS::Tiny;
# Create a CSS stylesheet
my $CSS = CSS::Tiny->new();
# Open a CSS stylesheet
$CSS = CSS::Tiny->read( 'style.css' );
# Reading properties
my $header_color = $CSS->{H1}->{color};
my $header2_hashref = $CSS->{H2};
my $this_color = $CSS->{'.this'}->{color};
my $that_color = $CSS->{'.that'}->{color};
# Changing styles and properties
$CSS->{'.newstyle'} = { color => '#FFFFFF' }; # Add a style
$CSS->{H1}->{color} = 'black'; # Change a property
delete $CSS->{H2}; # Delete a style
# Save a CSS stylesheet
$CSS->write( 'style.css' );
# Get the CSS as a <style>...</style> tag
$CSS->html;
=head1 DESCRIPTION
C<CSS::Tiny> is a perl class to read and write .css stylesheets with as
little code as possible, reducing load time and memory overhead. CSS.pm
requires about 2.6 meg or ram to load, which is a large amount of
overhead if you only want to do trivial things.
Memory usage is normally scoffed at in Perl, but in my opinion should be
at least kept in mind.
This module is primarily for reading and writing simple files, and anything
we write shouldn't need to have documentation/comments. If you need
something with more power, move up to CSS.pm. With the increasing complexity
of CSS, this is becoming more common, but many situations can still live
with simple CSS files.
=head2 CSS Feature Support
C<CSS::Tiny> supports grouped styles of the form
C<this, that { color: blue }> correctly when reading, ungrouping them into
the hash structure. However, it will not restore the grouping should you
write the file back out. In this case, an entry in the original file of
the form
H1, H2 { color: blue }
would become
H1 { color: blue }
H2 { color: blue }
C<CSS::Tiny> handles nested styles of the form C<P EM { color: red }>
in reads and writes correctly, making the property available in the
form
$CSS->{'P EM'}->{color}
C<CSS::Tiny> ignores comments of the form C</* comment */> on read
correctly, however these comments will not be written back out to the
file.
=head1 CSS FILE SYNTAX
Files are written in a relatively human-orientated form, as follows:
H1 {
color: blue;
}
.this {
color: red;
font-size: 10px;
}
P EM {
color: yellow;
}
When reading and writing, all property descriptors, for example C<color>
and C<font-size> in the example above, are converted to lower case. As an
example, take the following CSS.
P {
Font-Family: Verdana;
}
To get the value C<'Verdana'> from the object C<$CSS>, you should
reference the key C<$CSS-E<gt>{P}-E<gt>{font-family}>.
=head1 METHODS
=head2 new
The constructor C<new> creates and returns an empty C<CSS::Tiny> object.
=head2 read $filename
The C<read> constructor reads a CSS stylesheet, and returns a new
C<CSS::Tiny> object containing the properties in the file.
Returns the object on success, or C<undef> on error.
=head2 read_string $string
The C<read_string> constructor reads a CSS stylesheet from a string.
Returns the object on success, or C<undef> on error.
=head2 clone
The C<clone> method creates an identical copy of an existing C<CSS::Tiny>
object.
=head2 write_string
Generates the stylesheet for the object and returns it as a string.
=head2 write
The C<write $filename> generates the stylesheet for the properties, and
writes it to disk. Returns true on success. Returns C<undef> on error.
=head2 html
The C<html> method generates the CSS, but wrapped in a C<style> HTML tag,
so that it can be dropped directly onto a HTML page.
=head2 xhtml
The C<html> method generates the CSS, but wrapped in a C<style> XHTML tag,
so that it can be dropped directly onto an XHTML page.
=head2 errstr
When an error occurs, you can retrieve the error message either from the
C<$CSS::Tiny::errstr> variable, or using the C<errstr> method.
=head1 CAVEATS
=head2 CSS Rule Order
While the order of rules in CSS is important, this is one of the features
that is sacrificed to keep things small and dependency-free. If you need
to preserve order yourself, we recommend that you upgrade to the more
powerful L<CSS> module.
If this is not possible in your case, alternatively it can be done with the
help of another module such as L<Tie::IxHash>:
my $css = CSS::Tiny->new;
tie %$css, 'Tie::IxHash';
$css->read('style.css');
Note: You will also need to remember to add the additional dependency to
your code or module in this case.
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CSS-Tiny>
For other issues, or commercial enhancement or support, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 SEE ALSO
L<CSS>, L<http://www.w3.org/TR/REC-CSS1>, L<Config::Tiny>, L<http://ali.as/>
=head1 COPYRIGHT
Copyright 2002 - 2010 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
#!/usr/bin/perl
# Formal testing for CSS::Tiny
# This test only tests that the module compiles.
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 2;
# Check their Perl version
ok( $] >= 5.004, "Your perl is new enough" );
# Does the module load
use_ok( 'CSS::Tiny' );
#!/usr/bin/perl
# Formal testing for CSS::Tiny
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 23;
use CSS::Tiny;
# Test trivial creation
my $trivial = CSS::Tiny->new;
isa_ok( $trivial, 'CSS::Tiny' );
is( scalar(keys %$trivial), 0, '->new returns an empty object' );
# Try to read in a config
my $css = CSS::Tiny->read( 'test.css' );
isa_ok( $css, 'CSS::Tiny' );
# Check the structure of the config
my $expected = {
H1 => { color => 'blue' },
H2 => { color => 'red', 'font-height' => '16px' },
'P EM' => { this => 'that' },
'A B' => { foo => 'bar' },
'C D' => { foo => 'bar' },
};
bless $expected, 'CSS::Tiny';
is_deeply( $css, $expected, '->read returns expected structure' );
# Test clone
my $copy = $css->clone;
isa_ok( $copy, 'CSS::Tiny' );
is_deeply( $copy, $css, '->clone works as expected' );
# Add some stuff to the trivial stylesheet and check write_string() for it
$trivial->{H1} = { color => 'blue' };
$trivial->{'.this'} = {
color => '#FFFFFF',
'font-family' => 'Arial, "Courier New"',
'font-variant' => 'small-caps',
};
$trivial->{'P EM'} = { color => 'red' };
my $string = <<END;
P EM {
color: red;
}
H1 {
color: blue;
}
.this {
color: #FFFFFF;
font-family: Arial, "Courier New";
font-variant: small-caps;
}
END
my $read = CSS::Tiny->read_string( $string );
ok( $read, '>read_string() returns true' );
is_deeply( $read, $trivial, '->read_string() returns expected' );
my $read2 = CSS::Tiny->new;
$read2->read_string($string);
is_deeply( $read2, $trivial, 'object->read_string() returns expected' );
my $generated = $trivial->write_string();
ok( length $generated, '->write_string returns something' );
ok( $generated eq $string, '->write_string returns the correct file contents' );
# Try to write a file
my $rv = $trivial->write( 'test2.css' );
ok( $rv, '->write returned true' );
ok( -e 'test2.css', '->write actually created a file' );
# Clean up on unload
END {
unlink 'test2.css';
}
# Try to read the config back in
$read = CSS::Tiny->read( 'test2.css' );
isa_ok( $read, 'CSS::Tiny' );
# Check the structure of what we read back in
is_deeply( $trivial, $read, 'We get back what we wrote out' );
#####################################################################
# Check that two identical named styles overwrite-by-property, rather than
# replace-by-style, so that styles are relatively correctly merged.
my $mergable = <<'END_CSS';
FOO { test1: 1; }
FOO { test2: 2; }
END_CSS
my $merged = CSS::Tiny->read_string( $mergable );
ok( $merged, "CSS::Tiny reads mergable CSS ok" );
is_deeply( $merged, { FOO => { test1 => 1, test2 => 2 } }, "Mergable CSS merges ok" );
#####################################################################
# Check the HTML generation
my $html = CSS::Tiny->new();
isa_ok( $html, 'CSS::Tiny' );
is( $html->html, '', '->html returns empty string for empty stylesheet' );
$html->{'.foo'}->{bar} = 1;
is( $html->html . "\n", <<'END_HTML', '->html returns correct looking HTML' );
<style type="text/css">
<!--
.foo {
bar: 1;
}
-->
</style>
END_HTML
#####################################################################
# Check the XHTML generation
my $xhtml = CSS::Tiny->new;
isa_ok( $xhtml, 'CSS::Tiny' );
is( $xhtml->xhtml, '', '->xhtml returns empty string for empty stylesheet' );
$xhtml->{'.foo'}->{bar} = 1;
is( $html->xhtml . "\n", <<'END_XHTML', '->xhtml returns correct looking HTML' );
<style type="text/css">
/* <![CDATA[ */
.foo {
bar: 1;
}
/* ]]> */
</style>
END_XHTML
#!/usr/bin/perl
# Formal testing for CSS::Tiny
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 7;
my $new_called = 0;
# This won't work if a testing module uses Clone
SKIP: {
skip( "Clone unexpectedly already in use", 13 ) if $INC{'Clone.pm'};
# Create the broken fake Clone
$INC{'Clone.pm'} = "FAKED";
$Clone::VERSION = '10';
*Clone::import = sub {
die "Clone is busted";
};
# Get rid of some spurious warnings
*main::foo = *Clone::import;
# Loads ok when Clone busted?
use_ok('CSS::Tiny');
# Replace the new sub with one that signals it was called
sub mynew {
$new_called = 1;
return bless {}, shift;
}
*CSS::Tiny::new = *mynew;
*main::foo = *CSS::Tiny::new;
# Retry some tests to make sure the fake new works the same
# Test trivial creation
my $Trivial = CSS::Tiny->new();
isa_ok( $Trivial, 'CSS::Tiny' );
ok( scalar keys %$Trivial == 0, '->new returns an empty object' );
# Try to read in a config
my $Config = CSS::Tiny->read( 'test.css' );
isa_ok( $Config, 'CSS::Tiny' );
# Repeat the clone tests from 02_main.t
$new_called = 0;
my $copy = $Config->clone;
isa_ok( $copy, 'CSS::Tiny' );
is_deeply( $copy, $Config, '->clone works as expected' );
is( $new_called, 1, 'The inline ->clone was used as expected' );
}
#!/usr/bin/perl
# This test holds regression tests based on samples provided in various
# rt.cpan.org bug reports.
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 2;
use CSS::Tiny;
######################################################################
# Bug #60776 for CSS-Tiny: Bug in selector parsing in CSS::Tiny
# Test parsing of CSS selectors with multiple whitespace elements
my $css = CSS::Tiny->read_string( <<'END_CSS' );
.test .bodySettings {
font-size: 12px;
}
END_CSS
isa_ok( $css, 'CSS::Tiny' );
is_deeply(
[ %$css ],
[
'.test .bodySettings' => {
'font-size' => '12px',
},
],
'Bug 60776: Parsing with multiple whitespace',
);
#!/usr/bin/perl
# This test holds regression tests based on samples provided in various
# rt.cpan.org bug reports.
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 2;
use CSS::Tiny;
######################################################################
# Bug #87261 for CSS-Tiny: Bug with properties starting with asterisk
# Test parsing of CSS selectors with multiple whitespace elements
my $css = CSS::Tiny->read_string( <<'END_CSS' );
.mycls {
*display: inline;
*zoom: 1;
}
END_CSS
if (!defined $css) {
BAIL_OUT('CSS:Tiny object not created');
}
isa_ok( $css, 'CSS::Tiny' );
is_deeply(
[ %$css ],
[
'.mycls' => {
'*display' => 'inline',
'*zoom' => 1,
},
],
'Bug 87261: Parsing properties starting with asterisk',
);
H1 { color:blue }
H2 { color: red; font-height: 16px }
P EM { this: that }
A B, C D { Foo: bar }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment