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

Sync for validator/cpp/{engine,htmlparser} #35044

Merged
merged 2 commits into from
Jun 25, 2021
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
4 changes: 2 additions & 2 deletions validator/cpp/engine/parse-layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ CssLength CalculateWidth(AmpLayout::Layout input_layout,
input_layout == AmpLayout::FIXED) &&
!input_width.is_set) {
// These values come from AMP's external runtime and can be found in
// https://github.com/ampproject/amphtml/blob/main/src/core/dom/layout/index.js#L70
// https://github.com/ampproject/amphtml/blob/main/src/layout.js#L70
// Note that amp-audio is absent due to it not having explicit dimensions
// (the dimensions are determined at runtime and are specific to the
// particular device/browser/etc).
Expand Down Expand Up @@ -190,7 +190,7 @@ CssLength CalculateHeight(AmpLayout::Layout input_layout,
input_layout == AmpLayout::FIXED_HEIGHT) &&
!input_height.is_set) {
// These values come from AMP's external runtime and can be found in
// https://github.com/ampproject/amphtml/blob/main/src/core/dom/layout/index.js#L70
// https://github.com/ampproject/amphtml/blob/main/src/layout.js#L70
// Note that amp-audio is absent due to it not having explicit dimensions
// (the dimensions are determined at runtime and are specific to the
// particular device/browser/etc).
Expand Down
4 changes: 2 additions & 2 deletions validator/cpp/engine/parse-layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ amp::validator::AmpLayout::Layout CalculateLayout(
std::string GetCssLengthStyle(const CssLength& length, const std::string& type);

// Returns the CSS class name for the given AmpLayout. Equivalent to
// https://github.com/ampproject/amphtml/blob/main/src/core/dom/layout/index.js.
// https://github.com/ampproject/amphtml/blob/main/src/layout.js.
std::string GetLayoutClass(amp::validator::AmpLayout::Layout layout);

// Returns the name for the given AmpLayout.
Expand All @@ -119,7 +119,7 @@ std::string GetSizerStyle(amp::validator::AmpLayout::Layout layout,
const CssLength& width, const CssLength& height);

// Returns whether the given AmpLayout has size defined. Equivalent to
// https://github.com/ampproject/amphtml/blob/main/src/core/dom/layout/index.js.
// https://github.com/ampproject/amphtml/blob/main/src/layout.js.
bool IsLayoutSizeDefined(amp::validator::AmpLayout::Layout layout);

// Returns whether the given AmpLayout waits on a runtime size.
Expand Down
12 changes: 6 additions & 6 deletions validator/cpp/engine/validator-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5698,7 +5698,7 @@ class Validator {

// Updates context's line column index using the current node's position.
inline void UpdateLineColumnIndex(htmlparser::Node* node) {
auto node_line_col = node->PositionInHtmlSrc();
auto node_line_col = node->LineColInHtmlSrc();
if (node_line_col.has_value()) {
auto [line_no, col_no] = node_line_col.value();
context_.SetLineCol(line_no >= 0 ? line_no : line_no + 1,
Expand Down Expand Up @@ -5736,15 +5736,15 @@ class Validator {
if (node->IsManufactured()) {
UpdateLineColumnIndex(node);
context_.AddError(ValidationError::DISALLOWED_TAG,
LineCol(node->PositionInHtmlSrc()->first + 1,
node->PositionInHtmlSrc()->second),
LineCol(node->LineColInHtmlSrc()->first + 1,
node->LineColInHtmlSrc()->second),
/*params=*/{"<?"}, /*spec_url=*/"", &result_);
}
return true;
case htmlparser::NodeType::DOCTYPE_NODE:
if (parse_accounting_.quirks_mode) {
LineCol linecol(1, 0);
auto lc = node->PositionInHtmlSrc();
auto lc = node->LineColInHtmlSrc();
if (lc.has_value()) {
auto [line, col] = lc.value();
linecol = LineCol(line, col > 0 ? col - 1 : col);
Expand Down Expand Up @@ -5786,10 +5786,10 @@ class Validator {
!v.first) {
std::pair<int, int> json_linecol{0, 0};
std::pair<int, int> script_linecol{0, 0};
if (auto pos = node->PositionInHtmlSrc(); pos.has_value()) {
if (auto pos = node->LineColInHtmlSrc(); pos.has_value()) {
script_linecol = {pos.value().first, pos.value().second};
}
if (auto pos = node->FirstChild()->PositionInHtmlSrc();
if (auto pos = node->FirstChild()->LineColInHtmlSrc();
pos.has_value()) {
json_linecol = {pos.value().first, pos.value().second};
}
Expand Down
16 changes: 8 additions & 8 deletions validator/cpp/htmlparser/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,17 @@ std::string Node::InnerText() const {

void Node::UpdateChildNodesPositions(Node* relative_node) {
// Cannot proceed if relative node has no positional information.
if (!relative_node->PositionInHtmlSrc().has_value()) return;
if (!relative_node->LineColInHtmlSrc().has_value()) return;

auto [r_line, r_col] = relative_node->PositionInHtmlSrc().value();
auto [r_line, r_col] = relative_node->LineColInHtmlSrc().value();

// Update the positions of this node.
if (position_in_html_src_.has_value()) {
auto [line, col] = position_in_html_src_.value();
if (line_col_in_html_src_.has_value()) {
auto [line, col] = line_col_in_html_src_.value();
int effective_col = line == 1 ?
r_col + col + AtomUtil::ToString(
relative_node->DataAtom()).size() + 1 /* closing > */ : col;
position_in_html_src_ = LineCol({line + r_line - 1, effective_col});
line_col_in_html_src_ = LineCol({line + r_line - 1, effective_col});
}

// Update the positions of this node's children.
Expand All @@ -454,9 +454,9 @@ std::string Node::DebugString() {
break;
}

if (position_in_html_src_.has_value()) {
ost << position_in_html_src_.value().first << ":"
<< position_in_html_src_.value().second;
if (line_col_in_html_src_.has_value()) {
ost << line_col_in_html_src_.value().first << ":"
<< line_col_in_html_src_.value().second;
}
ost << "\n";

Expand Down
17 changes: 14 additions & 3 deletions validator/cpp/htmlparser/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ class Node {
Atom DataAtom() const { return atom_; }
std::string_view NameSpace() const { return name_space_; }
// Returns nullopt if ParseOptions.store_node_offsets is not set.
std::optional<LineCol> PositionInHtmlSrc() const {
return position_in_html_src_;
std::optional<LineCol> LineColInHtmlSrc() const {
return line_col_in_html_src_;
}
std::optional<Offsets> OffsetsInHtmlSrc() const {
return offsets_in_html_src_;
}
int NumTerms() const {
return num_terms_;
}

const std::vector<Attribute>& Attributes() const { return attributes_; }
Expand Down Expand Up @@ -153,7 +159,12 @@ class Node {
std::string data_;
std::string name_space_;
// Position at which this node appears in HTML source.
std::optional<LineCol> position_in_html_src_;
std::optional<LineCol> line_col_in_html_src_;
// Start/End offsets in original html src.
std::optional<LineCol> offsets_in_html_src_;
// Records the number of terms for text contents.
// Populated and meaningful only if node is of type TEXT_NODE.
int num_terms_ = 0;
std::vector<Attribute> attributes_{};
Node* first_child_ = nullptr;
Node* next_sibling_ = nullptr;
Expand Down
Loading