Move user-agent code to PaperScope and expose through PaperScope#browser.

This commit is contained in:
Jürg Lehni 2014-09-29 13:07:02 +02:00
parent 6a11532322
commit e541b10e89
2 changed files with 37 additions and 28 deletions

View file

@ -60,16 +60,49 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
// Assign a unique id to each scope . // Assign a unique id to each scope .
this._id = PaperScope._id++; this._id = PaperScope._id++;
PaperScope._scopes[this._id] = this; PaperScope._scopes[this._id] = this;
var proto = PaperScope.prototype;
if (!this.support) { if (!this.support) {
// Set up paper.support, as an object containing properties that // Set up paper.support, as an object containing properties that
// describe the support of various features. // describe the support of various features.
var ctx = CanvasProvider.getContext(1, 1); var ctx = CanvasProvider.getContext(1, 1);
PaperScope.prototype.support = { proto.support = {
nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx, nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx,
nativeBlendModes: BlendMode.nativeModes nativeBlendModes: BlendMode.nativeModes
}; };
CanvasProvider.release(ctx); CanvasProvider.release(ctx);
} }
/*#*/ if (__options.environment == 'browser') {
if (!this.browser) {
var browser = proto.browser = {};
// Use replace() to get all matches, and deal with Chrome/Webkit
// overlap:
// TODO: Do we need Mozilla next to Firefox? Other than the
// different treatment of the Chrome/Webkit overlap
// here: { chrome: true, webkit: false }, mozilla missing is the
// only difference to jQuery.browser
navigator.userAgent.toLowerCase().replace(
/(opera|chrome|safari|webkit|firefox|msie|trident)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,
function(all, n, v1, v2, rv) {
// Do not set additional browsers once chrome is detected.
if (!browser.chrome) {
var v = n === 'opera' ? v2 : v1;
if (n === 'trident') {
// Use rv: and rename to msie
v = rv;
n = 'msie';
} 
browser.version = v;
browser.versionNumber = parseFloat(v);
browser.name = n;
browser[n] = true;
}
}
);
if (browser.chrome)
delete browser.webkit;
}
/*#*/ } // __options.environment == 'browser'
}, },
/** /**

View file

@ -22,32 +22,6 @@ Base.exports.PaperScript = (function() {
scope = this; scope = this;
/*#*/ include('../../bower_components/acorn/acorn.min.js', { exports: false }); /*#*/ include('../../bower_components/acorn/acorn.min.js', { exports: false });
/*#*/ if (__options.environment == 'browser') {
// We need some browser info for dealing with source maps and code offsets
var ua = navigator.userAgent,
browser = {};
// Use replace() to get all matches, and deal with overlaps (e.g. Chrome)
ua.toLowerCase().replace(
/(opera|chrome|safari|webkit|firefox|msie|trident)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,
function(all, n, v1, v2, rv) {
// Do not set additional browsers once chrome is detected.
if (!browser.chrome) {
var v = n === 'opera' ? v2 : v1;
if (n === 'trident') {
// Use rv: and rename to msie
v = rv;
n = 'msie';
}
browser.version = parseFloat(v);
browser.name = n;
browser[n] = true;
if (browser.chrome)
delete browser.webkit;
}
}
);
/*#*/ } // __options.environment == 'browser'
// Operators to overload // Operators to overload
var binaryOperators = { var binaryOperators = {
@ -264,7 +238,8 @@ Base.exports.PaperScript = (function() {
/*#*/ if (__options.environment == 'browser') { /*#*/ if (__options.environment == 'browser') {
// Source-map support: // Source-map support:
var sourceMap = null, var sourceMap = null,
version = browser.version, browser = paper.browser,
version = browser.versionNumber,
lineBreaks = /\r\n|\n|\r/mg; lineBreaks = /\r\n|\n|\r/mg;
// TODO: Verify these browser versions for source map support, and check // TODO: Verify these browser versions for source map support, and check
// other browsers. // other browsers.
@ -396,6 +371,7 @@ Base.exports.PaperScript = (function() {
if (handlers) if (handlers)
code += '\nreturn { ' + handlers + ' };'; code += '\nreturn { ' + handlers + ' };';
/*#*/ if (__options.environment == 'browser') { /*#*/ if (__options.environment == 'browser') {
var browser = paper.browser;
if (browser.chrome || browser.firefox) { if (browser.chrome || browser.firefox) {
// On Firefox, all error numbers inside dynamically compiled code // On Firefox, all error numbers inside dynamically compiled code
// are relative to the line where the eval / compilation happened. // are relative to the line where the eval / compilation happened.