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
21 my $canihas = do 'browser-usage.inc.pl'
22 or printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $! || $@;
24 print "with $_ browser usage statistics." for map {
25 my $ref = $_->{-source} || 'unknown';
26 $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $_->{-url} || ();
27 $ref .= " $_" for $_->{-date} || ();
35 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
49 unoff => 'l0', # unofficial
51 cr => 'l5', # candidate
52 pr => 'l5', # proposed
53 rec => 'l6', # recommendation
54 ietf => 'l6', # standard
57 if (my ($somerow) = values %{ $caniuse->{data} }) {
58 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
59 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
62 my @browsers = grep { $versions{$_} }
63 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
69 ($_->[4] => 0 ), # future
70 ($_->[3] => .5), # next
71 ($_->[0] => 5 ), # past
72 ($_->[1] => 10 ), # previous
73 ($_->[2] => 30 ), # current
74 } $caniuse->{agents}->{$_}->{versions}
77 }; # fallback hash based on release semantics
78 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
80 print '<table class="mapped">';
82 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
87 '<th colspan="3">feature',
89 sprintf('<th colspan="%d" title="%.1f%%">%s',
90 scalar @{ $versions{$_} },
91 sum(values %{ $canihas->{$_} }),
93 my $name = $caniuse->{agents}->{$_}->{browser};
94 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
100 print '<thead>', $header;
101 # preceding row without any colspan to work around gecko bug
104 for my $browser (@browsers) {
105 printf('<td title="%.1f%%">%s',
106 $canihas->{$browser}->{$_}, showversions($_),
107 ) for @{ $versions{$browser} };
113 # relative amount of support for given feature
114 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
116 if (my $row = shift) {
118 while (my ($browser, $versions) = each %$row) {
119 ref $versions eq 'HASH' or next;
120 while (my ($version, $_) = each %$versions) {
121 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
127 while (my ($browser, $vercols) = each %versions) {
128 my $div = 0; # multiplier exponent (decreased to lower value)
129 my @vers = map { $row->{$browser}->{$_} } @$vercols;
130 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
131 my @future; # find upcoming releases (after current)
132 for (reverse @$vercols) {
133 last if $_ eq $current;
134 push @future, pop @vers;
135 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
137 splice @vers, -1, 0, @future; # move ahead to decrease precedence
139 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
146 featurescore($caniuse->{data}->{$b}->{stats})
147 <=> featurescore($caniuse->{data}->{$a}->{stats})
148 } keys %{ $caniuse->{data} }) {
149 my $row = $caniuse->{data}->{$id};
150 my $data = $row->{stats} or next; # skip metadata [summary]
151 printf '<tr id="%s">', $id;
152 for ($row->{categories}) {
153 my $cell = $_ ? lc $_->[0] : '-';
157 sprintf('<a href="%s" onclick="%s">%s</a>',
159 sprintf("try { %s; return false } catch(err) { return true }",
160 "document.getElementById('$id').classList.toggle('target')",
165 print '<div class=aside>';
166 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
167 $row->{description}, $row->{notes};
168 printf 'Resources: %s.', join(', ',
169 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
170 ) for grep { @$_ } $row->{links} // ();
172 for ($row->{status}) {
173 my $cell = $_ // '-';
174 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
175 printf '<td title="%s" class="%s">%s',
176 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
178 for my $browser (@browsers) {
180 for my $ver (@{ $versions{$browser} }, undef) {
181 unless (!defined $prev
182 or ref $data->{$browser} eq 'HASH'
183 && $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
184 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
185 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
188 ref $data->{$browser} ne 'HASH' ? 'u' :
189 $data->{$browser}->{$prev} // 'u'
191 !$usage ? ('p0') : ('p',
192 sprintf('p%01d', $usage / 10),
193 sprintf('p%02d', $usage),
195 sprintf('pp%02d', $usage / $scorediv),
207 state $maxscore = featurescore({ # yes for every possible version
208 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
210 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
212 print '<tfoot>', $header;
216 # normalised version number comparable as string (cmp)
217 shift =~ /^(\d*)(.*)/;
218 return sprintf('%02d', $1 || 0) . $2;
222 my @span = ($_[0], @_>1 ? $_[-1] : ());
227 return join('‒', @span);
235 <table class="glyphs"><tr>
236 <td class="X l6">supported
237 <td class="X l5">prefixed
238 <td class="X l4">partial
239 <td class="X l2">external (js/plugin)
240 <td class="X l0">missing
241 <td class="X l9">unknown
246 <span class=" p0">0</span> -
247 <span class="p p0 p00">.01</span> -
248 <span class="p p0 p05">1-9</span> -
249 <span class="p p1">10</span> -
250 <span class="p p2">20</span> -
251 <span class="p p5">majority</span>
255 <ul class="legend legend-set">
256 <li>default <strong>style</strong> is
257 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>