| 1 | package Mo::utils::CSS; | ||
| 2 | |||
| 3 | 2 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Error::Pure qw(err); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Graphics::ColorNames::CSS; | ||
| 9 | use List::Util 1.33 qw(any none); | ||
| 10 | use Mo::utils::Array qw(check_array); | ||
| 11 | use Mo::utils::Number::Utils qw(sub_check_percent); | ||
| 12 | use Readonly; | ||
| 13 | |||
| 14 | Readonly::Array our @EXPORT_OK => qw(check_array_css_color check_css_border | ||
| 15 | check_css_class check_css_color check_css_unit); | ||
| 16 | Readonly::Array our @ABSOLUTE_LENGTHS => qw(cm mm in px pt pc); | ||
| 17 | Readonly::Array our @BORDER_GLOBAL => qw(inherit initial revert revert-layer unset); | ||
| 18 | Readonly::Array our @BORDER_STYLES => qw(none hidden dotted dashed solid double groove ridge inset outset); | ||
| 19 | Readonly::Array our @BORDER_WIDTHS => qw(thin medium thick); | ||
| 20 | Readonly::Array our @RELATIVE_LENGTHS => qw(em ex ch rem vw vh vmin vmax %); | ||
| 21 | Readonly::Array our @COLOR_FUNC => qw(rgb rgba hsl hsla); | ||
| 22 | |||
| 23 | our $VERSION = 0.13; | ||
| 24 | |||
| 25 | sub check_array_css_color { | ||
| 26 | my ($self, $key) = @_; | ||
| 27 | |||
| 28 | if (! exists $self->{$key}) { | ||
| 29 | return; | ||
| 30 | } | ||
| 31 | |||
| 32 | check_array($self, $key); | ||
| 33 | |||
| 34 | foreach my $css_color (@{$self->{$key}}) { | ||
| 35 | _check_color($css_color, $key); | ||
| 36 | } | ||
| 37 | |||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub check_css_border { | ||
| 42 | my ($self, $key) = @_; | ||
| 43 | |||
| 44 | _check_key($self, $key) && return; | ||
| 45 | |||
| 46 | # Global values. | ||
| 47 | if (any { $self->{$key} eq $_ } @BORDER_GLOBAL) { | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | my @parts = split m/\s+/ms, $self->{$key}, 3; | ||
| 52 | if (@parts == 1) { | ||
| 53 | _check_border_style($self->{$key}, $key); | ||
| 54 | } elsif (@parts == 2) { | ||
| 55 | |||
| 56 | # Border style on first place. | ||
| 57 | if (any { $parts[0] eq $_ } @BORDER_STYLES) { | ||
| 58 | _check_color($parts[1], $key, $self->{$key}); | ||
| 59 | |||
| 60 | # Border style on second place. | ||
| 61 | } elsif (any { $parts[1] eq $_ } @BORDER_STYLES) { | ||
| 62 | if (none { $parts[0] eq $_ } @BORDER_WIDTHS) { | ||
| 63 | _check_unit($parts[0], $key, $self->{$key}); | ||
| 64 | } | ||
| 65 | |||
| 66 | } else { | ||
| 67 | err "Parameter '$key' hasn't border style.", | ||
| 68 | 'Value', $self->{$key}, | ||
| 69 | ; | ||
| 70 | } | ||
| 71 | } else { | ||
| 72 | if (none { $parts[0] eq $_ } @BORDER_WIDTHS) { | ||
| 73 | _check_unit($parts[0], $key, $self->{$key}); | ||
| 74 | } | ||
| 75 | _check_border_style($parts[1], $key, $self->{$key}); | ||
| 76 | _check_color($parts[2], $key, $self->{$key}); | ||
| 77 | } | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub check_css_class { | ||
| 83 | my ($self, $key) = @_; | ||
| 84 | |||
| 85 | _check_key($self, $key) && return; | ||
| 86 | |||
| 87 | if ($self->{$key} !~ m/^[a-zA-Z0-9\-_]+$/ms) { | ||
| 88 | err "Parameter '$key' has bad CSS class name.", | ||
| 89 | 'Value', $self->{$key}, | ||
| 90 | ; | ||
| 91 | } elsif ($self->{$key} =~ m/^\d/ms) { | ||
| 92 | err "Parameter '$key' has bad CSS class name (number on begin).", | ||
| 93 | 'Value', $self->{$key}, | ||
| 94 | ; | ||
| 95 | } | ||
| 96 | |||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub check_css_color { | ||
| 101 | my ($self, $key) = @_; | ||
| 102 | |||
| 103 | _check_key($self, $key) && return; | ||
| 104 | |||
| 105 | _check_color($self->{$key}, $key); | ||
| 106 | |||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub check_css_unit { | ||
| 111 | my ($self, $key) = @_; | ||
| 112 | |||
| 113 | _check_key($self, $key) && return; | ||
| 114 | |||
| 115 | _check_unit($self->{$key}, $key); | ||
| 116 | |||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _check_alpha { | ||
| 121 | my ($alpha, $key, $func, $error_value) = @_; | ||
| 122 | |||
| 123 | if ($alpha !~ m/^[\d\.]+$/ms || $alpha > 1) { | ||
| 124 | err "Parameter '$key' has bad $func alpha.", | ||
| 125 | 'Value', $error_value, | ||
| 126 | ; | ||
| 127 | } | ||
| 128 | |||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub _check_border_style { | ||
| 133 | my ($value, $key, $error_value) = @_; | ||
| 134 | |||
| 135 | if (! defined $error_value) { | ||
| 136 | $error_value = $value; | ||
| 137 | } | ||
| 138 | |||
| 139 | if (none { $value eq $_ } @BORDER_STYLES) { | ||
| 140 | err "Parameter '$key' has bad border style.", | ||
| 141 | 'Value', $error_value, | ||
| 142 | ; | ||
| 143 | } | ||
| 144 | |||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _check_color { | ||
| 149 | my ($value, $key, $error_value) = @_; | ||
| 150 | |||
| 151 | if (! defined $error_value) { | ||
| 152 | $error_value = $value; | ||
| 153 | } | ||
| 154 | |||
| 155 | my $funcs = join '|', @COLOR_FUNC; | ||
| 156 | if ($value =~ m/^#(.*)$/ms) { | ||
| 157 | my $rgb = $1; | ||
| 158 | if (length $rgb == 3 || length $rgb == 6 || length $rgb == 8) { | ||
| 159 | if ($rgb !~ m/^[0-9A-Fa-f]+$/ms) { | ||
| 160 | err "Parameter '$key' has bad rgb color (bad hex number).", | ||
| 161 | 'Value', $error_value, | ||
| 162 | ; | ||
| 163 | } | ||
| 164 | } else { | ||
| 165 | err "Parameter '$key' has bad rgb color (bad length).", | ||
| 166 | 'Value', $error_value, | ||
| 167 | ; | ||
| 168 | } | ||
| 169 | } elsif ($value =~ m/^($funcs)\((.*)\)$/ms) { | ||
| 170 | my $func = $1; | ||
| 171 | my $args_string = $2; | ||
| 172 | my @args = split m/\s*,\s*/ms, $args_string; | ||
| 173 | if ($func eq 'rgb') { | ||
| 174 | if (@args != 3) { | ||
| 175 | err "Parameter '$key' has bad rgb color (bad number of arguments).", | ||
| 176 | 'Value', $error_value, | ||
| 177 | ; | ||
| 178 | } | ||
| 179 | _check_colors([@args[0 .. 2]], $key, $func, $error_value); | ||
| 180 | } elsif ($func eq 'rgba') { | ||
| 181 | if (@args != 4) { | ||
| 182 | err "Parameter '$key' has bad rgba color (bad number of arguments).", | ||
| 183 | 'Value', $error_value, | ||
| 184 | ; | ||
| 185 | } | ||
| 186 | _check_colors([@args[0 .. 2]], $key, $func, $error_value); | ||
| 187 | _check_alpha($args[3], $key, $func, $error_value); | ||
| 188 | } elsif ($func eq 'hsl') { | ||
| 189 | if (@args != 3) { | ||
| 190 | err "Parameter '$key' has bad hsl color (bad number of arguments).", | ||
| 191 | 'Value', $error_value, | ||
| 192 | ; | ||
| 193 | } | ||
| 194 | _check_degree($args[0], $key, $func, $error_value); | ||
| 195 | _check_percent([@args[1 .. 2]], $key, $func.' percent', $error_value); | ||
| 196 | # hsla | ||
| 197 | } else { | ||
| 198 | if (@args != 4) { | ||
| 199 | err "Parameter '$key' has bad hsla color (bad number of arguments).", | ||
| 200 | 'Value', $error_value, | ||
| 201 | ; | ||
| 202 | } | ||
| 203 | _check_degree($args[0], $key, $func, $error_value); | ||
| 204 | _check_percent([@args[1 .. 2]], $key, $func.' percent', $error_value); | ||
| 205 | _check_alpha($args[3], $key, $func, $error_value); | ||
| 206 | } | ||
| 207 | } else { | ||
| 208 | if (none { $value eq $_ } keys %{Graphics::ColorNames::CSS->NamesRgbTable}) { | ||
| 209 | err "Parameter '$key' has bad color name.", | ||
| 210 | 'Value', $error_value, | ||
| 211 | ; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | return; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _check_colors { | ||
| 219 | my ($value_ar, $key, $func, $error_value) = @_; | ||
| 220 | |||
| 221 | foreach my $i (@{$value_ar}) { | ||
| 222 | if ($i !~ m/^\d+$/ms || $i > 255) { | ||
| 223 | err "Parameter '$key' has bad $func color (bad number).", | ||
| 224 | 'Value', $error_value, | ||
| 225 | ; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _check_degree { | ||
| 233 | my ($angle, $key, $func, $error_value) = @_; | ||
| 234 | |||
| 235 | if ($angle !~ m/^\d+$/ms || $angle > 360) { | ||
| 236 | err "Parameter '$key' has bad $func degree.", | ||
| 237 | 'Value', $error_value, | ||
| 238 | ; | ||
| 239 | } | ||
| 240 | |||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub _check_key { | ||
| 245 | my ($self, $key) = @_; | ||
| 246 | |||
| 247 | if (! exists $self->{$key} || ! defined $self->{$key}) { | ||
| 248 | return 1; | ||
| 249 | } | ||
| 250 | |||
| 251 | return 0; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _check_percent { | ||
| 255 | my ($value_ar, $key, $func, $error_value) = @_; | ||
| 256 | |||
| 257 | foreach my $i (@{$value_ar}) { | ||
| 258 | sub_check_percent($i, $key, $func, $error_value); | ||
| 259 | } | ||
| 260 | |||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub _check_unit { | ||
| 265 | my ($value, $key, $error_value) = @_; | ||
| 266 | |||
| 267 | if (! defined $error_value) { | ||
| 268 | $error_value = $value; | ||
| 269 | } | ||
| 270 | |||
| 271 | my ($num, $unit) = $value =~ m/^(\d*\.?\d+)([^\d]*)$/ms; | ||
| 272 | if (! $num) { | ||
| 273 | err "Parameter '$key' doesn't contain unit number.", | ||
| 274 | 'Value', $error_value, | ||
| 275 | ; | ||
| 276 | } | ||
| 277 | if (! $unit) { | ||
| 278 | err "Parameter '$key' doesn't contain unit name.", | ||
| 279 | 'Value', $error_value, | ||
| 280 | ; | ||
| 281 | } | ||
| 282 | if (none { $_ eq $unit } (@ABSOLUTE_LENGTHS, @RELATIVE_LENGTHS)) { | ||
| 283 | err "Parameter '$key' contain bad unit.", | ||
| 284 | 'Unit', $unit, | ||
| 285 | 'Value', $error_value, | ||
| 286 | ; | ||
| 287 | } | ||
| 288 | |||
| 289 | return; | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; | ||
| 293 | |||
| 294 | __END__ | ||