Skip to content

Commit

Permalink
Sync for validator cpp engine and cpp htmlparser (#37510)
Browse files Browse the repository at this point in the history
* Add C++ tests on whether validator rules make sense

PiperOrigin-RevId: 423415201

* Remove allowlist of tags which have no descriptive name

PiperOrigin-RevId: 423430953

* Fix a bug by no longer using string_view of block scope string

PiperOrigin-RevId: 423900419

* Test whether attribute rule makes sense

PiperOrigin-RevId: 424432911

* Test whether satisfies match up with requires and excludes

PiperOrigin-RevId: 424454520

* Test whether error codes are unique

PiperOrigin-RevId: 424454591

* Fix compile errors in github unit test

PiperOrigin-RevId: 424701686

Co-authored-by: Boxiao Cao <caoboxiao@google.com>
  • Loading branch information
twifkak and caoboxiao committed Jan 27, 2022
1 parent 7319405 commit a602362
Show file tree
Hide file tree
Showing 4 changed files with 594 additions and 18 deletions.
3 changes: 3 additions & 0 deletions validator/cpp/engine/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ cc_library(
copts = ["-std=c++17"],
deps = [
":validator-internal",
"//cpp/htmlparser/css:parse-css",
"//:validator_cc_proto",
],
alwayslink = 1,
Expand Down Expand Up @@ -329,6 +330,8 @@ cc_test(
":testing-utils",
":validator",
":validator_pb",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
"//cpp/htmlparser/css:parse_css_cc_proto",
Expand Down
32 changes: 16 additions & 16 deletions validator/cpp/engine/validator-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ static const LazyRE2 kRuntimeScriptPathRe = {
static const LazyRE2 kExtensionPathRe = {
R"re((?:lts/)?v0/(amp-[a-z0-9-]*)-([a-z0-9.]*)\.(?:m)?js(?:\?f=sxg)?)re"};

// Generates a htmlparser::css::CssParsingConfig.
CssParsingConfig GenCssParsingConfig() {
CssParsingConfig config;
// If other @ rule types are added to the rules, their block parsing types
// will need to be added here as well.
config.at_rule_spec["font-face"] = BlockType::PARSE_AS_DECLARATIONS;
config.at_rule_spec["keyframes"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["media"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["page"] = BlockType::PARSE_AS_DECLARATIONS;
config.at_rule_spec["supports"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["-moz-document"] = BlockType::PARSE_AS_RULES;
// Note that ignoring still generates an error.
config.default_spec = BlockType::PARSE_AS_IGNORE;
return config;
}

namespace {

#define CHECK_NOTNULL(x) (x)
Expand Down Expand Up @@ -845,22 +861,6 @@ class ParsedAttrSpecs {
unordered_map<std::string, vector<const ParsedAttrSpec*>> attr_lists_by_name_;
};

// Generates a htmlparser::css::CssParsingConfig.
CssParsingConfig GenCssParsingConfig() {
CssParsingConfig config;
// If other @ rule types are added to the rules, their block parsing types
// will need to be added here as well.
config.at_rule_spec["font-face"] = BlockType::PARSE_AS_DECLARATIONS;
config.at_rule_spec["keyframes"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["media"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["page"] = BlockType::PARSE_AS_DECLARATIONS;
config.at_rule_spec["supports"] = BlockType::PARSE_AS_RULES;
config.at_rule_spec["-moz-document"] = BlockType::PARSE_AS_RULES;
// Note that ignoring still generates an error.
config.default_spec = BlockType::PARSE_AS_IGNORE;
return config;
}

// Instances of this class precompute the regular expressions for a particular
// Cdata specification.
class ParsedCdataSpec {
Expand Down
2 changes: 2 additions & 0 deletions validator/cpp/engine/validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifndef CPP_ENGINE_VALIDATOR_H_
#define CPP_ENGINE_VALIDATOR_H_

#include "cpp/htmlparser/css/parse-css.h"
#include "cpp/htmlparser/document.h"
#include "validator.pb.h"

Expand All @@ -38,6 +39,7 @@ ValidationResult Validate(std::string_view html,

int RulesSpecVersion();
int ValidatorVersion();
htmlparser::css::CssParsingConfig GenCssParsingConfig();

} // namespace amp::validator

Expand Down

0 comments on commit a602362

Please sign in to comment.