-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Viewport test: accept initial-scale and allow spaces #1267
Conversation
thanks for this one :) can you add a test case for this as well. Not sure we should only test for width or initial-scale though. |
Done! Whilst I agree with your comment elsewhere it probably wants to check for more (though for accessibility, I don’t think it can just be ‘any valid value’ e.g. if it was just maximum-scale!), I think this would hopefully count as a good incremental change prior to that happening, rather than waiting for it to be fully decided :) |
@@ -38,4 +38,16 @@ describe('Mobile-friendly: viewport audit', () => { | |||
Viewport: 'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1' | |||
}).rawValue, true); | |||
}); | |||
|
|||
it('passes when a viewport with spaces is provided', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of separate tests, what if we expand the "'passes when a viewport is provided" test with a few permutations. Could just rename it to 'passes when a valid viewport is provided'. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m happy either way, whichever would better fit your existing tests; I assume a failure would be as verbose either way. Pushed change.
http://andreasbovens.github.io/understanding-viewport/ is a good resource for understanding various viewports and how phones handle them. it looks like just initial-scale is satisfactory for this test, yup. Nice! |
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the regex approach is a bit funky.
Should we just graduate to a parser? I discovered https://www.npmjs.com/package/metaviewport-parser which seems good since the spec's parsing language is a bit wild: https://drafts.csswg.org/css-device-adapt/#parsing-algorithm
@dracos I put up a PR to adopt this package into your PR.. let me know what you think!
dracos#1
@dracos There's also a line length issue after my rebase fix. If you can take care of it, that would be splendid :) |
Use of metaviewport-parser package added by Paul Irish.
606b1e9
to
bd2353c
Compare
CLAs look good, thanks! |
@paulirish Thanks for that! Merged and squashed. |
@ebidel can you take another look? |
Nice changes. A few more things after the change. |
artifacts.Viewport.includes('width='); | ||
if (typeof artifacts.Viewport !== 'string') { | ||
return Viewport.generateAuditResult({ | ||
rawValue: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an error case:
debugString: 'Error in determining viewport',
rawValue: -1
} | ||
|
||
const viewportProps = Parser.parseMetaViewPortContent(artifacts.Viewport).validProperties; | ||
const hasMobileViewport = viewportProps['width'] || viewportProps['initial-scale']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dot notation where you can: viewportProps.width
const viewports = [ | ||
'width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1', | ||
'width = device-width, initial-scale = 1', | ||
'initial-scale=1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's include one with just width
as well.
}); | ||
} | ||
|
||
const viewportProps = Parser.parseMetaViewPortContent(artifacts.Viewport).validProperties; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that we have a parser, it might be nice to also warn if there are unknownProperties
or invalidValues
values. If that's the case, add a debugString to the artifact.
let debugString = '';
const parsedProps = Parser.parseMetaViewPortContent(artifacts.Viewport);
if (Object.keys(parsedProps.unknownProperties).length) {
debugString += `Invalid properties found: ${parsedProps.unknownProperties}`;
}
if (Object.keys(parsedProps.invalidValues).length) {
debugString += `Invalid values found: ${parsedProps.invalidValues}`;
}
const viewportProps = parsedProps.validProperties;
const hasMobileViewport = viewportProps.width || viewportProps['initial-scale'];
Viewport.generateAuditResult({
rawValue: !!hasMobileViewport
debugString
});
Fixes from Eric Bidelman's review, plus his code to add invalid entries to debugString.
All done! |
debugString | ||
}); | ||
|
||
if (typeof artifacts.Viewport !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops. this is duplicated from the top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops!
@@ -24,7 +24,7 @@ describe('Mobile-friendly: viewport audit', () => { | |||
it('fails when no input present', () => { | |||
return assert.equal(Audit.audit({ | |||
Viewport: -1 | |||
}).rawValue, false); | |||
}).rawValue, -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry! One more thing. Can you add a test that checks that there's a debugString
present for this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two last things
debugString += `Invalid properties found: ${parsedProps.unknownProperties}`; | ||
} | ||
if (Object.keys(parsedProps.invalidValues).length) { | ||
debugString += `Invalid values found: ${parsedProps.invalidValues}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this debugString probably needs some separator between the two parts in case there are invalid properties and invalid values
@@ -33,9 +35,17 @@ describe('Mobile-friendly: viewport audit', () => { | |||
}).rawValue, false); | |||
}); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a better failure case than this old test of just not having a viewport meta tag? e.g. a non-mobile friendly but valid viewport
Also is it worth adding a test for an invalid property and/or value and test to make sure it's in the debugString
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some more tests for this.
Used JSON.stringify to print out the invalid property/values, sorry if there's a better way of doing this that I missed!
if (Object.keys(parsedProps.invalidValues).length) { | ||
debugString += `Invalid values found: ${JSON.stringify(parsedProps.invalidValues)}. `; | ||
} | ||
debugString = debugString.trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice solution :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I was so slow on this. Looks great!
Fixes #641. and comments in it; allow a viewport to pass if it has width or initial-scale, and tweak the displayed error message to say this. It also allows spaces in the viewport elements, they are explicitly given in an example on e.g. https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html#//apple_ref/doc/uid/TP40008193