Skip to content

Commit

Permalink
Validator rollup (#27419)
Browse files Browse the repository at this point in the history
* cl/302269937 Replace goog.provides with goog.module.

* cl/302272234 github commit msg missing or malformed

* cl/302517930 Replace goog.provides with goog.module.

* cl/302982780 Update entry points for validator.

Co-authored-by: Greg Grothaus <greggrothaus@google.com>
  • Loading branch information
banaag and Greg Grothaus committed Mar 25, 2020
1 parent 81f54b3 commit 6eb5881
Show file tree
Hide file tree
Showing 20 changed files with 2,256 additions and 2,319 deletions.
4 changes: 1 addition & 3 deletions validator/build.py
Expand Up @@ -321,9 +321,7 @@ def CompileValidatorMinified(out_dir):
],
definitions=[],
entry_points=[
'amp.validator.validateString',
'amp.validator.renderValidationResult',
'amp.validator.renderErrorMessage'
'amp.validator',
],
output_file='%s/validator_minified.js' % out_dir)
logging.info('... done')
Expand Down
62 changes: 32 additions & 30 deletions validator/engine/amp4ads-parse-css.js
Expand Up @@ -15,62 +15,58 @@
* limitations under the license.
*/

goog.provide('parse_css.validateAmp4AdsCss');
goog.module('amp.validator.validateAmp4AdsCss');

goog.require('amp.validator.ValidationError');
goog.require('parse_css.ErrorToken');
goog.require('parse_css.RuleVisitor');
goog.require('parse_css.Stylesheet');
goog.require('parse_css.TRIVIAL_ERROR_TOKEN');
goog.require('parse_css.TokenType');
goog.require('parse_css.stripVendorPrefix');
const parse_css = goog.require('parse_css');
const tokenize_css = goog.require('tokenize_css');
const {ValidationError} = goog.require('amp.validator.protogenerated');

/**
* Fills an ErrorToken with the provided position, code, and params.
* @param {!parse_css.Token} positionToken
* @param {!amp.validator.ValidationError.Code} code
* @param {!tokenize_css.Token} positionToken
* @param {!ValidationError.Code} code
* @param {!Array<string>} params
* @return {!parse_css.ErrorToken}
* @return {!tokenize_css.ErrorToken}
*/
function createParseErrorTokenAt(positionToken, code, params) {
const token = new parse_css.ErrorToken(code, params);
const token = new tokenize_css.ErrorToken(code, params);
positionToken.copyPosTo(token);
return token;
}

/**
* For a list of |tokens|, if the first non-whitespace token is an identifier,
* returns its string value. Otherwise, returns the empty string.
* @param {!Array<parse_css.Token>} tokens
* @param {!Array<!tokenize_css.Token>} tokens
* @return {string}
*/
function firstIdent(tokens) {
if (tokens.length === 0) {
return '';
}
if (tokens[0].tokenType === parse_css.TokenType.IDENT) {
return /** @type {!parse_css.StringValuedToken} */ (tokens[0]).value;
if (tokens[0].tokenType === tokenize_css.TokenType.IDENT) {
return /** @type {!tokenize_css.StringValuedToken} */ (tokens[0]).value;
}
if (tokens.length >= 2 &&
(tokens[0].tokenType === parse_css.TokenType.WHITESPACE) &&
tokens[1].tokenType === parse_css.TokenType.IDENT) {
return /** @type {!parse_css.StringValuedToken} */ (tokens[1]).value;
(tokens[0].tokenType === tokenize_css.TokenType.WHITESPACE) &&
tokens[1].tokenType === tokenize_css.TokenType.IDENT) {
return /** @type {!tokenize_css.StringValuedToken} */ (tokens[1]).value;
}
return '';
}

/** @private */
class Amp4AdsVisitor extends parse_css.RuleVisitor {
/**
* @param {!Array<parse_css.ErrorToken>} errors
* @param {!Array<!tokenize_css.ErrorToken>} errors
*/
constructor(errors) {
super();

/** @type {!Array<parse_css.ErrorToken>} */
/** @type {!Array<!tokenize_css.ErrorToken>} */
this.errors = errors;

/** @type {parse_css.AtRule} */
/** @type {?parse_css.AtRule} */
this.inKeyframes = null;
}

Expand All @@ -83,8 +79,8 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
const ident = firstIdent(declaration.value);
if (ident === 'fixed' || ident === 'sticky') {
this.errors.push(createParseErrorTokenAt(
declaration, amp.validator.ValidationError.Code
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE,
declaration,
ValidationError.Code.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE,
['style', 'position', ident]));
}
}
Expand All @@ -102,10 +98,13 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
if (transitionedPropertyStripped !== 'opacity' &&
transitionedPropertyStripped !== 'transform') {
this.errors.push(createParseErrorTokenAt(
decl, amp.validator.ValidationError.Code
decl,
ValidationError.Code
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE_WITH_HINT,
[
'style', 'transition', transitionedProperty,
'style',
'transition',
transitionedProperty,
'[\'opacity\', \'transform\']',
]));
}
Expand All @@ -116,10 +115,12 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
if (this.inKeyframes !== null && name !== 'transform' &&
name !== 'opacity' && name !== 'animation-timing-function') {
this.errors.push(createParseErrorTokenAt(
decl, amp.validator.ValidationError.Code
.CSS_SYNTAX_PROPERTY_DISALLOWED_WITHIN_AT_RULE,
decl,
ValidationError.Code.CSS_SYNTAX_PROPERTY_DISALLOWED_WITHIN_AT_RULE,
[
'style', decl.name, this.inKeyframes.name,
'style',
decl.name,
this.inKeyframes.name,
'[\'animation-timing-function\', \'opacity\', \'transform\']',
]));
}
Expand All @@ -141,9 +142,10 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {

/**
* @param {!parse_css.Stylesheet} styleSheet
* @param {!Array<!parse_css.ErrorToken>} errors
* @param {!Array<!tokenize_css.ErrorToken>} errors
*/
parse_css.validateAmp4AdsCss = function(styleSheet, errors) {
const validateAmp4AdsCss = function(styleSheet, errors) {
const visitor = new Amp4AdsVisitor(errors);
styleSheet.accept(visitor);
};
exports.validateAmp4AdsCss = validateAmp4AdsCss;
60 changes: 29 additions & 31 deletions validator/engine/amp4ads-parse-css_test.js
Expand Up @@ -14,14 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the license.
*/
goog.provide('parse_css.Amp4AdsParseCssTest');
goog.module('parse_css.Amp4AdsParseCssTest');

goog.require('json_testutil.makeJsonKeyCmpFn');
goog.require('json_testutil.renderJSON');
goog.require('parse_css.BlockType');
goog.require('parse_css.parseAStylesheet');
goog.require('parse_css.tokenize');
goog.require('parse_css.validateAmp4AdsCss');
const json_testutil = goog.require('json_testutil');
const parse_css = goog.require('parse_css');
const tokenize_css = goog.require('tokenize_css');
const {validateAmp4AdsCss} = goog.require('amp.validator.validateAmp4AdsCss');

/**
* A strict comparison between two values that does not truncate the
Expand Down Expand Up @@ -65,12 +63,12 @@ describe('validateAmp4AdsCss', () => {
' transform: rotate(180deg); transition: transform 2s; ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -80,24 +78,24 @@ describe('validateAmp4AdsCss', () => {
' -webkit-transition: -o-transform 2s; ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

it('reports that position fixed and position sticky are disallowed', () => {
const css = '.box { position: fixed; position:sticky; }';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals(
[
{
Expand Down Expand Up @@ -127,12 +125,12 @@ describe('validateAmp4AdsCss', () => {
' transition: transform 2s;' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -146,12 +144,12 @@ describe('validateAmp4AdsCss', () => {
' -ms-transition: -webkit-transform 2s;' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -161,12 +159,12 @@ describe('validateAmp4AdsCss', () => {
' transition: transform 2s; ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -175,12 +173,12 @@ describe('validateAmp4AdsCss', () => {
' transition: background-color 2s; ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals(
[{
'line': 1,
Expand All @@ -201,12 +199,12 @@ describe('validateAmp4AdsCss', () => {
' to { transform: rotate(90deg); } ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -217,12 +215,12 @@ describe('validateAmp4AdsCss', () => {
' to { -o-transform: rotate(90deg); } ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -234,12 +232,12 @@ describe('validateAmp4AdsCss', () => {
' to { transform: rotate(90deg); } ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals([], errors);
});

Expand All @@ -251,12 +249,12 @@ describe('validateAmp4AdsCss', () => {
' to { margin-left:0%; width:100%; } ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals(
[
{
Expand Down Expand Up @@ -310,12 +308,12 @@ describe('validateAmp4AdsCss', () => {
' to { margin-left:0%; width:100%; } ' +
'}';
const errors = [];
const tokens = parse_css.tokenize(css, 1, 0, errors);
const tokens = tokenize_css.tokenize(css, 1, 0, errors);
const sheet = parse_css.parseAStylesheet(
tokens, amp4AdsCssParsingSpec, parse_css.BlockType.PARSE_AS_IGNORE,
errors);
assertJSONEquals([], errors);
parse_css.validateAmp4AdsCss(sheet, errors);
validateAmp4AdsCss(sheet, errors);
assertJSONEquals(
[
{
Expand Down

0 comments on commit 6eb5881

Please sign in to comment.