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'],
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 with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
21 browser usage statistics.</p>
24 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
37 unoff => 'ex', # unofficial
39 pr => 'l2', # proposed
40 cr => 'l4', # candidate
41 rec => 'l5', # recommendation
42 ietf => 'l6', # standard
44 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
46 if (my ($somerow) = values %{ $caniuse->{data} }) {
47 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
48 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
52 my $canihas = do 'browser-usage.inc.pl' or do {
53 printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
56 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
58 print '<table class="mapped">';
60 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
65 '<th colspan="3">feature',
67 sprintf('<th colspan="%d" title="%.1f%%">%s',
68 scalar @{ $versions{$_} },
69 sum(values %{ $canihas->{$_} }),
71 my $name = $caniuse->{agents}->{$_}->{browser};
72 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
78 print '<thead>', $header;
79 # preceding row without any colspan to work around gecko bug
82 for my $browser (@browsers) {
83 printf('<td title="%.1f%%">%s',
84 $canihas->{$browser}->{$_}, showversions($_),
85 ) for @{ $versions{$browser} };
91 # relative amount of support for given feature
92 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
94 if (my $row = shift) {
96 while (my ($browser, $versions) = each %$row) {
97 while (my ($version, $_) = each %$versions) {
98 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
104 while (my ($browser, $vercols) = each %versions) {
105 my $div = 0; # multiplier exponent (decreased to lower value)
106 my @vers = map { $row->{$browser}->{$_} } @$vercols;
107 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
108 my @future; # find upcoming releases (after current)
109 for (reverse @$vercols) {
110 last if $_ eq $current;
111 push @future, pop @vers;
112 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
114 splice @vers, -1, 0, @future; # move ahead to decrease precedence
116 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
123 featurescore($caniuse->{data}->{$b}->{stats})
124 <=> featurescore($caniuse->{data}->{$a}->{stats})
125 } keys %{ $caniuse->{data} }) {
126 my $row = $caniuse->{data}->{$id};
127 my $data = $row->{stats} or next; # skip metadata [summary]
128 printf '<tr id="%s">', $id;
129 for ($row->{categories}) {
130 my $cell = $_ ? lc $_->[0] : '-';
133 printf '<td title="%s">%s', $row->{description}, $row->{title};
134 for ($row->{status}) {
135 my $cell = $_ // '-';
136 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
137 printf '<td title="%s" class="%s">%s',
138 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
140 for my $browser (@browsers) {
142 for my $ver (@{ $versions{$browser} }, undef) {
143 unless (!defined $prev
144 or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
145 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
146 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
148 $CSTATS{ $data->{$browser}->{$prev} },
149 sprintf('opacity%.0f', $usage / $scorediv),
161 state $maxscore = featurescore({ # yes for every possible version
162 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
164 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
166 print '<tfoot>', $header;
170 # normalised version number comparable as string (cmp)
171 shift =~ /^(\d*)(.*)/;
172 return sprintf('%02d', $1) . $2;
176 my @span = ($_[0], @_>1 ? $_[-1] : ());
181 return join('‒', @span);
186 var classmatch = /\bopacity(\d+)/;
187 Array.forEach(document.getElementsByTagName('TD'), function(val) {
189 if (opacity = classmatch.exec(val.className)) {
190 var c = document.defaultView.getComputedStyle(val, null).getPropertyValue('background-color');
191 var o = opacity[1] / 100;
192 val.style.backgroundColor = c.replace(/rgb\((.*)\)/, 'rgba($1, '+o+')');
193 val.style.textShadow = c+' 1px 1px 2px, ' + c+' -1px -1px 2px';
201 <table class="glyphs"><tr>
202 <td class="X l6">supported
203 <td class="X l5">prefixed
204 <td class="X l4">partial
205 <td class="X l2">external (js/plugin)
206 <td class="X l0">missing
210 <ul class="legend legend-set">
211 <li>default <strong>style</strong> is
212 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>