13e6b5e1ac7f30f6d3e97091a5d344436172db31
[sheet.git] / browser.plp
1 <(common.inc.plp)><:
2 use 5.010;
3 use utf8;
4 use List::Util qw(sum max);
5
6 Html({
7         title => 'browser compatibility cheat sheet',
8         version => 'v1.0',
9         description =>
10                 "caniuse.",
11         keywords => [qw'html css browser feature'],
12         stylesheet => [qw'circus dark mono red light'],
13         data => ['browser-support.inc.pl'],
14 });
15
16 :>
17 <h1>Browser compatibility</h1>
18
19 <p id="intro">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') {
22         when (!$_) {
23                 # none
24         }
25         when (!/^\w+$/) {
26                 printf "<p>Invalid browser usage data request: <em>%s</em>",
27                         'identifier must be alphanumeric name or <q>0</q>';
28         }
29         $canihas = do "browser-usage-$_.inc.pl" or do {
30                 printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
31                 break;
32         };
33         $usage = $_;
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";
38 }
39 :>.
40 </p>
41
42 <:
43 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
44 $_->{verrelease} = {
45         # mark last two (future) versions as unreleased, ensure current isn't
46         map { $_->[-1] => 0, $_->[-2] => 0, $_->[-3] => undef } $_->{versions}
47 } for values %{ $caniuse->{agents} };
48
49 my %CSTATS = (
50         'n'   => 'l1',
51         'y'   => 'l5',
52         'y x' => 'l5 ex',
53         'a'   => 'l3',
54         'a x' => 'l3 ex',
55         'p j' => 'l2',
56         'j'   => 'l2',
57         'p'   => 'l2',
58         'p p' => 'l2',
59         'u'   => 'l0',
60 );
61 my %CSTATUS = (
62         unoff => 'l1', # unofficial
63         wd    => 'l3', # draft
64         cr    => 'l4', # candidate
65         pr    => 'l4', # proposed
66         rec   => 'l5', # recommendation
67         other => 'l2', # non-w3
68         ietf  => 'l5', # standard
69 );
70 my %versions;
71 if (my ($somerow) = values %{ $caniuse->{data} }) {
72         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
73                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
74         }
75 }
76 my @browsers = grep { $versions{$_} }
77         qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
78
79 $canihas ||= {
80         map {
81                 $_ => +{
82                         map {
83                                 my $zero = $#$_ - 2;  # baseline index
84                                 ($_->[$zero - 2] =>  .5), # past
85                                 ($_->[$zero - 1] => 10 ), # previous
86                                 ($_->[$zero + 2] =>  0 ), # future
87                                 ($_->[$zero + 1] =>  .5), # next
88                                 ($_->[$zero    ] => 30 ), # current
89                         } $caniuse->{agents}->{$_}->{versions}
90                 }
91         } @browsers
92 }; # fallback hash based on release semantics
93 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
94
95 print '<table class="mapped">';
96 print '<col>' x 3;
97 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
98 print "\n";
99
100 my $header = join('',
101         '<tr>',
102         '<th colspan="3">feature',
103         (map {
104                 my $name = $caniuse->{agents}->{$_}->{browser};
105                 sprintf('<th colspan="%d" title="%s">%s',
106                         scalar @{ $versions{$_} },
107                         join(' ',
108                                 sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
109                                 $name,
110                         ),
111                         do {
112                                 length $name < 3 + @{ $versions{$_} }*2 ? $name
113                                         : $caniuse->{agents}->{$_}->{abbr};
114                         },
115                 )
116         } @browsers),
117         '<th>%',
118 );
119 print '<thead>', $header;
120 # preceding row without any colspan to work around gecko bug
121 print "\n<tr>";
122 print '<td>' x 3;
123 for my $browser (@browsers) {
124         printf('<td title="%.1f%%"%s>%s',
125                 $canihas->{$browser}->{$_},
126                 (map {
127                         defined $_ && !$_ && ' class="ex"'
128                 } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
129                 showversions($_),
130         ) for @{ $versions{$browser} };
131 }
132 print '<td>' x 1;
133 say '</thead>';
134 say '<tfoot>', $header, '</tfoot>';
135
136 sub featurescore {
137         # relative amount of support for given feature
138         state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
139         my $rank = 0;
140         if (my $row = shift) {
141                 if ($canihas) {
142                         while (my ($browser, $versions) = each %$row) {
143                                 ref $versions eq 'HASH' or next;
144                                 while (my ($version, $_) = each %$versions) {
145                                         $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
146                                 }
147                         }
148                         return $rank;
149                 }
150
151                 while (my ($browser, $vercols) = each %versions) {
152                         my $div = 0;  # multiplier exponent (decreased to lower value)
153                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
154                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
155                                 my @future;  # find upcoming releases (after current)
156                                 for (reverse @$vercols) {
157                                         last if $_ eq $current;
158                                         push @future, pop @vers;
159                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
160                                 }
161                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
162                         }
163                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
164                 }
165         }
166         return $rank;
167 }
168
169 sub saytitlecol {
170         my ($id) = @_;
171         my $row = $caniuse->{data}->{$id};
172
173         for ($row->{categories}) {
174                 my $cell = $_ ? lc $_->[0] : '-';
175                 printf '<th title="%s">%s', join(' + ', @$_), $cell;
176         }
177
178         print '<td>', map {
179                 sprintf('<a href="%s" onclick="%s">%s</a>',
180                         "#$id",
181                         sprintf("try { %s; return false } catch(err) { return true }",
182                                 "document.getElementById('$id').classList.toggle('target')",
183                         ),
184                         Entity($_),
185                 );
186         } $row->{title};
187         print '<div class=aside>';
188         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
189                 Entity($row->{description}), formathtml($row->{notes});  # sic
190         printf 'Resources: %s.', join(', ', map {
191                 sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
192         } @$_) for grep { @$_ } $row->{links} // ();
193         print '</div>';
194 }
195
196 sub saystatuscol {
197         my ($id) = @_;
198         my $row = $caniuse->{data}->{$id};
199
200         for ($row->{status}) {
201                 my $cell = $_ // '-';
202                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
203                 printf '<td title="%s" class="l %s">%s',
204                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
205         }
206 }
207
208 sub saybrowsercols {
209         my ($id, $browser) = @_;
210         my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
211
212         my ($prev, @span);
213         for my $ver (@{ $versions{$browser} }, undef) {
214                 unless (!defined $prev
215                 or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
216                         my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
217                         printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
218                                 join(' ',
219                                         X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
220                                         !$usage ? ('p0') : ('p',
221                                                 sprintf('p%01d', $usage / 10),
222                                                 sprintf('p%02d', $usage),
223                                         ),
224                                         sprintf('pp%02d', $usage / $scorediv),
225                                 ),
226                                 scalar @span,
227                                 $usage,
228                                 showversions(@span),
229                         undef $prev;
230                         @span = ();
231                 }
232                 push @span, $ver;
233                 $prev = $ver;
234         }
235 }
236
237 sub sayusagecol {
238         my ($id) = @_;
239         state $maxscore = featurescore({  # yes for every possible version
240                 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
241         });
242         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
243 }
244
245 say '<tbody>';
246 for my $id (sort {
247             featurescore($caniuse->{data}->{$b}->{stats})
248         <=> featurescore($caniuse->{data}->{$a}->{stats})
249 } keys %{ $caniuse->{data} }) {
250         $caniuse->{data}->{$id}->{stats} or next;  # skip metadata [summary]
251         printf '<tr id="%s">', $id;
252         saytitlecol($id);
253         saystatuscol($id);
254         saybrowsercols($id, $_) for @browsers;
255         sayusagecol($id);
256         say '</tr>';
257 }
258 say '</tbody>';
259 say '</table>';
260
261 sub formathtml {
262         my $ref = defined wantarray ? [@_] : \@_;
263         for (@$ref) {
264                 s/& (?!\w)/&amp;/gx;
265                 s/< \s/&lt;/gx;
266                 s/\n\K\n/<br>/g;
267         }
268         return @$ref;
269 }
270
271 sub paddedver {
272         # normalised version number comparable as string (cmp)
273         shift =~ /^(\d*)(.*)/;
274         return sprintf('%02d', $1 || 0) . $2;
275 }
276
277 sub showversions {
278         my @span = ($_[0], @_>1 ? $_[-1] : ());
279         for (@span) {
280                 s/^\./0./;
281                 s/x$/.*/;
282         }
283         return join('‒', @span);
284 }
285
286 :>
287 <hr>
288
289 <div class="legend">
290         <table class="glyphs"><tr>
291         <td class="X l5">supported
292         <td class="X l3">partial
293         <td class="X l2">external (js/plugin)
294         <td class="X l1">missing
295         <td class="X l0">unknown
296         <td class="X ex">prefixed
297         </table>
298
299         <p><: if ($usage) { :>
300                 Usage percentage:
301                 <span class="  p0">0</span> -
302                 <span class="p p0 p00">.01</span> -
303                 <span class="p p0 p05">1-9</span> -
304                 <span class="p p1">10</span> -
305                 <span class="p p2">20</span> -
306                 <span class="p p5">majority</span>
307 <: } else { :>
308                 <table class="glyphs"><tr>
309                         <td class="p p1">previous version</td>
310                         <td class="p p3">current</td>
311                         <td class="p p0 p00">upcoming (within months)</td>
312                         <td class="  p0">future (within a year)</td>
313                 </table>
314 <: } :> </p>
315
316         <div class="right">
317                 <ul class="legend legend-set">
318                 <li>default <strong>style</strong> is
319                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
320                 <li><strong>usage</strong> source is
321                         <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
322                 </ul>
323         </div>
324 </div>
325
326 <script type="text/javascript" src="/searchlocal.js"></script>
327 <script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>
328