1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00

Add an engines parser

This commit is contained in:
Denis Demchenko 2017-06-09 23:04:43 +03:00
parent c3ad15e0ff
commit a794b10797
3 changed files with 157 additions and 19 deletions

View File

@ -29,13 +29,6 @@ import {
} from './utils'; } from './utils';
const commonVersionIdentifier = /version\/(\d+(\.?_?\d+)+)/i; const commonVersionIdentifier = /version\/(\d+(\.?_?\d+)+)/i;
const RENDERING_ENGINES_NAMES = {
blink: 'Blink',
webkit: 'WebKit',
gecko: 'Gecko',
presto: 'Presto',
edgehtml: 'EdgeHTML'
};
const browsersList = [ const browsersList = [
{ {
@ -177,7 +170,6 @@ const browsersList = [
return { return {
name: 'Microsoft Edge', name: 'Microsoft Edge',
engine: 'EdgeHTML',
version version
} }
} }
@ -199,7 +191,6 @@ const browsersList = [
const version = getFirstMatch(/seamonkey\/(\d+(\.?_?\d+)+)/i, ua); const version = getFirstMatch(/seamonkey\/(\d+(\.?_?\d+)+)/i, ua);
return { return {
name: 'SeaMonkey', name: 'SeaMonkey',
engine: RENDERING_ENGINES_NAMES.gecko,
version version
} }
} }
@ -210,7 +201,6 @@ const browsersList = [
const version = getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.?_?\d+)+)/i, ua); const version = getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.?_?\d+)+)/i, ua);
return { return {
name: 'Firefox', name: 'Firefox',
engine: RENDERING_ENGINES_NAMES.gecko,
version version
} }
} }
@ -317,16 +307,9 @@ const browsersList = [
test: [/chrome|crios|crmo/i], test: [/chrome|crios|crmo/i],
describe(ua) { describe(ua) {
const version = getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.?_?\d+)+)/i, ua); const version = getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.?_?\d+)+)/i, ua);
let engine;
if (/(apple)?webkit\/537\.36/i.test(ua)) {
engine = RENDERING_ENGINES_NAMES.blink;
} else {
engine = RENDERING_ENGINES_NAMES.webkit;
}
return { return {
name: 'Chrome', name: 'Chrome',
engine,
version version
} }
} }
@ -357,7 +340,6 @@ const browsersList = [
return { return {
name: 'Safari', name: 'Safari',
engine: RENDERING_ENGINES_NAMES.webkit,
version version
} }
} }

110
src/parser-engines.js Normal file
View File

@ -0,0 +1,110 @@
import {
getFirstMatch
} from './utils';
/*
* More specific goes first
*/
export default [
/* EdgeHTML */
{
test(parser) {
return parser.getBrowserName(true) === 'microsoft edge';
},
describe(ua) {
const version = getFirstMatch(/edge\/(\d+(\.?_?\d+)+)/i, ua);
return {
name: 'EdgeHTML',
version
};
}
},
/* Trident */
{
test: [/trident/i],
describe(ua) {
const engine = {
name: 'Trident'
};
const version = getFirstMatch(/trident\/(\d+(\.?_?\d+)+)/i, ua);
if (version) {
engine.version = version;
}
return engine;
}
},
/* Presto */
{
test(parser) {
return parser.test(/presto/i);
},
describe(ua) {
const engine = {
name: 'Presto'
};
const version = getFirstMatch(/presto\/(\d+(\.?_?\d+)+)/i, ua);
if (version) {
engine.version = version;
}
return engine;
}
},
/* Gecko */
{
test(parser) {
const isGecko = parser.test(/gecko/i);
const likeGecko = parser.test(/like gecko/i);
return isGecko && !likeGecko;
},
describe(ua) {
const engine = {
name: 'Gecko'
};
const version = getFirstMatch(/gecko\/(\d+(\.?_?\d+)+)/i, ua);
if (version) {
engine.version = version;
}
return engine;
}
},
/* Blink */
{
test: [/(apple)?webkit\/537\.36/i],
describe() {
return {
name: 'Blink'
};
}
},
/* WebKit */
{
test: [/(apple)?webkit/i],
describe(ua) {
const engine = {
name: 'WebKit'
};
const version = getFirstMatch(/webkit\/(\d+(\.?_?\d+)+)/i, ua);
if (version) {
engine.version = version;
}
return engine;
}
}
];

View File

@ -1,6 +1,7 @@
import browserParsersList from './parser-browsers'; import browserParsersList from './parser-browsers';
import osParsersList from './parser-os'; import osParsersList from './parser-os';
import platformParsersList from './parser-platforms'; import platformParsersList from './parser-platforms';
import enginesParsersList from './parser-engines';
class Parser { class Parser {
/** /**
@ -87,7 +88,10 @@ class Parser {
* *
* @public * @public
*/ */
getBrowserName() { getBrowserName(toLowerCase) {
if (toLowerCase) {
return String(this.getBrowser().name).toLowerCase();
}
return this.getBrowser().name; return this.getBrowser().name;
} }
@ -214,6 +218,47 @@ class Parser {
return this.parsedResult.platform; return this.parsedResult.platform;
} }
/**
* Get parsed engine
* @return {{}}
*/
getEngine() {
if (this.parsedResult.engine) {
return this.parsedResult.engine;
}
return this._parseEngine();
}
/**
* Get parsed platform
* @return {{}}
* @private
*/
_parseEngine() {
this.parsedResult.engine = {};
const engine = enginesParsersList.find(_engine => {
if (typeof _engine.test === 'function') {
return _engine.test(this);
}
if (_engine.test instanceof Array) {
return _engine.test.some((condition) => {
return this.test(condition);
});
}
throw new Error("Browser's test function is not valid");
});
if (engine) {
this.parsedResult.engine = engine.describe(this.getUA());
}
return this.parsedResult.engine;
}
/** /**
* Parse full information about the browser * Parse full information about the browser
*/ */
@ -221,6 +266,7 @@ class Parser {
this._parseBrowser(); this._parseBrowser();
this._parseOS(); this._parseOS();
this._parsePlatform(); this._parsePlatform();
this._parseEngine();
return this; return this;
} }