Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
fix: remove polyfill-service dependency
Browse files Browse the repository at this point in the history
Fixes a lot of warnings regarding outdated dependecies and allows
to install gemini with yarn.

Close #673, close #635
  • Loading branch information
Sergey Tatarintsev committed Nov 17, 2016
1 parent 4ea13c4 commit 2b71a19
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 77 deletions.
23 changes: 17 additions & 6 deletions lib/browser/client-scripts/gemini.calibrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@
bodyStyle.backgroundColor = '#96fa00';
}

function hasCSS3Selectors() {
try {
document.querySelector('body:nth-child(1)');
} catch (e) {
return false;
}
return true;
}

function needsCompatLib() {
return !hasCSS3Selectors() ||
!window.getComputedStyle ||
!window.matchMedia ||
!String.prototype.trim;
}

function getBrowserFeatures() {
var features = {
hasCSS3Selectors: true,
needsCompatLib: needsCompatLib(),
pixelRatio: window.devicePixelRatio,
innerWidth: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
};
try {
document.querySelector('body:nth-child(1)');
} catch (e) {
features.hasCSS3Selectors = false;
}

return features;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/browser/client-scripts/gemini.coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var util = require('./util'),
coverageLevel = require('../../coverage-level'),
rect = require('./rect'),
query = require('./query');
lib = require('./lib');

exports.collectCoverage = function collectCoverage(rect) {
var coverage = {},
Expand Down Expand Up @@ -49,7 +49,7 @@ function coverageForRule(rule, area, ctx) {
if (rule.cssRules || rule.styleSheet) {
if (rule.conditionText) {
ctx.media++;
if (!window.matchMedia(rule.conditionText).matches) {
if (!lib.matchMedia(rule.conditionText).matches) {
return;
}
}
Expand All @@ -67,17 +67,17 @@ function coverageForRule(rule, area, ctx) {
}

util.each(rule.selectorText.split(','), function(selector) {
selector = selector.trim();
selector = lib.trim(selector);
var coverage = coverageLevel.NONE,
matches = query.all(selector);
matches = lib.queryAll(selector);

var re = /:{1,2}(?:after|before|first-letter|first-line|selection)(:{1,2}\w+)?$/;
// if selector contains pseudo-elements cut it off and try to find element without it
if (matches.length === 0 && re.test(selector)) {
var newSelector = selector.replace(re, '$1').trim();
var newSelector = lib.trim(selector.replace(re, '$1'));

if (newSelector.length > 0) {
matches = query.all(newSelector);
matches = lib.queryAll(newSelector);
}
}

Expand Down
16 changes: 8 additions & 8 deletions lib/browser/client-scripts/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var util = require('./util'),
rect = require('./rect'),
query = require('./query'),
lib = require('./lib'),
Rect = rect.Rect;

if (typeof window === 'undefined') {
Expand All @@ -12,7 +12,7 @@ if (typeof window === 'undefined') {
window.__gemini = exports;
}

exports.query = query;
exports.queryFirst = lib.queryFirst;

// Terminology
// - clientRect - the result of calling getBoundingClientRect of the element
Expand Down Expand Up @@ -77,19 +77,19 @@ function prepareScreenshotUnsafe(selectors, opts) {
}

exports.resetZoom = function() {
var meta = query.first('meta[name="viewport"]');
var meta = lib.queryFirst('meta[name="viewport"]');
if (!meta) {
meta = document.createElement('meta');
meta.name = 'viewport';
query.first('head').appendChild(meta);
lib.queryFirst('head').appendChild(meta);
}
meta.content = 'width=device-width,initial-scale=1.0,user-scalable=no';
};

function getCaptureRect(selectors) {
var element, elementRect, rect;
for (var i = 0; i < selectors.length; i++) {
element = query.first(selectors[i]);
element = lib.queryFirst(selectors[i]);
if (!element) {
return {
error: 'NOTFOUND',
Expand All @@ -114,7 +114,7 @@ function getCaptureRect(selectors) {
function findIgnoreAreas(selectors) {
var result = [];
util.each(selectors, function(selector) {
var element = query.first(selector);
var element = lib.queryFirst(selector);
if (element) {
var rect = getElementCaptureRect(element);
if (rect) {
Expand All @@ -136,7 +136,7 @@ function isHidden(css, clientRect) {

function getElementCaptureRect(element) {
var pseudo = [':before', ':after'],
css = window.getComputedStyle(element),
css = lib.getComputedStyle(element),
clientRect = rect.getAbsoluteClientRect(element);

if (isHidden(css, clientRect)) {
Expand All @@ -146,7 +146,7 @@ function getElementCaptureRect(element) {
var elementRect = getExtRect(css, clientRect);

util.each(pseudo, function(pseudoEl) {
css = window.getComputedStyle(element, pseudoEl);
css = lib.getComputedStyle(element, pseudoEl);
elementRect = elementRect.merge(getExtRect(css, clientRect));
});

Expand Down
21 changes: 21 additions & 0 deletions lib/browser/client-scripts/lib.compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
/*jshint newcap:false*/
var Sizzle = require('sizzle');

exports.queryFirst = function(selector) {
var elems = Sizzle(exports.trim(selector) + ':first');
return elems.length > 0 ? elems[0] : null;
};

exports.queryAll = function(selector) {
return Sizzle(selector);
};

exports.trim = function(str) {
// trim spaces, unicode BOM and NBSP and the beginning and the end of the line
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};

exports.getComputedStyle = require('./polyfills/getComputedStyle').getComputedStyle;
exports.matchMedia = require('./polyfills/matchMedia').matchMedia;
21 changes: 21 additions & 0 deletions lib/browser/client-scripts/lib.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

exports.queryFirst = function(selector) {
return document.querySelector(selector);
};

exports.queryAll = function(selector) {
return document.querySelectorAll(selector);
};

exports.getComputedStyle = function(element, pseudoElement) {
return getComputedStyle(element, pseudoElement);
};

exports.matchMedia = function(mediaQuery) {
return matchMedia(mediaQuery);
};

exports.trim = function(str) {
return str.trim();
};
9 changes: 9 additions & 0 deletions lib/browser/client-scripts/polyfills/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2016 Financial Times

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
114 changes: 114 additions & 0 deletions lib/browser/client-scripts/polyfills/getComputedStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Adapted from: https://raw.githubusercontent.com/Financial-Times/polyfill-service
*/
function getComputedStylePixel(element, property, fontSize) {
var
// Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed.
value = element.document && element.currentStyle[property].match(/([\d\.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''],
size = value[1],
suffix = value[2],
rootSize;

fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16;
rootSize = property === 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;

return suffix === '%' ? size / 100 * rootSize :
suffix === 'cm' ? size * 0.3937 * 96 :
suffix === 'em' ? size * fontSize :
suffix === 'in' ? size * 96 :
suffix === 'mm' ? size * 0.3937 * 96 / 10 :
suffix === 'pc' ? size * 12 * 96 / 72 :
suffix === 'pt' ? size * 96 / 72 :
size;
}

function setShortStyleProperty(style, property) {
var
borderSuffix = property === 'border' ? 'Width' : '',
t = property + 'Top' + borderSuffix,
r = property + 'Right' + borderSuffix,
b = property + 'Bottom' + borderSuffix,
l = property + 'Left' + borderSuffix;

style[property] = (style[t] === style[r] && style[t] === style[b] && style[t] === style[l] ? [style[t]] :
style[t] === style[b] && style[l] === style[r] ? [style[t], style[r]] :
style[l] === style[r] ? [style[t], style[r], style[b]] :
[style[t], style[r], style[b], style[l]]).join(' ');
}

// <CSSStyleDeclaration>
function CSSStyleDeclaration(element) {
var currentStyle = element.currentStyle,
fontSize = getComputedStylePixel(element, 'fontSize'),
unCamelCase = function(match) {
return '-' + match.toLowerCase();
},
property;

for (property in currentStyle) {
Array.prototype.push.call(this, property === 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase));

if (property === 'width') {
this[property] = element.offsetWidth + 'px';
} else if (property === 'height') {
this[property] = element.offsetHeight + 'px';
} else if (property === 'styleFloat') {
this.float = currentStyle[property];
} else if (/margin.|padding.|border.+W/.test(property) && this[property] !== 'auto') {
this[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px';
} else if (/^outline/.test(property)) {
// errors on checking outline
try {
this[property] = currentStyle[property];
} catch (error) {
this.outlineColor = currentStyle.color;
this.outlineStyle = this.outlineStyle || 'none';
this.outlineWidth = this.outlineWidth || '0px';
this.outline = [this.outlineColor, this.outlineWidth, this.outlineStyle].join(' ');
}
} else {
this[property] = currentStyle[property];
}
}

setShortStyleProperty(this, 'margin');
setShortStyleProperty(this, 'padding');
setShortStyleProperty(this, 'border');

this.fontSize = Math.round(fontSize) + 'px';
}

CSSStyleDeclaration.prototype = {
constructor: CSSStyleDeclaration,
// <CSSStyleDeclaration>.getPropertyPriority
getPropertyPriority: function() {
throw new Error('NotSupportedError: DOM Exception 9');
},
// <CSSStyleDeclaration>.getPropertyValue
getPropertyValue: function(property) {
return this[property.replace(/-\w/g, function(match) {
return match[1].toUpperCase();
})];
},
// <CSSStyleDeclaration>.item
item: function(index) {
return this[index];
},
// <CSSStyleDeclaration>.removeProperty
removeProperty: function() {
throw new Error('NoModificationAllowedError: DOM Exception 7');
},
// <CSSStyleDeclaration>.setProperty
setProperty: function() {
throw new Error('NoModificationAllowedError: DOM Exception 7');
},
// <CSSStyleDeclaration>.getPropertyCSSValue
getPropertyCSSValue: function() {
throw new Error('NotSupportedError: DOM Exception 9');
}
};

// <Global>.getComputedStyle
exports.getComputedStyle = function getComputedStyle(element) {
return new CSSStyleDeclaration(element);
};
84 changes: 84 additions & 0 deletions lib/browser/client-scripts/polyfills/matchMedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Adapted from: https://raw.githubusercontent.com/Financial-Times/polyfill-service
*/
function evalQuery(query) {
/* jshint evil: true */
query = (query || 'true')
.replace(/^only\s+/, '')
.replace(/(device)-([\w.]+)/g, '$1.$2')
.replace(/([\w.]+)\s*:/g, 'media.$1 ===')
.replace(/min-([\w.]+)\s*===/g, '$1 >=')
.replace(/max-([\w.]+)\s*===/g, '$1 <=')
.replace(/all|screen/g, '1')
.replace(/print/g, '0')
.replace(/,/g, '||')
.replace(/\band\b/g, '&&')
.replace(/dpi/g, '')
.replace(/(\d+)(cm|em|in|dppx|mm|pc|pt|px|rem)/g, function($0, $1, $2) {
return $1 * (
$2 === 'cm' ? 0.3937 * 96 : (
$2 === 'em' || $2 === 'rem' ? 16 : (
$2 === 'in' || $2 === 'dppx' ? 96 : (
$2 === 'mm' ? 0.3937 * 96 / 10 : (
$2 === 'pc' ? 12 * 96 / 72 : (
$2 === 'pt' ? 96 / 72 : 1
)
)
)
)
)
);
});
return new Function('media', 'try{ return !!(%s) }catch(e){ return false }'
.replace('%s', query)
)({
width: global.innerWidth,
height: global.innerHeight,
orientation: global.orientation || 'landscape',
device: {
width: global.screen.width,
height: global.screen.height,
orientation: global.screen.orientation || global.orientation || 'landscape'
}
});
}

function MediaQueryList() {
this.matches = false;
this.media = 'invalid';
}

MediaQueryList.prototype.addListener = function addListener(listener) {
this.addListener.listeners.push(listener);
};

MediaQueryList.prototype.removeListener = function removeListener(listener) {
this.addListener.listeners.splice(this.addListener.listeners.indexOf(listener), 1);
};

// <Global>.matchMedia
exports.matchMedia = function matchMedia(query) {
var
list = new MediaQueryList();

if (arguments.length === 0) {
throw new TypeError('Not enough arguments to matchMedia');
}

list.media = String(query);
list.matches = evalQuery(list.media);
list.addListener.listeners = [];

window.addEventListener('resize', function() {
var listeners = [].concat(list.addListener.listeners), matches = evalQuery(list.media);

if (matches !== list.matches) {
list.matches = matches;
for (var index = 0, length = listeners.length; index < length; ++index) {
listeners[index].call(global, list);
}
}
});

return list;
};
Loading

0 comments on commit 2b71a19

Please sign in to comment.