4 use List::Util qw(sum max);
7 title => 'browser compatibility cheat sheet',
11 keywords => [qw'html css browser feature'],
12 stylesheet => [qw'circus dark mono red light'],
13 data => ['browser-support.inc.pl'],
17 <h1>Browser compatibility</h1>
19 <p>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page<:
20 my ($canihas, $usage);
21 given ($get{usage} // 'wm') {
26 printf "<p>Invalid browser usage data request: <em>%s</em>",
27 'identifier must be alphanumeric name or <q>0</q>';
29 $canihas = do "browser-usage-$_.inc.pl" or do {
30 printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
34 my $ref = $canihas->{-source} || 'unknown';
35 $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
36 $ref .= " $_" for $canihas->{-date} || ();
37 print "\nwith $ref browser usage statistics";
44 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
58 unoff => 'l1', # unofficial
60 cr => 'l4', # candidate
61 pr => 'l4', # proposed
62 rec => 'l5', # recommendation
63 other => 'l5', # non-w3
64 ietf => 'l5', # standard
67 if (my ($somerow) = values %{ $caniuse->{data} }) {
68 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
69 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
72 my @browsers = grep { $versions{$_} }
73 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
79 my $zero = $#$_ - 2; # baseline index
80 ($_->[$zero - 2] => .5), # past
81 ($_->[$zero - 1] => 10 ), # previous
82 ($_->[$zero + 2] => 0 ), # future
83 ($_->[$zero + 1] => .5), # next
84 ($_->[$zero ] => 30 ), # current
85 } $caniuse->{agents}->{$_}->{versions}
88 }; # fallback hash based on release semantics
89 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
91 print '<table class="mapped">';
93 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
98 '<th colspan="3">feature',
100 sprintf('<th colspan="%d" title="%.1f%%">%s',
101 scalar @{ $versions{$_} },
102 sum(values %{ $canihas->{$_} }),
104 my $name = $caniuse->{agents}->{$_}->{browser};
105 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
111 print '<thead>', $header;
112 # preceding row without any colspan to work around gecko bug
115 for my $browser (@browsers) {
116 printf('<td title="%.1f%%">%s',
117 $canihas->{$browser}->{$_}, showversions($_),
118 ) for @{ $versions{$browser} };
124 # relative amount of support for given feature
125 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
127 if (my $row = shift) {
129 while (my ($browser, $versions) = each %$row) {
130 ref $versions eq 'HASH' or next;
131 while (my ($version, $_) = each %$versions) {
132 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
138 while (my ($browser, $vercols) = each %versions) {
139 my $div = 0; # multiplier exponent (decreased to lower value)
140 my @vers = map { $row->{$browser}->{$_} } @$vercols;
141 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
142 my @future; # find upcoming releases (after current)
143 for (reverse @$vercols) {
144 last if $_ eq $current;
145 push @future, pop @vers;
146 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
148 splice @vers, -1, 0, @future; # move ahead to decrease precedence
150 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
157 featurescore($caniuse->{data}->{$b}->{stats})
158 <=> featurescore($caniuse->{data}->{$a}->{stats})
159 } keys %{ $caniuse->{data} }) {
160 my $row = $caniuse->{data}->{$id};
161 my $data = $row->{stats} or next; # skip metadata [summary]
162 printf '<tr id="%s">', $id;
163 for ($row->{categories}) {
164 my $cell = $_ ? lc $_->[0] : '-';
168 sprintf('<a href="%s" onclick="%s">%s</a>',
170 sprintf("try { %s; return false } catch(err) { return true }",
171 "document.getElementById('$id').classList.toggle('target')",
176 print '<div class=aside>';
177 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
178 $row->{description}, $row->{notes};
179 printf 'Resources: %s.', join(', ',
180 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
181 ) for grep { @$_ } $row->{links} // ();
183 for ($row->{status}) {
184 my $cell = $_ // '-';
185 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
186 printf '<td title="%s" class="l %s">%s',
187 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
189 for my $browser (@browsers) {
191 for my $ver (@{ $versions{$browser} }, undef) {
192 unless (!defined $prev
193 or ref $data->{$browser} eq 'HASH'
194 && $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
195 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
196 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
199 ref $data->{$browser} ne 'HASH' ? 'u' :
200 $data->{$browser}->{$prev} // 'u'
202 !$usage ? ('p0') : ('p',
203 sprintf('p%01d', $usage / 10),
204 sprintf('p%02d', $usage),
206 sprintf('pp%02d', $usage / $scorediv),
218 state $maxscore = featurescore({ # yes for every possible version
219 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
221 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
223 print '<tfoot>', $header;
227 # normalised version number comparable as string (cmp)
228 shift =~ /^(\d*)(.*)/;
229 return sprintf('%02d', $1 || 0) . $2;
233 my @span = ($_[0], @_>1 ? $_[-1] : ());
238 return join('‒', @span);
246 <table class="glyphs"><tr>
247 <td class="X l5">supported
248 <td class="X l3">partial
249 <td class="X l2">external (js/plugin)
250 <td class="X l1">missing
251 <td class="X l0">unknown
252 <td class="X l0 ex">prefixed
255 <p><: if ($usage) { :>
257 <span class=" p0">0</span> -
258 <span class="p p0 p00">.01</span> -
259 <span class="p p0 p05">1-9</span> -
260 <span class="p p1">10</span> -
261 <span class="p p2">20</span> -
262 <span class="p p5">majority</span>
264 <table class="glyphs"><tr>
265 <td class="p p1">previous version</td>
266 <td class="p p3">current</td>
267 <td class="p p0 p00">upcoming (within months)</td>
268 <td class=" p0">future (within a year)</td>
273 <ul class="legend legend-set">
274 <li>default <strong>style</strong> is
275 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
276 <li><strong>usage</strong> source is
277 <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>