Skip to content

Commit

Permalink
fix(meta-viewport): parse negative and string values for `maxim… (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy committed Mar 31, 2020
1 parent 3ab9f21 commit 8c92472
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
50 changes: 32 additions & 18 deletions lib/checks/mobile/meta-viewport-scale.js
@@ -1,22 +1,33 @@
options = options || {};
var params,
content = node.getAttribute('content') || '',
parsedParams = content.split(/[;,]/),
result = {},
minimum = options.scaleMinimum || 2,
lowerBound = options.lowerBound || false;

for (var i = 0, l = parsedParams.length; i < l; i++) {
params = parsedParams[i].split('=');
var key = params.shift().toLowerCase();
if (key && params.length) {
result[key.trim()] = params
.shift()
.trim()
.toLowerCase();
}
const { scaleMinimum = 2, lowerBound = false } = options || {};

const content = node.getAttribute('content') || '';
if (!content) {
return true;
}

const result = content.split(/[;,]/).reduce((out, item) => {
const contentValue = item.trim();
if (!contentValue) {
return out;
}

const [key, value] = contentValue.split('=');
const curatedKey = key.toLowerCase().trim();
let curatedValue = value.toLowerCase().trim();

// convert `yes` to `1`
if (curatedKey === 'maximum-scale' && curatedValue === 'yes') {
curatedValue = 1;
}
// when negative ignore key
if (curatedKey === 'maximum-scale' && parseFloat(curatedValue) < 0) {
return out;
}

out[curatedKey] = curatedValue;
return out;
}, {});

if (
lowerBound &&
result['maximum-scale'] &&
Expand All @@ -30,7 +41,10 @@ if (!lowerBound && result['user-scalable'] === 'no') {
return false;
}

if (result['maximum-scale'] && parseFloat(result['maximum-scale']) < minimum) {
if (
result['maximum-scale'] &&
parseFloat(result['maximum-scale']) < scaleMinimum
) {
this.data('maximum-scale');
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/checks/mobile/meta-viewport-scale.js
Expand Up @@ -34,6 +34,18 @@ describe('meta-viewport', function() {
assert.isTrue(checks['meta-viewport'].evaluate(node));
});

it('should return false on maximum-scale=yes (translates to 1)', function() {
fixture.innerHTML = '<meta name="viewport" content="maximum-scale=yes">';
var node = fixture.querySelector('meta');
assert.isFalse(checks['meta-viewport'].evaluate.call(checkContext, node));
});

it('should return true on negative maximum scale (should be ignored)', function() {
fixture.innerHTML = '<meta name="viewport" content="maximum-scale=-1">';
var node = fixture.querySelector('meta');
assert.isTrue(checks['meta-viewport'].evaluate.call(checkContext, node));
});

it('should return true if maximum-scale >= options.scaleMinimum', function() {
fixture.innerHTML =
'<meta name="viewport" content="foo=bar, maximum-scale=5, cats=dogs">';
Expand Down

0 comments on commit 8c92472

Please sign in to comment.