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

Validator 20191218 #26084

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ const forbiddenTermsSrcInclusive = {
'testing/local-amp-chrome-extension/background.js',
'tools/errortracker/errortracker.go',
'tools/experiments/experiments.js',
'validator/engine/validator.js',
'validator/engine/validator-in-browser.js',
'validator/nodejs/index.js',
'validator/webui/serve-standalone.go',
Expand Down
156 changes: 154 additions & 2 deletions validator/engine/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,86 @@ class ExtensionsContext {
}
}

// If any script in the page uses LTS, all scripts must use LTS. This is used to
// record when a script is seen and validate following script tags.
/** @enum {string} */
const ScriptReleaseVersion = {
UNKNOWN: 'UNKNOWN',
STANDARD: 'STANDARD',
LTS: 'LTS',
};

/**
* Gets the name attribute for an extension script tag.
* @param {!amp.htmlparser.ParsedHtmlTag} tag
* @return {string}
*/
function ExtensionScriptNameAttribute(tag) {
if (tag.upperName() == 'SCRIPT') {
for (const attribute
of ['custom-element', 'custom-template', 'host-service']) {
if (attribute in tag.attrsByKey()) {
return attribute;
}
}
}
return '';
}

/**
* Gets the extension name for an extension script tag.
* @param {!amp.htmlparser.ParsedHtmlTag} tag
* @return {string}
*/
function ExtensionScriptName(tag) {
const nameAttr = ExtensionScriptNameAttribute(tag);
if (nameAttr) {
// Extension script names are required to be in lowercase by the validator,
// so we don't need to lowercase them here.
return tag.attrsByKey()[nameAttr] || '';
}
return '';
}

/**
* Tests if a tag is an extension script tag.
* @param {!amp.htmlparser.ParsedHtmlTag} tag
* @return {boolean}
*/
function IsExtensionScript(tag) {
return !!ExtensionScriptNameAttribute(tag);
}

/**
* Tests if a tag is an async script tag.
* @param {!amp.htmlparser.ParsedHtmlTag} tag
* @return {boolean}
*/
function IsAsyncScriptTag(tag) {
return tag.upperName() == 'SCRIPT' && 'async' in tag.attrsByKey() &&
'src' in tag.attrsByKey();
}

/**
* Tests if a tag is the AMP runtime script tag.
* @param {!amp.htmlparser.ParsedHtmlTag} tag
* @return {boolean}
*/
function IsAmpRuntimeScript(tag) {
const src = tag.attrsByKey()['src'] || '';
return IsAsyncScriptTag(tag) && !IsExtensionScript(tag) &&
src.startsWith('https://cdn.ampproject.org/') && src.endsWith('/v0.js');
}

/**
* Tests if a URL is for the LTS version of a script.
* @param {string} url
* @return {boolean}
*/
function IsLtsScriptUrl(url) {
return url.startsWith('https://cdn.ampproject.org/lts/');
}

/**
* The Context keeps track of the line / column that the validator is
* in, as well as the mandatory tag specs that have already been validated.
Expand Down Expand Up @@ -2501,6 +2581,13 @@ class Context {
* @private
*/
this.extensions_ = new ExtensionsContext();

/**
* Flag for if the LTS runtime engine is present.
* @type {!ScriptReleaseVersion}
* @private
*/
this.scriptReleaseVersion_ = ScriptReleaseVersion.UNKNOWN;
}

/** @return {!ParsedValidatorRules} */
Expand Down Expand Up @@ -2617,6 +2704,22 @@ class Context {
}
}

/**
* Record if this document contains a tag requesting the LTS runtime engine.
* @param {!amp.htmlparser.ParsedHtmlTag} parsedTag
* @param {!amp.validator.ValidationResult} result
* @private
*/
recordScriptReleaseVersionFromTagResult_(parsedTag, result) {
if (this.getScriptReleaseVersion() === ScriptReleaseVersion.UNKNOWN &&
(IsExtensionScript(parsedTag) || IsAmpRuntimeScript(parsedTag))) {
const src = parsedTag.attrsByKey()['src'] || '';
this.scriptReleaseVersion_ = IsLtsScriptUrl(src) ?
ScriptReleaseVersion.LTS :
ScriptReleaseVersion.STANDARD;
}
}

/**
* @param {!amp.validator.ValueSetProvision} provision
* @return {string} A key for valueSetsProvided_ and valueSetsRequired_.
Expand All @@ -2642,6 +2745,8 @@ class Context {
this.recordAttrRequiresExtension_(encounteredTag, tagResult);
this.updateFromTagResult_(referencePointResult);
this.updateFromTagResult_(tagResult);
this.recordScriptReleaseVersionFromTagResult_(
encounteredTag, tagResult.validationResult);
}

/**
Expand Down Expand Up @@ -2881,6 +2986,11 @@ class Context {
getEncounteredBodyLineCol() {
return /** @type {!LineCol} */ (this.encounteredBodyLineCol_);
}

/** @return {!ScriptReleaseVersion} */
getScriptReleaseVersion() {
return this.scriptReleaseVersion_;
}
}

/**
Expand Down Expand Up @@ -4240,7 +4350,7 @@ function validateAttributeInExtension(tagSpec, context, attr, result) {
return true;
} else if (attr.name === 'src') {
const srcUrlRe =
/^https:\/\/cdn\.ampproject\.org\/v0\/(amp-[a-z0-9-]*)-([a-z0-9.]*)\.js$/;
/^https:\/\/cdn\.ampproject\.org(?:\/lts)?\/v0\/(amp-[a-z0-9-]*)-([a-z0-9.]*)\.js$/;
const reResult = srcUrlRe.exec(attr.value);
// If the src URL matches this regex and the base name of the file matches
// the extension, look to see if the version matches.
Expand Down Expand Up @@ -4269,6 +4379,38 @@ function validateAttributeInExtension(tagSpec, context, attr, result) {
return false;
}

/**
* Validates that LTS is used for either all script sources or none.
* @param {!Object} srcAttr
* @param {!amp.validator.TagSpec} tagSpec
* @param {!Context} context
* @param {!amp.validator.ValidationResult} result
*/
function validateScriptSrcAttr(srcAttr, tagSpec, context, result) {
if (context.getScriptReleaseVersion() === ScriptReleaseVersion.UNKNOWN)
return;

const scriptReleaseVersion = IsLtsScriptUrl(srcAttr.value) ?
ScriptReleaseVersion.LTS :
ScriptReleaseVersion.STANDARD;

if (context.getScriptReleaseVersion() != scriptReleaseVersion) {
const specName = tagSpec.extensionSpec !== null ?
tagSpec.extensionSpec.name :
tagSpec.specName;
context.addError(
scriptReleaseVersion == ScriptReleaseVersion.LTS ?
amp.validator.ValidationError.Code.LTS_SCRIPT_AFTER_NON_LTS :
amp.validator.ValidationError.Code.NON_LTS_SCRIPT_AFTER_LTS,
context.getLineCol(),
/*params=*/[specName],

'https://amp.dev/documentation/guides-and-tutorials/learn/spec/' +
'amphtml#required-markup',
result);
}
}

/**
* Helper method for ValidateAttributes.
* @param {!ParsedAttrSpec} parsedAttrSpec
Expand Down Expand Up @@ -4405,10 +4547,20 @@ function validateAttributes(
for (const attr of encounteredTag.attrs()) {
// For transformed AMP, attributes `class` and `i-amphtml-layout` are
// handled within validateSsrLayout for non-sizer elements.
if (context.isTransformed() && encounteredTag.lowerName() !== 'i-amphtml-sizer' &&
if (context.isTransformed() &&
encounteredTag.lowerName() !== 'i-amphtml-sizer' &&
(attr.name === 'class' || attr.name === 'i-amphtml-layout')) {
continue;
}

// If |spec| is the runtime or an extension script, validate that LTS is
// either used by all pages or no pages.
if (attr.name == 'src' &&
(IsExtensionScript(encounteredTag) ||
IsAmpRuntimeScript(encounteredTag))) {
validateScriptSrcAttr(attr, spec, context, result);
}

if (!(attr.name in attrsByName)) {
// The HTML tag specifies type identifiers which are validated in
// validateHtmlTag(), so we skip them here.
Expand Down
2 changes: 1 addition & 1 deletion validator/engine/validator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ describe('ValidatorRulesMakeSense', () => {
// getNameByAttrSpecId() looks like it would do what we want, but it's
// sufficiently wrapped in private context inside the validator that I
// don't see a way to call it. For now just gold the current index.
expect(tagSpec.attrLists[0]).toEqual(19);
expect(tagSpec.attrLists[0]).toEqual(20);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FAIL
| <style amp4email-boilerplate>body{visibility:hidden}</style>
| <script async src="https://cdn.ampproject.org/v0.js" nonce="disallowed"></script>
>> ^~~~~~~~~
amp4email_feature_tests/nonce_disallowed.html:26:2 The attribute 'nonce' may not appear in tag 'amphtml engine v0.js script'. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
amp4email_feature_tests/nonce_disallowed.html:26:2 The attribute 'nonce' may not appear in tag 'amphtml engine v0.js script [AMP4EMAIL,ACTIONS]'. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
| </head>
| <body>
| Hello, world.
Expand Down
4 changes: 2 additions & 2 deletions validator/testdata/feature_tests/empty.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ feature_tests/empty.html:1:0 The mandatory tag 'noscript > style[amp-boilerplate
>> ^~~~~~~~~
feature_tests/empty.html:1:0 The mandatory tag 'body' is missing or incorrect. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
>> ^~~~~~~~~
feature_tests/empty.html:1:0 The mandatory tag 'amphtml engine v0.js script' is missing or incorrect. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
>> ^~~~~~~~~
feature_tests/empty.html:1:0 The mandatory tag 'noscript enclosure for boilerplate' is missing or incorrect. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amp-boilerplate?format=websites)
>> ^~~~~~~~~
feature_tests/empty.html:1:0 The mandatory tag 'amphtml engine v0.js script' is missing or incorrect. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
Copyright 2019 The AMP HTML Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the license.
-->
<!--
Test Description:

This test demonstrates that an LTS version of an extension may not be used with the standard
runtime.
-->
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<link rel="canonical" href="./regular-html-version.html">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>

<!-- Valid: First script is non-LTS runtime -->
<script async src="https://cdn.ampproject.org/v0.js"></script>
<!-- Invalid: LTS extension is included. Produces error. -->
<script async custom-element='amp-ad'
src='https://cdn.ampproject.org/lts/v0/amp-ad-0.1.js'></script>
<!-- Valid: Non-LTS extension is included and used. No error or warning. -->
<script async custom-element='amp-font'
src='https://cdn.ampproject.org/v0/amp-font-0.1.js'></script>
</head>
<body>
<amp-font
layout="nodisplay"
font-family="Comic Amp"
timeout="3000">
</amp-font>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FAIL
| <!--
| Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS-IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the license.
| -->
| <!--
| Test Description:
|
| This test demonstrates that an LTS version of an extension may not be used with the standard
| runtime.
| -->
| <!doctype html>
| <html ⚡>
| <head>
| <meta charset="utf-8">
| <link rel="canonical" href="./regular-html-version.html">
| <meta name="viewport" content="width=device-width,minimum-scale=1">
| <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
|
| <!-- Valid: First script is non-LTS runtime -->
| <script async src="https://cdn.ampproject.org/v0.js"></script>
| <!-- Invalid: LTS extension is included. Produces error. -->
| <script async custom-element='amp-ad'
>> ^~~~~~~~~
feature_tests/lts_extension_without_lts_runtime.html:33:2 'amp-ad' must use the non-LTS version to correspond with the first script in the page, which does not use LTS. (see https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml#required-markup)
| src='https://cdn.ampproject.org/lts/v0/amp-ad-0.1.js'></script>
| <!-- Valid: Non-LTS extension is included and used. No error or warning. -->
| <script async custom-element='amp-font'
| src='https://cdn.ampproject.org/v0/amp-font-0.1.js'></script>
| </head>
| <body>
| <amp-font
| layout="nodisplay"
| font-family="Comic Amp"
| timeout="3000">
| </amp-font>
| </body>
| </html>
46 changes: 46 additions & 0 deletions validator/testdata/feature_tests/lts_runtime_after_extension.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
Copyright 2019 The AMP HTML Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the license.
-->
<!--
Test Description:

This test demonstrates that an LTS version of an extension may not be used with the standard
runtime when the runtime is included after the extensions.
-->
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<link rel="canonical" href="./regular-html-version.html">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>

<!-- Valid: First script is an LTS extension. -->
<script async custom-element='amp-ad'
src='https://cdn.ampproject.org/lts/v0/amp-ad-0.1.js'></script>
<!-- Invalid: Non-LTS extension is included and used. Produces error. -->
<script async custom-element='amp-font'
src='https://cdn.ampproject.org/v0/amp-font-0.1.js'></script>
<!-- Valid: LTS runtime is included at the end. -->
<script async src="https://cdn.ampproject.org/lts/v0.js"></script>
</head>
<body>
<amp-font
layout="nodisplay"
font-family="Comic Amp"
timeout="3000">
</amp-font>
</body>
</html>
Loading