3 use List::Util qw(sum max);
6 title => 'browser compatibility cheat sheet',
10 keywords => [qw'html css browser feature'],
11 stylesheet => [qw'light dark circus mono red'],
12 data => ['caniuse.js'],
16 <h1>Browser compatibility</h1>
18 <p>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
19 with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
20 browser usage statistics.</p>
24 use File::Slurp 'read_file';
25 my $source = read_file('caniuse.js');
26 for ($source) { # cleanup
27 # convert seperate variables to hash keys
29 s/^caniuse\.(\w+) = /"$1":/gm;
32 # fractions not supported by barekey
33 s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
34 # escapes not supported in singlequote
35 s{'((?:[^\\']+|\\.)*)'}{
42 my $caniuse = from_json($source, {
43 # allow_singlequote => 1,
58 unoff => 'di-rare', # unofficial
60 pr => 'di-prop', # proposed
61 cr => 'di-d', # candidate
62 rec => 'di-a', # recommendation
63 ietf => 'di-aa', # standard
65 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
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 ];
73 my $canihas = do 'browser-usage.inc.pl' or do {
74 printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
77 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
79 print '<table class="mapped">';
81 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
85 print '<th colspan="3">feature';
86 printf '<th colspan="%d">%s',
87 scalar @{ $versions{$_} }, $caniuse->{agents}->{$_}->{browser}
90 # preceding row without any colspan to work around gecko bug
92 print '<td>' x $_ for 3, (map { scalar @{ $versions{$_} } } @browsers), 1;
96 # relative amount of support for given feature
97 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
99 if (my $row = shift) {
101 while (my ($browser, $versions) = each %$row) {
102 while (my ($version, $_) = each %$versions) {
103 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
109 while (my ($browser, $vercols) = each %versions) {
110 my $div = 0; # multiplier exponent (decreased to lower value)
111 my @vers = map { $row->{$browser}->{$_} } @$vercols;
112 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
113 my @future; # find upcoming releases (after current)
114 for (reverse @$vercols) {
115 last if $_ eq $current;
116 push @future, pop @vers;
117 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
119 splice @vers, -1, 0, @future; # move ahead to decrease precedence
121 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
128 featurescore($caniuse->{data}->{$b}->{stats})
129 <=> featurescore($caniuse->{data}->{$a}->{stats})
130 } keys %{ $caniuse->{data} }) {
131 my $row = $caniuse->{data}->{$id};
132 my $data = $row->{stats} or next; # skip metadata [summary]
133 printf '<tr id="%s">', $id;
134 for ($row->{categories}) {
135 my $cell = $_ ? lc $_->[0] : '-';
138 printf '<td title="%s">%s', $row->{description}, $row->{title};
139 for ($row->{status}) {
140 my $cell = $_ // '-';
141 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
142 printf '<td title="%s" class="%s">%s',
143 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
145 for my $browser (@browsers) {
147 for my $ver (@{ $versions{$browser} }, undef) {
148 unless (!defined $prev
149 or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
150 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
151 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
153 $CSTATS{ $data->{$browser}->{$prev} },
154 sprintf('opacity%.0f', $usage / $scorediv),
158 showversions(\@span),
166 state $maxscore = featurescore({ # yes for every possible version
167 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
169 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
174 # normalised version number comparable as string (cmp)
175 shift =~ /^(\d*)(.*)/;
176 return sprintf('%02d', $1) . $2;
181 splice @$span, 1, -1;
186 return join('‒', @$span);
193 <table class="glyphs"><tr>
194 <td class="X di-aa">supported
195 <td class="X di-a">prefixed
196 <td class="X di-d">partial
197 <td class="X di-prop">external (js/plugin)
198 <td class="X di-b">missing
202 <ul class="legend legend-set">
203 <li>default <strong>style</strong> is
204 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>