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

Commit 364bcbf

Browse files
Sergey TatarintsevSevInf
authored andcommitted
fix: remove polyfill-service dependency
Fixes a lot of warnings regarding outdated dependecies and allows to install gemini with yarn. Close #673, close #635
1 parent 4ea13c4 commit 364bcbf

File tree

13 files changed

+292
-77
lines changed

13 files changed

+292
-77
lines changed

lib/browser/client-scripts/gemini.calibrate.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@
3131
bodyStyle.backgroundColor = '#96fa00';
3232
}
3333

34+
function hasCSS3Selectors() {
35+
try {
36+
document.querySelector('body:nth-child(1)');
37+
} catch (e) {
38+
return false;
39+
}
40+
return true;
41+
}
42+
43+
function needsCompatLib() {
44+
return !hasCSS3Selectors() ||
45+
!window.getComputedStyle ||
46+
!window.matchMedia ||
47+
!String.prototype.trim;
48+
}
49+
3450
function getBrowserFeatures() {
3551
var features = {
36-
hasCSS3Selectors: true,
52+
needsCompatLib: needsCompatLib(),
3753
pixelRatio: window.devicePixelRatio,
3854
innerWidth: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
3955
};
40-
try {
41-
document.querySelector('body:nth-child(1)');
42-
} catch (e) {
43-
features.hasCSS3Selectors = false;
44-
}
4556

4657
return features;
4758
}

lib/browser/client-scripts/gemini.coverage.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var util = require('./util'),
44
coverageLevel = require('../../coverage-level'),
55
rect = require('./rect'),
6-
query = require('./query');
6+
lib = require('./lib');
77

88
exports.collectCoverage = function collectCoverage(rect) {
99
var coverage = {},
@@ -49,7 +49,7 @@ function coverageForRule(rule, area, ctx) {
4949
if (rule.cssRules || rule.styleSheet) {
5050
if (rule.conditionText) {
5151
ctx.media++;
52-
if (!window.matchMedia(rule.conditionText).matches) {
52+
if (!lib.matchMedia(rule.conditionText).matches) {
5353
return;
5454
}
5555
}
@@ -67,17 +67,17 @@ function coverageForRule(rule, area, ctx) {
6767
}
6868

6969
util.each(rule.selectorText.split(','), function(selector) {
70-
selector = selector.trim();
70+
selector = lib.trim(selector);
7171
var coverage = coverageLevel.NONE,
72-
matches = query.all(selector);
72+
matches = lib.queryAll(selector);
7373

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

7979
if (newSelector.length > 0) {
80-
matches = query.all(newSelector);
80+
matches = lib.queryAll(newSelector);
8181
}
8282
}
8383

lib/browser/client-scripts/gemini.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
var util = require('./util'),
55
rect = require('./rect'),
6-
query = require('./query'),
6+
lib = require('./lib'),
77
Rect = rect.Rect;
88

99
if (typeof window === 'undefined') {
@@ -12,7 +12,7 @@ if (typeof window === 'undefined') {
1212
window.__gemini = exports;
1313
}
1414

15-
exports.query = query;
15+
exports.queryFirst = lib.queryFirst;
1616

1717
// Terminology
1818
// - clientRect - the result of calling getBoundingClientRect of the element
@@ -77,19 +77,19 @@ function prepareScreenshotUnsafe(selectors, opts) {
7777
}
7878

7979
exports.resetZoom = function() {
80-
var meta = query.first('meta[name="viewport"]');
80+
var meta = lib.queryFirst('meta[name="viewport"]');
8181
if (!meta) {
8282
meta = document.createElement('meta');
8383
meta.name = 'viewport';
84-
query.first('head').appendChild(meta);
84+
lib.queryFirst('head').appendChild(meta);
8585
}
8686
meta.content = 'width=device-width,initial-scale=1.0,user-scalable=no';
8787
};
8888

8989
function getCaptureRect(selectors) {
9090
var element, elementRect, rect;
9191
for (var i = 0; i < selectors.length; i++) {
92-
element = query.first(selectors[i]);
92+
element = lib.queryFirst(selectors[i]);
9393
if (!element) {
9494
return {
9595
error: 'NOTFOUND',
@@ -114,7 +114,7 @@ function getCaptureRect(selectors) {
114114
function findIgnoreAreas(selectors) {
115115
var result = [];
116116
util.each(selectors, function(selector) {
117-
var element = query.first(selector);
117+
var element = lib.queryFirst(selector);
118118
if (element) {
119119
var rect = getElementCaptureRect(element);
120120
if (rect) {
@@ -136,7 +136,7 @@ function isHidden(css, clientRect) {
136136

137137
function getElementCaptureRect(element) {
138138
var pseudo = [':before', ':after'],
139-
css = window.getComputedStyle(element),
139+
css = lib.getComputedStyle(element),
140140
clientRect = rect.getAbsoluteClientRect(element);
141141

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

148148
util.each(pseudo, function(pseudoEl) {
149-
css = window.getComputedStyle(element, pseudoEl);
149+
css = lib.getComputedStyle(element, pseudoEl);
150150
elementRect = elementRect.merge(getExtRect(css, clientRect));
151151
});
152152

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
/*jshint newcap:false*/
3+
var Sizzle = require('sizzle');
4+
5+
exports.queryFirst = function(selector) {
6+
var elems = Sizzle(exports.trim(selector) + ':first');
7+
return elems.length > 0 ? elems[0] : null;
8+
};
9+
10+
exports.queryAll = function(selector) {
11+
return Sizzle(selector);
12+
};
13+
14+
exports.trim = function(str) {
15+
// trim spaces, unicode BOM and NBSP and the beginning and the end of the line
16+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
17+
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
18+
};
19+
20+
exports.getComputedStyle = require('./polyfills/getComputedStyle').getComputedStyle;
21+
exports.matchMedia = require('./polyfills/matchMedia').matchMedia;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
exports.queryFirst = function(selector) {
4+
return document.querySelector(selector);
5+
};
6+
7+
exports.queryAll = function(selector) {
8+
return document.querySelectorAll(selector);
9+
};
10+
11+
exports.getComputedStyle = function(element, pseudoElement) {
12+
return getComputedStyle(element, pseudoElement);
13+
};
14+
15+
exports.matchMedia = function(mediaQuery) {
16+
return matchMedia(mediaQuery);
17+
};
18+
19+
exports.trim = function(str) {
20+
return str.trim();
21+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Financial Times
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Adapted from: https://raw.githubusercontent.com/Financial-Times/polyfill-service
3+
*/
4+
function getComputedStylePixel(element, property, fontSize) {
5+
var
6+
// Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed.
7+
value = element.document && element.currentStyle[property].match(/([\d\.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''],
8+
size = value[1],
9+
suffix = value[2],
10+
rootSize;
11+
12+
fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16;
13+
rootSize = property === 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
14+
15+
return suffix === '%' ? size / 100 * rootSize :
16+
suffix === 'cm' ? size * 0.3937 * 96 :
17+
suffix === 'em' ? size * fontSize :
18+
suffix === 'in' ? size * 96 :
19+
suffix === 'mm' ? size * 0.3937 * 96 / 10 :
20+
suffix === 'pc' ? size * 12 * 96 / 72 :
21+
suffix === 'pt' ? size * 96 / 72 :
22+
size;
23+
}
24+
25+
function setShortStyleProperty(style, property) {
26+
var
27+
borderSuffix = property === 'border' ? 'Width' : '',
28+
t = property + 'Top' + borderSuffix,
29+
r = property + 'Right' + borderSuffix,
30+
b = property + 'Bottom' + borderSuffix,
31+
l = property + 'Left' + borderSuffix;
32+
33+
style[property] = (style[t] === style[r] && style[t] === style[b] && style[t] === style[l] ? [style[t]] :
34+
style[t] === style[b] && style[l] === style[r] ? [style[t], style[r]] :
35+
style[l] === style[r] ? [style[t], style[r], style[b]] :
36+
[style[t], style[r], style[b], style[l]]).join(' ');
37+
}
38+
39+
// <CSSStyleDeclaration>
40+
function CSSStyleDeclaration(element) {
41+
var currentStyle = element.currentStyle,
42+
fontSize = getComputedStylePixel(element, 'fontSize'),
43+
unCamelCase = function(match) {
44+
return '-' + match.toLowerCase();
45+
},
46+
property;
47+
48+
for (property in currentStyle) {
49+
Array.prototype.push.call(this, property === 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase));
50+
51+
if (property === 'width') {
52+
this[property] = element.offsetWidth + 'px';
53+
} else if (property === 'height') {
54+
this[property] = element.offsetHeight + 'px';
55+
} else if (property === 'styleFloat') {
56+
this.float = currentStyle[property];
57+
} else if (/margin.|padding.|border.+W/.test(property) && this[property] !== 'auto') {
58+
this[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px';
59+
} else if (/^outline/.test(property)) {
60+
// errors on checking outline
61+
try {
62+
this[property] = currentStyle[property];
63+
} catch (error) {
64+
this.outlineColor = currentStyle.color;
65+
this.outlineStyle = this.outlineStyle || 'none';
66+
this.outlineWidth = this.outlineWidth || '0px';
67+
this.outline = [this.outlineColor, this.outlineWidth, this.outlineStyle].join(' ');
68+
}
69+
} else {
70+
this[property] = currentStyle[property];
71+
}
72+
}
73+
74+
setShortStyleProperty(this, 'margin');
75+
setShortStyleProperty(this, 'padding');
76+
setShortStyleProperty(this, 'border');
77+
78+
this.fontSize = Math.round(fontSize) + 'px';
79+
}
80+
81+
CSSStyleDeclaration.prototype = {
82+
constructor: CSSStyleDeclaration,
83+
// <CSSStyleDeclaration>.getPropertyPriority
84+
getPropertyPriority: function() {
85+
throw new Error('NotSupportedError: DOM Exception 9');
86+
},
87+
// <CSSStyleDeclaration>.getPropertyValue
88+
getPropertyValue: function(property) {
89+
return this[property.replace(/-\w/g, function(match) {
90+
return match[1].toUpperCase();
91+
})];
92+
},
93+
// <CSSStyleDeclaration>.item
94+
item: function(index) {
95+
return this[index];
96+
},
97+
// <CSSStyleDeclaration>.removeProperty
98+
removeProperty: function() {
99+
throw new Error('NoModificationAllowedError: DOM Exception 7');
100+
},
101+
// <CSSStyleDeclaration>.setProperty
102+
setProperty: function() {
103+
throw new Error('NoModificationAllowedError: DOM Exception 7');
104+
},
105+
// <CSSStyleDeclaration>.getPropertyCSSValue
106+
getPropertyCSSValue: function() {
107+
throw new Error('NotSupportedError: DOM Exception 9');
108+
}
109+
};
110+
111+
// <Global>.getComputedStyle
112+
exports.getComputedStyle = function getComputedStyle(element) {
113+
return new CSSStyleDeclaration(element);
114+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Adapted from: https://raw.githubusercontent.com/Financial-Times/polyfill-service
3+
*/
4+
function evalQuery(query) {
5+
/* jshint evil: true */
6+
query = (query || 'true')
7+
.replace(/^only\s+/, '')
8+
.replace(/(device)-([\w.]+)/g, '$1.$2')
9+
.replace(/([\w.]+)\s*:/g, 'media.$1 ===')
10+
.replace(/min-([\w.]+)\s*===/g, '$1 >=')
11+
.replace(/max-([\w.]+)\s*===/g, '$1 <=')
12+
.replace(/all|screen/g, '1')
13+
.replace(/print/g, '0')
14+
.replace(/,/g, '||')
15+
.replace(/\band\b/g, '&&')
16+
.replace(/dpi/g, '')
17+
.replace(/(\d+)(cm|em|in|dppx|mm|pc|pt|px|rem)/g, function($0, $1, $2) {
18+
return $1 * (
19+
$2 === 'cm' ? 0.3937 * 96 : (
20+
$2 === 'em' || $2 === 'rem' ? 16 : (
21+
$2 === 'in' || $2 === 'dppx' ? 96 : (
22+
$2 === 'mm' ? 0.3937 * 96 / 10 : (
23+
$2 === 'pc' ? 12 * 96 / 72 : (
24+
$2 === 'pt' ? 96 / 72 : 1
25+
)
26+
)
27+
)
28+
)
29+
)
30+
);
31+
});
32+
return new Function('media', 'try{ return !!(%s) }catch(e){ return false }'
33+
.replace('%s', query)
34+
)({
35+
width: global.innerWidth,
36+
height: global.innerHeight,
37+
orientation: global.orientation || 'landscape',
38+
device: {
39+
width: global.screen.width,
40+
height: global.screen.height,
41+
orientation: global.screen.orientation || global.orientation || 'landscape'
42+
}
43+
});
44+
}
45+
46+
function MediaQueryList() {
47+
this.matches = false;
48+
this.media = 'invalid';
49+
}
50+
51+
MediaQueryList.prototype.addListener = function addListener(listener) {
52+
this.addListener.listeners.push(listener);
53+
};
54+
55+
MediaQueryList.prototype.removeListener = function removeListener(listener) {
56+
this.addListener.listeners.splice(this.addListener.listeners.indexOf(listener), 1);
57+
};
58+
59+
// <Global>.matchMedia
60+
exports.matchMedia = function matchMedia(query) {
61+
var
62+
list = new MediaQueryList();
63+
64+
if (arguments.length === 0) {
65+
throw new TypeError('Not enough arguments to matchMedia');
66+
}
67+
68+
list.media = String(query);
69+
list.matches = evalQuery(list.media);
70+
list.addListener.listeners = [];
71+
72+
window.addEventListener('resize', function() {
73+
var listeners = [].concat(list.addListener.listeners), matches = evalQuery(list.media);
74+
75+
if (matches !== list.matches) {
76+
list.matches = matches;
77+
for (var index = 0, length = listeners.length; index < length; ++index) {
78+
listeners[index].call(global, list);
79+
}
80+
}
81+
});
82+
83+
return list;
84+
};

0 commit comments

Comments
 (0)