Skip to content

Commit

Permalink
Removes named capture groups from regular expressions
Browse files Browse the repository at this point in the history
This feature needed perl 5.10 and was not really used.
Replaced with normal capture groups and two helpers
(last_match and last_index) to query match result.
  • Loading branch information
mgreter committed Aug 19, 2014
1 parent 3629a2e commit e1abe04
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"provides" : {
"OCBNET::CSS3" : {
"file" : "lib/OCBNET/CSS3.pm",
"version" : "v0.2.6"
"version" : "v0.2.7"
},
"OCBNET::CSS3::DOM::Block" : {
"file" : "lib/OCBNET/CSS3/DOM/Block.pm",
Expand Down Expand Up @@ -114,7 +114,7 @@
},
"OCBNET::CSS3::Regex::Base" : {
"file" : "lib/OCBNET/CSS3/Regex/Base.pm",
"version" : "v0.2.5"
"version" : "v0.2.7"
},
"OCBNET::CSS3::Regex::Colors" : {
"file" : "lib/OCBNET/CSS3/Regex/Colors.pm",
Expand Down Expand Up @@ -176,5 +176,5 @@
"web" : "https://github.com/mgreter/OCBNET-CSS3"
}
},
"version" : "v0.2.6"
"version" : "v0.2.7"
}
6 changes: 3 additions & 3 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name: OCBNET-CSS3
provides:
OCBNET::CSS3:
file: lib/OCBNET/CSS3.pm
version: v0.2.6
version: v0.2.7
OCBNET::CSS3::DOM::Block:
file: lib/OCBNET/CSS3/DOM/Block.pm
version: v0.2.5
Expand Down Expand Up @@ -77,7 +77,7 @@ provides:
version: v0.2.5
OCBNET::CSS3::Regex::Base:
file: lib/OCBNET/CSS3/Regex/Base.pm
version: v0.2.5
version: v0.2.7
OCBNET::CSS3::Regex::Colors:
file: lib/OCBNET/CSS3/Regex/Colors.pm
version: v0.2.6
Expand Down Expand Up @@ -119,4 +119,4 @@ requires:
resources:
license: http://www.gnu.org/licenses/gpl-3.0.txt
repository: https://github.com/mgreter/OCBNET-CSS3.git
version: v0.2.6
version: v0.2.7
2 changes: 1 addition & 1 deletion lib/OCBNET/CSS3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
####################################################################################################
package OCBNET::CSS3;
####################################################################################################
our $VERSION = '0.2.6';
our $VERSION = '0.2.7';
####################################################################################################

use strict;
Expand Down
73 changes: 54 additions & 19 deletions lib/OCBNET/CSS3/Regex/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
####################################################################################################
package OCBNET::CSS3::Regex::Base;
####################################################################################################
our $VERSION = '0.2.5';
our $VERSION = '0.2.7';
####################################################################################################

use strict;
use warnings;
our @EXPORT;
our @EXPORT_OK;

####################################################################################################

# load exporter and inherit from it
BEGIN { use Exporter qw(); our @ISA = qw(Exporter); }

# define our functions that can be exported
BEGIN { our @EXPORT_OK = qw($re_url $re_uri $re_import unwrapUrl wrapUrl); }
use Exporter qw(); our @ISA = qw(Exporter);

# define our functions that will be exported
BEGIN { our @EXPORT = qw($re_apo $re_quot $re_identifier $re_string $re_vendors unquot); }
push @EXPORT, qw($re_apo $re_quot $re_identifier $re_string);
push @EXPORT_OK, qw($re_uri $re_import last_match last_index);
push @EXPORT_OK, qw($re_vendors $re_url unquot unwrapUrl wrapUrl);

####################################################################################################
# base regular expressions
Expand All @@ -38,40 +39,74 @@ our $re_identifier = qr/\b[_a-zA-Z][_a-zA-Z0-9\-]*/s;
#**************************************************************************************************
our $re_string = qr/(?:$re_identifier|\"$re_quot\"|\'$re_apo\')/is;

# regular expression to match any url
# regular expression to match a wrapped url
#**************************************************************************************************
our $re_url = qr/url\((?:\'$re_apo\'|\"$re_quot\"|[^\)]*)\)/s;

# match specific vendors
# match vendors prefixes
#**************************************************************************************************
our $re_vendors = qr/(?:o|ms|moz|webkit)/is;

####################################################################################################

# parse urls out of the css file
# match will be saved as $+{uri}
# only supports wrapped urls
our $re_uri = qr/url\(\s*(?:
\s*\"(?!data:)(?<uri>$re_quot)\" |
\s*\'(?!data:)(?<uri>$re_apo)\' |
(?![\"\'])\s*(?!data:)(?<uri>[^\)]*)
\s*\"(?!data:)($re_quot)\" |
\s*\'(?!data:)($re_apo)\' |
(?![\"\'])\s*(?!data:)([^\)]*)
)\s*\)/xi;

####################################################################################################

# parse urls out of the css file
# match will be saved as $+{uri}
# also supports not wrapped urls
our $re_import = qr/\@import\s*(?:
url\(\s*(?:
\s*\"(?!data:)(?<url>$re_quot)\" |
\s*\'(?!data:)(?<url>$re_apo)\' |
(?![\"\'])\s*(?!data:)(?<url>[^\)]*)
\s*\"(?!data:)($re_quot)\" |
\s*\'(?!data:)($re_apo)\' |
(?![\"\'])\s*(?!data:)([^\)]*)
)\) | (?:
\s*\"(?!data:)(?<uri>$re_quot)\" |
\s*\'(?!data:)(?<uri>$re_apo)\' |
(?![\"\'])\s*(?!data:)(?<uri>[^\s;]*)
\s*\"(?!data:)($re_quot)\" |
\s*\'(?!data:)($re_apo)\' |
(?![\"\'])\s*(?!data:)([^\s;]*)
))
\s*;?/xi;

####################################################################################################
# regular expressions helpers
####################################################################################################

# return first defined match of last expression
# helper for expressions that match alternatives
sub last_match ()
{
if (defined $1) { $1 }
elsif (defined $2) { $2 }
elsif (defined $3) { $3 }
elsif (defined $4) { $4 }
elsif (defined $5) { $5 }
elsif (defined $6) { $6 }
elsif (defined $7) { $7 }
elsif (defined $8) { $8 }
elsif (defined $9) { $9 }
}

# return index of first defined match of last expression
# can be used to differentiate between match alternatives
sub last_index ()
{
if (defined $1) { 1 }
elsif (defined $2) { 2 }
elsif (defined $3) { 3 }
elsif (defined $4) { 4 }
elsif (defined $5) { 5 }
elsif (defined $6) { 6 }
elsif (defined $7) { 7 }
elsif (defined $8) { 8 }
elsif (defined $9) { 9 }
}

####################################################################################################

# a very plain and simply unquote function
Expand Down
14 changes: 12 additions & 2 deletions t/02_regex.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use utf8;
use strict;
use warnings;

use Test::More tests => 41;
use Test::More tests => 50;
BEGIN { use_ok('OCBNET::CSS3::Regex::Base') };
BEGIN { use_ok('OCBNET::CSS3::Regex::Numbers') };
BEGIN { use_ok('OCBNET::CSS3::Regex::Background') };

use OCBNET::CSS3::Regex::Base qw(unquot unwrapUrl wrapUrl);
use OCBNET::CSS3::Regex::Base qw(unwrapUrl wrapUrl $re_uri);

is (unwrapUrl('url("test.png")'), 'test.png', 'unwrapUrl test #1');
is (unwrapUrl('url(url("test.png"))'), 'test.png', 'unwrapUrl test #2');
Expand Down Expand Up @@ -67,4 +67,14 @@ is (fromPosition('top'), '0', 'fromPosition test #5
is (fromPosition('bottom'), 'bottom', 'fromPosition test #6');
is (fromPosition('right'), 'right', 'fromPosition test #7');

use OCBNET::CSS3::Regex::Base qw(unquot last_match last_index);

is ('url("test")' =~ $re_uri, 1, 'match uri test #1');
is (last_match, 'test', 'test last match #1');
is (last_index, 1, 'test last match #1');
is ("url('test')" =~ $re_uri, 1, 'match uri test #2');
is (last_match, 'test', 'test last match #2');
is (last_index, 2, 'test last match #2');
is ('url(test)' =~ $re_uri, 1, 'match uri test #3');
is (last_match, 'test', 'test last match #3');
is (last_index, 3, 'test last match #3');

0 comments on commit e1abe04

Please sign in to comment.