| 1 | package CSS::DOM::Util; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 27 | $VERSION = '0.16'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use strict; use warnings; no warnings qw 'utf8 parenthesis'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 27 | ▶ | use Exporter 5.57 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 27 | our @EXPORT_OK = qw ' | |||||||||||||||||||||||||||||||
| 9 | unescape escape | ||||||||||||||||||||||||||||||||
| 10 | escape_ident | ||||||||||||||||||||||||||||||||
| 11 | unescape_url | ||||||||||||||||||||||||||||||||
| 12 | unescape_str escape_str'; | ||||||||||||||||||||||||||||||||
| 13 | 27 | our %EXPORT_TAGS = (all=>\@EXPORT_OK); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | sub escape($$) { | ||||||||||||||||||||||||||||||||
| 17 | 117 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
escape: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 117 | my $hex_or_space = qr/[0-9a-fA-F]|(?!$_[0])[ \t]/; | |||||||||||||||||||||||||||||||
| 19 | 117 | $str =~ s/([\n\r\f]|$_[0])/ | |||||||||||||||||||||||||||||||
| 20 | 86 | my $c = $1; | |||||||||||||||||||||||||||||||
| 21 | 86 | ▶ | $c =~ m'[ -\/:-@[-`{-~]' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
ord $c < 1048576 && (substr($str, $+[0], 1) || 'a') =~ /$hex_or_space/
substr($str, $+[0], 1) || 'a'
MC/DC100%2/2
ord $c < 1048576 && (substr($str, $+[0], 1) || 'a') =~ /$hex_or_space/
ord $c < 1048576
substr($str, $+[0], 1)
Truth table100%3/3
A && B
A= ord $c < 1048576B= substr($str, $+[0], 1)
| |||||||||||||||||||||||||||||||||
| 22 | ? "\\$c" | ||||||||||||||||||||||||||||||||
| 23 | : sprintf '\%x' . ' ' x ( | ||||||||||||||||||||||||||||||||
| 24 | ord $c < 0x100000 && | ||||||||||||||||||||||||||||||||
| 25 | (substr $str, $+[0], 1,||'a') =~ $hex_or_space | ||||||||||||||||||||||||||||||||
| 26 | ), ord $c | ||||||||||||||||||||||||||||||||
| 27 | /ge; | ||||||||||||||||||||||||||||||||
| 28 | 117 | $str; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | sub unescape($) { | ||||||||||||||||||||||||||||||||
| 32 | 3069 | ▶ | my $val = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
unescape: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | 3069 | $val =~ s/\\(?: | |||||||||||||||||||||||||||||||
| 34 | ([a-fA-F0-9]{1,6})(?:\r\n|[ \n\r\t\f])? | ||||||||||||||||||||||||||||||||
| 35 | | | ||||||||||||||||||||||||||||||||
| 36 | ([^\n\r\f0-9a-f]) | ||||||||||||||||||||||||||||||||
| 37 | | | ||||||||||||||||||||||||||||||||
| 38 | (\r\n?|[\n\f]) | ||||||||||||||||||||||||||||||||
| 39 | )/ | ||||||||||||||||||||||||||||||||
| 40 | 4203 | ▶ | defined $1 ? chr hex $1 : | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 41 | defined $2 ? $2 : | ||||||||||||||||||||||||||||||||
| 42 | '' | ||||||||||||||||||||||||||||||||
| 43 | /gex; | ||||||||||||||||||||||||||||||||
| 44 | 3069 | $val; | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | sub escape_ident($) { | ||||||||||||||||||||||||||||||||
| 48 | 101 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
escape_ident: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # An identifier can’t have [0-9] for the first character, or for | ||||||||||||||||||||||||||||||||
| 51 | # the second if | ||||||||||||||||||||||||||||||||
| 52 | # the first is [-]. | ||||||||||||||||||||||||||||||||
| 53 | 101 | return escape $str, | |||||||||||||||||||||||||||||||
| 54 | qr/([\0-,.\/:-\@[-^`{-\177]|^[0-9]|(?<=^-)[0-9])/; | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | sub unescape_url($) { | ||||||||||||||||||||||||||||||||
| 58 | 43 | ▶ | my $token = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
unescape_url: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 59 | 43 | $token =~ s/^url\([ \t\r\n\f]*//; | |||||||||||||||||||||||||||||||
| 60 | 43 | $token =~ s/[ \t\r\n\f]*\)\z//; | |||||||||||||||||||||||||||||||
| 61 | 43 | ▶ | $token =~ s/^['"]// and chop $token; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 43 | return unescape $token | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | sub escape_str($) { | ||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | "'" . escape($_[0],qr/'/) . "'" | ||||||||||||||||||||||||||||||
|
Subroutine
escape_str: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | sub unescape_str($) { | ||||||||||||||||||||||||||||||||
| 70 | 57 | ▶ | unescape substr $_[0], 1, -1; | ||||||||||||||||||||||||||||||
|
Subroutine
unescape_str: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 27 | **__END__** | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | CSS::DOM::Util - Utility functions for dealing with CSS tokens | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | Version 0.16 | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | use CSS::DOM::Util ':all'; | ||||||||||||||||||||||||||||||||
| 86 | # or: | ||||||||||||||||||||||||||||||||
| 87 | use CSS::DOM::Util qw[ | ||||||||||||||||||||||||||||||||
| 88 | escape unescape | ||||||||||||||||||||||||||||||||
| 89 | escape_ident unescape_url | ||||||||||||||||||||||||||||||||
| 90 | escape_str unescape_str | ||||||||||||||||||||||||||||||||
| 91 | ]; | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | This module provides utility functions for dealing with CSS tokens. | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | All functions below that take one argument have a C<($)> prototype, so they | ||||||||||||||||||||||||||||||||
| 100 | have the same precedence as C<closedir> | ||||||||||||||||||||||||||||||||
| 101 | and C<delete>. | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =over | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =item escape $string, $chars_to_escape | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | This escapes any characters in C<$string> that occur in | ||||||||||||||||||||||||||||||||
| 108 | C<$chars_to_escape>, which is interpreted as a regular expression. The | ||||||||||||||||||||||||||||||||
| 109 | regexp must consume just one character; otherwise you'll find chars | ||||||||||||||||||||||||||||||||
| 110 | missing from the output. ASCII vertical whitespace (except the vertical | ||||||||||||||||||||||||||||||||
| 111 | tab) is always escaped. | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | Printable non-alphanumeric ASCII characters and the space character are | ||||||||||||||||||||||||||||||||
| 114 | escaped with a single | ||||||||||||||||||||||||||||||||
| 115 | backslash. Other characters are encoded in hexadecimal. | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | C<escape> also considers that you might want to include the escaped string | ||||||||||||||||||||||||||||||||
| 118 | in a larger string, so it appends a space if the escaped string ends with a | ||||||||||||||||||||||||||||||||
| 119 | hexadecimal escape with fewer than six digits. | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | =item unescape $string | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | This turns something like \"H\65llo\" into "Hello" (including quotes). | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | =item escape_ident $string | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =item escape_ident $string, $more_chars_to_escape | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | This escapes C<$string> as a CSS identifier, escaping also any characters | ||||||||||||||||||||||||||||||||
| 130 | matched by C<$more_chars_to_escape>. | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | =item unescape_url $url_token | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | Returns the URL that the token represents. | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | =item escape_str $string | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | Returns a CSS string token containing C<$string> (within quotes; characters | ||||||||||||||||||||||||||||||||
| 139 | possibly escaped). | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | =item unescape_str $string_token | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | Returns the value that a CSS string token represents. | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | L<CSS::DOM> | ||||||||||||||||||||||||||||||||