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