Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compute orientation lock from various transformation functions #1937

Merged
merged 6 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 32 additions & 33 deletions lib/checks/mobile/css-orientation-lock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global context */
const { cssom = undefined } = context || {};
const { degreeThreshold = 0 } = options || {};
if (!cssom || !cssom.length) {
return undefined;
}
Expand All @@ -10,7 +11,7 @@ const rulesGroupByDocumentFragment = groupCssomByDocument(cssom);

for (const key of Object.keys(rulesGroupByDocumentFragment)) {
const { root, rules } = rulesGroupByDocumentFragment[key];
const orientationRules = getOrientaionCssMediaFeatureRules(rules);
const orientationRules = rules.filter(isMediaRuleWithOrientation);
if (!orientationRules.length) {
continue;
}
Expand All @@ -26,8 +27,7 @@ for (const key of Object.keys(rulesGroupByDocumentFragment)) {
relatedElements = relatedElements.concat(elms);
}

// set locked boolean
isLocked = locked;
isLocked = isLocked || locked;
});
});
}
Expand Down Expand Up @@ -65,34 +65,27 @@ function groupCssomByDocument(cssObjectModel) {
}

/**
* Get CSS Rules that target Orientation CSS Media Features
* Filter CSS Rules that target Orientation CSS Media Features
* @param {Array<Object>} cssRules
* @returns {Array<Object>}
*/
function getOrientaionCssMediaFeatureRules(cssRules) {
function isMediaRuleWithOrientation({ type, cssText }) {
/**
* Filter:
* CSSRule.MEDIA_Rule
* -> https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule
*/
if (type !== 4) {
return false;
}

/**
* Filter:
* CSSRule with conditionText of `orientation`
*/
return (
cssRules
/**
* Filter:
* CSSRule.MEDIA_Rule
* -> https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule
*/
.filter(({ type }) => type === 4)
/**
* Filter:
* CSSRule with conditionText of `orientation`
*/
.filter(({ cssText }) => {
return (
/orientation:\s*landscape/i.test(cssText) ||
/orientation:\s*portrait/i.test(cssText)
);
})
/**
* Filter:
* Ensure RULELIST is not empty
*/
.filter(({ cssRules }) => cssRules.length > 0)
/orientation:\s*landscape/i.test(cssText) ||
/orientation:\s*portrait/i.test(cssText)
);
}

Expand Down Expand Up @@ -130,8 +123,14 @@ function getIsOrientationLocked({ selectorText, style }) {
return false;
}

const locked = degrees % 90 === 0 && degrees % 180 !== 0;
return !!locked;
/**
* When degree is a multiple of 180, it is not considered an orientation lock
*/
if (Math.abs(degrees) % 180 <= degreeThreshold) {
jeeyyy marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

return Math.abs(degrees) % 90 <= degreeThreshold;
}

/**
Expand Down Expand Up @@ -168,7 +167,7 @@ function getRotationInDegrees(transformFunction, transformFnValue) {
* @returns{Number|undefined}
*/
function getAngleInDegrees(angleWithUnit) {
const [unit] = angleWithUnit.match(/(deg|grad|rad|turn)/);
const [unit] = angleWithUnit.match(/(deg|grad|rad|turn)/) || [];
if (!unit) {
return;
}
Expand Down Expand Up @@ -223,7 +222,7 @@ function getAngleInDegreesFromMatrixTransform(transformFnValue) {
* @return {Number}
*/
function convertRadToDeg(radians) {
return parseInt(radians * (180 / Math.PI));
return Math.round(radians * (180 / Math.PI));
}

/**
Expand All @@ -237,7 +236,7 @@ function convertGradToDeg(grad) {
if (grad < 0) {
grad += 400;
}
return parseInt((grad / 400) * 360);
return Math.round((grad / 400) * 360);
}

/**
Expand All @@ -247,5 +246,5 @@ function convertGradToDeg(grad) {
* @returns {Number}
*/
function convertTurnToDeg(turn) {
return parseInt(360 / (1 / turn));
return Math.round(360 / (1 / turn));
}
3 changes: 3 additions & 0 deletions lib/checks/mobile/css-orientation-lock.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"id": "css-orientation-lock",
"evaluate": "css-orientation-lock.js",
"options": {
"degreeThreshold": 2
},
"metadata": {
"impact": "serious",
"messages": {
Expand Down