-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make AMP Layouts first class in the validator.
This implements the logic in src/custom-element.js, applyLayout_. validator.proto: - Introduce AmpLayout, a specification per TagSpec. When added, AmpLayout takes over responsibility for validating the four style attributes width,height,layout,sizes. - AmpLayout::Layout is an enum with the layouts. - I've modeled the natural dimensions with two booleans: defines_default_width and defines_default_height. This is efficient, and has sufficient expressive power for the validation needs. E.g., if only defines_default_height is set, that means the width will be auto. validator.protoascii: - For all AMP tags, I've looked through the implementations in github to see how they override isLayoutSupported. E.g., see here for amp-img.js: builtins/amp-img.js isLayoutSizeDefined is a predicate covering fixed, fixed-height, responsive, and fill, and in addition, nodisplay is always allowed. This, along with the whitelist from layout.js for hasNaturalDimensions is what then makes up a tag spec. validator.js is implemented in the same way: - When AmpLayout is active (as in, the TagSpec under consideration has the amp_layout field set), then the four layout tags are added as optional via a taglist, $AMP_LAYOUT_ATTRS, to disable the usual attribute checks. - Instead, ValidateLayout is triggered, which works like so: * ParseLayout looks up the enum values. * CssLengthAndUnit is a parsing routine for the length values for width and height that we deal with. I made this into a constructor so that I can return four named values. This thing supports 'auto', based on a constructor argument. * After the input attributes have been parsed (that is, layout, width, height), CalculateWidth, CalculateHeight, and CalculateLayout compute the effective layout spec of the element. This is where the defaults get implemented, and the logic is intended to produce the same results as the original. * After this, we verify that the computed layout is OK for the tag, based on the specification from the protoascii (AmpLayout::supported_layouts). In the runtime, this is done via the isLayoutSupported hook I mentioned above. * After this, we run verification specific to particular layout values, e.g., FIXED_HEIGHT requires that the width field is set to 'auto'. I kept those errors short in our usual style. Thanks to @dvoytenko and @Gregable for advice and review (and catching bugs!).
- Loading branch information
1 parent
1f78f94
commit ca2e59c
Showing
14 changed files
with
876 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<!-- | ||
Copyright 2015 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 is a (partial) transcription from test/functional/test-layout.js. | ||
--> | ||
<!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>body {opacity: 0}</style><noscript><style>body {opacity: 1}</style></noscript> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<!-- valid: layout=nodisplay --> | ||
<amp-img layout="nodisplay" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed --> | ||
<amp-img layout="fixed" width="100" height="200" | ||
src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed default width/height --> | ||
<amp-img width="100" height="200" src="itshappening.gif"></amp-img> | ||
|
||
<!-- invalid: layout=fixed - requires width/height --> | ||
<amp-img layout="fixed" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed-height --> | ||
<amp-img layout="fixed-height" height="200" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed-height, with width=auto --> | ||
<amp-img layout="fixed-height" height="200" width="auto" | ||
src="itshappening.gif"></amp-img> | ||
|
||
<!-- invalid: layout=fixed-height, prohibit width!=auto --> | ||
<amp-img layout="fixed-height" height="200" width="300" | ||
src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed-height - default with height --> | ||
<amp-img height="200" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fixed-height - default with height and width=auto --> | ||
<amp-img height="200" width="auto" src="itshappening.gif"></amp-img> | ||
|
||
<!-- invalid: layout=fixed-height - requires height --> | ||
<amp-img layout="fixed-height" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=responsive --> | ||
<amp-img layout="responsive" width="100" height="200" | ||
src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=responsive with sizes --> | ||
<amp-img layout="responsive" width="100" height="200" sizes="50vw" | ||
src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: layout=fill --> | ||
<amp-img layout="fill" src="itshappening.gif"></amp-img> | ||
|
||
<!-- invalid: layout=container | ||
Note that this would be valid if amp-img allowed container. | ||
It doesn't and there is no other element that does. | ||
TODO(johannes): Long run we should get rid of container or use it. | ||
https://github.com/ampproject/amphtml/issues/1109 | ||
--> | ||
<amp-img layout="container" src="itshappening.gif"></amp-img> | ||
|
||
<!-- invalid: layout=unknown --> | ||
<amp-img layout="unknown" src="itshappening.gif"></amp-img> | ||
|
||
<!-- valid: should configure natural dimensions; default layout --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count"></amp-pixel> | ||
|
||
<!-- valid: should configure natural dimensions; default layout; | ||
with width --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" width="11"> | ||
</amp-pixel> | ||
|
||
<!-- valid: should configure natural dimensions; default layout; | ||
with height --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" height="11"> | ||
</amp-pixel> | ||
|
||
<!-- valid: should configure natural dimensions; layout=fixed --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" layout="fixed"> | ||
</amp-pixel> | ||
|
||
<!-- valid: should configure natural dimensions; layout=fixed --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" | ||
layout="fixed"> | ||
</amp-pixel> | ||
|
||
<!-- valid: width and hight set with valid css lengths --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" | ||
width="1px" height="1px"></amp-pixel> | ||
|
||
<!-- invalid: width=auto won't work because amp-pixel doesn't | ||
support fixed-height layout. --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" | ||
width="auto" height="1px"></amp-pixel> | ||
|
||
<!-- invalid: width=X. --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" | ||
width="X" height="1px"></amp-pixel> | ||
|
||
<!-- invalid: height=X. --> | ||
<amp-pixel src="https://www.example.com/make-my-visit-count" | ||
height="X" width="1px"></amp-pixel> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FAIL | ||
feature_tests/amp_layouts.html:41:2 MANDATORY_ATTR_MISSING height (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-img.md) | ||
feature_tests/amp_layouts.html:51:2 INVALID_ATTR_VALUE layout FIXED_HEIGHT requires width='auto' (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-img.md) | ||
feature_tests/amp_layouts.html:61:2 MANDATORY_ATTR_MISSING height (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-img.md) | ||
feature_tests/amp_layouts.html:80:2 INVALID_ATTR_VALUE layout CONTAINER not supported (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-img.md) | ||
feature_tests/amp_layouts.html:83:2 INVALID_ATTR_VALUE layout (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-img.md) | ||
feature_tests/amp_layouts.html:113:2 IMPLIED_LAYOUT_INVALID layout FIXED_HEIGHT not supported (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-pixel.md) | ||
feature_tests/amp_layouts.html:117:2 INVALID_ATTR_VALUE width (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-pixel.md) | ||
feature_tests/amp_layouts.html:121:2 INVALID_ATTR_VALUE height (see https://github.com/ampproject/amphtml/blob/master/builtins/amp-pixel.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
FAIL | ||
feature_tests/amp_list.html:35:2 MANDATORY_ATTR_MISSING src (see https://github.com/ampproject/amphtml/blob/master/extensions/amp-list/amp-list.md) | ||
feature_tests/amp_list.html:36:2 DISALLOWED_ATTR wdith (see https://github.com/ampproject/amphtml/blob/master/extensions/amp-list/amp-list.md) | ||
feature_tests/amp_list.html:41:2 IMPLIED_LAYOUT_INVALID layout CONTAINER not supported (see https://github.com/ampproject/amphtml/blob/master/extensions/amp-list/amp-list.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.