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

Viewport test: accept initial-scale and allow spaces #1267

Merged
merged 6 commits into from
Jan 12, 2017

Conversation

dracos
Copy link
Contributor

@dracos dracos commented Dec 23, 2016

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

@wardpeet
Copy link
Collaborator

wardpeet commented Dec 26, 2016

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.

@dracos
Copy link
Contributor Author

dracos commented Dec 26, 2016

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', () => {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@ebidel ebidel changed the title Allow initial-scale and spaces in viewport test. Viewport test: accept initial-scale and allow spaces Dec 30, 2016
@ebidel ebidel changed the title Viewport test: accept initial-scale and allow spaces Viewport test: accept initial-scale and allow spaces values Dec 30, 2016
@ebidel ebidel changed the title Viewport test: accept initial-scale and allow spaces values Viewport test: accept initial-scale and allow spaces Dec 30, 2016
@paulirish
Copy link
Member

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!

@googlebot
Copy link

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 cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

Copy link
Member

@paulirish paulirish left a 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

@ebidel
Copy link
Contributor

ebidel commented Jan 4, 2017

@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.
@googlebot
Copy link

CLAs look good, thanks!

@dracos
Copy link
Contributor Author

dracos commented Jan 4, 2017

@paulirish Thanks for that! Merged and squashed.
@ebidel Fixed :)

@paulirish
Copy link
Member

@ebidel can you take another look?

@ebidel
Copy link
Contributor

ebidel commented Jan 4, 2017

Nice changes. A few more things after the change.

artifacts.Viewport.includes('width=');
if (typeof artifacts.Viewport !== 'string') {
return Viewport.generateAuditResult({
rawValue: false
Copy link
Contributor

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

see https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/audits/dobetterweb/no-datenow.js#L57-L59

}

const viewportProps = Parser.parseMetaViewPortContent(artifacts.Viewport).validProperties;
const hasMobileViewport = viewportProps['width'] || viewportProps['initial-scale'];
Copy link
Contributor

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',
Copy link
Contributor

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;
Copy link
Contributor

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.
@dracos
Copy link
Contributor Author

dracos commented Jan 4, 2017

All done!

debugString
});

if (typeof artifacts.Viewport !== 'string') {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops!

@paulirish paulirish dismissed their stale review January 4, 2017 18:12

because pippi longstocking

@@ -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);
Copy link
Contributor

@ebidel ebidel Jan 4, 2017

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid!

Copy link
Member

@brendankenny brendankenny left a 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}`;
Copy link
Member

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);
});

Copy link
Member

@brendankenny brendankenny Jan 4, 2017

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?

Copy link
Contributor Author

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice solution :)

Copy link
Member

@brendankenny brendankenny left a 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!

@brendankenny brendankenny merged commit 4514a73 into GoogleChrome:master Jan 12, 2017
andrewrota pushed a commit to andrewrota/lighthouse that referenced this pull request Jan 13, 2017
)

Introduce `metaviewport-parser` module for better parsing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants