rack-html5
Rack::Html5 detects the browser and its version by parsing the user agent and adds a HTTP header to the current request for each feature the browser supports.
Usage
gem install rack-html5
require 'rack-html5'
use Rack::Html5
app = lambda { |env| [200, { 'Content-Type' => 'text/html' }, '<html><body><p></p></body></html>'] }
run app
Rack::Html5 then sets the following HTTP headers:
-
HTTP_X_DETECTED_BROWSER: Browser that has been DETECTED, eg.: Firefox
-
HTTP_X_DETECTED_VERSION: Version of the browser that has been DETECTED, eg.: 3.6.6
-
HTTP_X_SUPPORTS_HTML5_WYSIWYG: WYSIWYG editable elements
-
HTTP_X_SUPPORTS_HTML5_CLASSNAME: getElementsByClassName
-
HTTP_X_SUPPORTS_HTML5_ELEMENTS: Stylable HTML5 elements
-
HTTP_X_SUPPORTS_HTML5_CANVAS: Canvas (basic support)
-
HTTP_X_SUPPORTS_HTML5_MESSAGING: Cross-document messaging
-
HTTP_X_SUPPORTS_HTML5_AUDIO: Audio element
-
HTTP_X_SUPPORTS_HTML5_VIDEO: Video element
-
HTTP_X_SUPPORTS_HTML5_TEXTAPI: Text API for canvas
-
HTTP_X_SUPPORTS_HTML5_DRAGANDDROP: Drag and drop
-
HTTP_X_SUPPORTS_HTML5_OFFLINE: Offline web applications
-
HTTP_X_SUPPORTS_HTML5_SVG: Inline SVG
-
HTTP_X_SUPPORTS_HTML5_FORMS: Form features (Web Forms 2.0)
Rails
The Rack-Html5-rails_helper gem provides Rails helper methods, so you don’t have to read the headers by yourself: github.com/masone/rack-html5-rails_helper
Configuration for Rails 2
Add the following lines to the config block in your environment.rb file:
config.gem "rack-html5"
config.middleware.use "Rack::Html5"
Configuration for Rails 3
Add the gem dependency to Gemfile:
gem 'rack-html5'
Install the gem with bundler:
sudo bundler install
Add the Rack middleware to config.ru:
use Rack::Html5
Notices
-
The matching is done based on the information provided on: caniuse.com/#eras=farpast,past,now&cats=HTML5&statuses=rec,pr,cr,wd,ietf&nodetails=1
-
If the feature is not supported, the header will not be set.
-
If a browser implements a feature only partially, the corresponding header will not be set.
-
It is assumed that future versions will still implement a certain feature if it’s implemented now.
-
User agent matching is a dirty thing. Feel free to help improving the matching rules.