Convert style query parsing to the streaming parser.

This removes the last use of CSSTokenizedValue.

Change-Id: Ic5210a4232511420eb576701cd9bf4e8b6148801
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5797788
Commit-Queue: Steinar H Gunderson <sesse@chromium.org>
Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1344685}
diff --git a/third_party/blink/renderer/core/css/build.gni b/third_party/blink/renderer/core/css/build.gni
index 9a611e9..89fcab4 100644
--- a/third_party/blink/renderer/core/css/build.gni
+++ b/third_party/blink/renderer/core/css/build.gni
@@ -559,7 +559,6 @@
   "parser/css_selector_parser.h",
   "parser/css_supports_parser.cc",
   "parser/css_supports_parser.h",
-  "parser/css_tokenized_value.h",
   "parser/css_tokenizer.cc",
   "parser/css_tokenizer.h",
   "parser/css_tokenizer_input_stream.cc",
diff --git a/third_party/blink/renderer/core/css/css_syntax_definition.h b/third_party/blink/renderer/core/css/css_syntax_definition.h
index ca713f5c..4d863521 100644
--- a/third_party/blink/renderer/core/css/css_syntax_definition.h
+++ b/third_party/blink/renderer/core/css/css_syntax_definition.h
@@ -8,7 +8,6 @@
 #include "third_party/blink/renderer/core/core_export.h"
 #include "third_party/blink/renderer/core/css/css_syntax_component.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h"
 
 namespace blink {
diff --git a/third_party/blink/renderer/core/css/css_variable_data.h b/third_party/blink/renderer/core/css/css_variable_data.h
index 2ad12341..16b7445 100644
--- a/third_party/blink/renderer/core/css/css_variable_data.h
+++ b/third_party/blink/renderer/core/css/css_variable_data.h
@@ -14,7 +14,6 @@
 #include "third_party/blink/renderer/core/core_export.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/platform/wtf/forward.h"
 #include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
diff --git a/third_party/blink/renderer/core/css/media_query_exp.cc b/third_party/blink/renderer/core/css/media_query_exp.cc
index f839b98..bc9337c 100644
--- a/third_party/blink/renderer/core/css/media_query_exp.cc
+++ b/third_party/blink/renderer/core/css/media_query_exp.cc
@@ -42,7 +42,6 @@
 #include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_impl.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/core/css/parser/css_variable_parser.h"
 #include "third_party/blink/renderer/core/css/properties/css_parsing_utils.h"
 #include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
@@ -427,14 +426,18 @@
   CSSParserContext::ParserModeOverridingScope scope(context, kHTMLStandardMode);
 
   if (CSSVariableParser::IsValidVariableName(media_feature)) {
+    // Parse style queries for container queries, e.g. “style(--foo: bar)”.
+    // (These look like a declaration, but are really a test as part of
+    // a media query expression.) !important, if present, is stripped
+    // and ignored.
     base::span span = range.RemainingSpan();
     StringView original_string =
         offsets.StringForTokens(span.data(), span.data() + span.size());
-    CSSTokenizedValue tokenized_value{range, original_string};
-    CSSParserImpl::RemoveImportantAnnotationIfPresent(tokenized_value);
+    CSSTokenizer tokenizer(original_string);
+    CSSParserTokenStream stream(tokenizer);
     if (const CSSValue* value =
-            CSSVariableParser::ParseDeclarationIncludingCSSWide(
-                tokenized_value, false, context)) {
+            CSSVariableParser::ParseDeclarationIncludingCSSWide(stream, false,
+                                                                context)) {
       while (!range.AtEnd()) {
         range.Consume();
       }
diff --git a/third_party/blink/renderer/core/css/parser/at_rule_descriptor_parser.h b/third_party/blink/renderer/core/css/parser/at_rule_descriptor_parser.h
index dc5c09f..48bb642 100644
--- a/third_party/blink/renderer/core/css/parser/at_rule_descriptor_parser.h
+++ b/third_party/blink/renderer/core/css/parser/at_rule_descriptor_parser.h
@@ -15,7 +15,6 @@
 class CSSParserContext;
 class CSSParserTokenStream;
 class CSSValue;
-struct CSSTokenizedValue;
 
 class AtRuleDescriptorParser {
   STATIC_ONLY(AtRuleDescriptorParser);
diff --git a/third_party/blink/renderer/core/css/parser/css_parser_impl.cc b/third_party/blink/renderer/core/css/parser/css_parser_impl.cc
index ed54a917..e441310 100644
--- a/third_party/blink/renderer/core/css/parser/css_parser_impl.cc
+++ b/third_party/blink/renderer/core/css/parser/css_parser_impl.cc
@@ -2862,43 +2862,6 @@
                                 rule_type);
 }
 
-bool CSSParserImpl::RemoveImportantAnnotationIfPresent(
-    CSSTokenizedValue& tokenized_value) {
-  if (tokenized_value.range.size() == 0) {
-    return false;
-  }
-  const CSSParserToken* first = tokenized_value.range.begin();
-  const CSSParserToken* last = tokenized_value.range.end() - 1;
-  while (last >= first && last->GetType() == kWhitespaceToken) {
-    --last;
-  }
-  if (last >= first && last->GetType() == kIdentToken &&
-      EqualIgnoringASCIICase(last->Value(), "important")) {
-    --last;
-    while (last >= first && last->GetType() == kWhitespaceToken) {
-      --last;
-    }
-    if (last >= first && last->GetType() == kDelimiterToken &&
-        last->Delimiter() == '!') {
-      tokenized_value.range = tokenized_value.range.MakeSubRange(first, last);
-
-      // Truncate the text to remove the delimiter and everything after it.
-      if (!tokenized_value.text.empty()) {
-        DCHECK_NE(tokenized_value.text.ToString().find('!'), kNotFound);
-        unsigned truncated_length = tokenized_value.text.length() - 1;
-        while (tokenized_value.text[truncated_length] != '!') {
-          --truncated_length;
-        }
-        tokenized_value.text =
-            StringView(tokenized_value.text, 0, truncated_length);
-      }
-      return true;
-    }
-  }
-
-  return false;
-}
-
 std::unique_ptr<Vector<KeyframeOffset>> CSSParserImpl::ConsumeKeyframeKeyList(
     const CSSParserContext* context,
     CSSParserTokenRange range) {
diff --git a/third_party/blink/renderer/core/css/parser/css_parser_impl.h b/third_party/blink/renderer/core/css/parser/css_parser_impl.h
index 8b7cf11..5a6e81c 100644
--- a/third_party/blink/renderer/core/css/parser/css_parser_impl.h
+++ b/third_party/blink/renderer/core/css/parser/css_parser_impl.h
@@ -17,7 +17,6 @@
 #include "third_party/blink/renderer/core/css/css_selector.h"
 #include "third_party/blink/renderer/core/css/parser/css_nesting_type.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
 #include "third_party/blink/renderer/core/css/style_rule_keyframe.h"
 #include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h"
@@ -165,8 +164,6 @@
       wtf_size_t offset,
       const CSSParserContext*);
 
-  static bool RemoveImportantAnnotationIfPresent(CSSTokenizedValue&);
-
   CSSParserMode GetMode() const;
 
  private:
diff --git a/third_party/blink/renderer/core/css/parser/css_property_parser.h b/third_party/blink/renderer/core/css/parser/css_property_parser.h
index acbeb24..d6956cf 100644
--- a/third_party/blink/renderer/core/css/parser/css_property_parser.h
+++ b/third_party/blink/renderer/core/css/parser/css_property_parser.h
@@ -25,7 +25,6 @@
 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_PROPERTY_PARSER_H_
 #define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_PROPERTY_PARSER_H_
 
-#include "css_tokenized_value.h"
 #include "third_party/blink/renderer/core/core_export.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_mode.h"
diff --git a/third_party/blink/renderer/core/css/parser/css_tokenized_value.h b/third_party/blink/renderer/core/css/parser/css_tokenized_value.h
deleted file mode 100644
index 623a2954..0000000
--- a/third_party/blink/renderer/core/css/parser/css_tokenized_value.h
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2020 The Chromium Authors
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_TOKENIZED_VALUE_H_
-#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_TOKENIZED_VALUE_H_
-
-#include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
-
-namespace blink {
-
-struct CSSTokenizedValue {
-  STACK_ALLOCATED();
-
- public:
-  CSSParserTokenRange range;
-  StringView text;
-};
-
-}  // namespace blink
-
-#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_CSS_TOKENIZED_VALUE_H_
diff --git a/third_party/blink/renderer/core/css/parser/css_variable_parser.cc b/third_party/blink/renderer/core/css/parser/css_variable_parser.cc
index 1a66753d..3f1fc14 100644
--- a/third_party/blink/renderer/core/css/parser/css_variable_parser.cc
+++ b/third_party/blink/renderer/core/css/parser/css_variable_parser.cc
@@ -18,16 +18,6 @@
 
 namespace blink {
 
-namespace {
-
-CSSValue* ParseCSSWideValue(CSSParserTokenRange range) {
-  range.ConsumeWhitespace();
-  CSSValue* value = css_parsing_utils::ConsumeCSSWideKeyword(range);
-  return range.AtEnd() ? value : nullptr;
-}
-
-}  // namespace
-
 bool CSSVariableParser::IsValidVariableName(const CSSParserToken& token) {
   if (token.GetType() != kIdentToken) {
     return false;
@@ -40,15 +30,28 @@
   return string.length() >= 3 && string[0] == '-' && string[1] == '-';
 }
 
-CSSValue* CSSVariableParser::ParseDeclarationIncludingCSSWide(
-    const CSSTokenizedValue& tokenized_value,
+const CSSValue* CSSVariableParser::ParseDeclarationIncludingCSSWide(
+    CSSParserTokenStream& stream,
     bool is_animation_tainted,
     const CSSParserContext& context) {
-  if (CSSValue* css_wide = ParseCSSWideValue(tokenized_value.range)) {
+  stream.EnsureLookAhead();
+  bool important_ignored;
+  if (const CSSValue* css_wide = CSSPropertyParser::ConsumeCSSWideKeyword(
+          stream, /*allow_important_annotation=*/true, important_ignored)) {
     return css_wide;
   }
-  return ParseDeclarationValue(tokenized_value.text, is_animation_tainted,
-                               context);
+  CSSVariableData* variable_data = ConsumeUnparsedDeclaration(
+      stream,
+      /*allow_important_annotation=*/true, is_animation_tainted,
+      /*must_contain_variable_reference=*/false,
+      /*restricted_value=*/false,
+      /*comma_ends_declaration=*/false, important_ignored,
+      context.GetExecutionContext());
+  if (!variable_data) {
+    return nullptr;
+  }
+  return MakeGarbageCollected<CSSUnparsedDeclarationValue>(variable_data,
+                                                           &context);
 }
 
 CSSUnparsedDeclarationValue* CSSVariableParser::ParseDeclarationValue(
diff --git a/third_party/blink/renderer/core/css/parser/css_variable_parser.h b/third_party/blink/renderer/core/css/parser/css_variable_parser.h
index d842a452..cf880dc 100644
--- a/third_party/blink/renderer/core/css/parser/css_variable_parser.h
+++ b/third_party/blink/renderer/core/css/parser/css_variable_parser.h
@@ -18,13 +18,13 @@
 class CSSParserTokenStream;
 class CSSUnparsedDeclarationValue;
 class CSSVariableData;
-struct CSSTokenizedValue;
 
 class CORE_EXPORT CSSVariableParser {
  public:
-  static CSSValue* ParseDeclarationIncludingCSSWide(const CSSTokenizedValue&,
-                                                    bool is_animation_tainted,
-                                                    const CSSParserContext&);
+  static const CSSValue* ParseDeclarationIncludingCSSWide(
+      CSSParserTokenStream&,
+      bool is_animation_tainted,
+      const CSSParserContext&);
   static CSSUnparsedDeclarationValue* ParseDeclarationValue(
       StringView,
       bool is_animation_tainted,
diff --git a/third_party/blink/renderer/core/css/parser/css_variable_parser_test.cc b/third_party/blink/renderer/core/css/parser/css_variable_parser_test.cc
index 5f25f74..0937164a 100644
--- a/third_party/blink/renderer/core/css/parser/css_variable_parser_test.cc
+++ b/third_party/blink/renderer/core/css/parser/css_variable_parser_test.cc
@@ -8,7 +8,6 @@
 #include "third_party/blink/renderer/core/css/css_test_helpers.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_stream.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
 #include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
 
diff --git a/third_party/blink/renderer/core/css/resolver/style_cascade.h b/third_party/blink/renderer/core/css/resolver/style_cascade.h
index d6993fb..dedcd98 100644
--- a/third_party/blink/renderer/core/css/resolver/style_cascade.h
+++ b/third_party/blink/renderer/core/css/resolver/style_cascade.h
@@ -12,7 +12,6 @@
 #include "third_party/blink/renderer/core/css/css_property_value.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token.h"
 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
-#include "third_party/blink/renderer/core/css/parser/css_tokenized_value.h"
 #include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
 #include "third_party/blink/renderer/core/css/properties/css_bitset.h"
 #include "third_party/blink/renderer/core/css/properties/css_property.h"