const Document* document,
Alison MaherIf document is non-null here, isn't execution_context typically a LocalDOMWindow, and you can get the Document from that via DynamicTo<LocalDOMWindow>(execution_context) when creating the fake_context_ instead of plumbing it down here?
Rune LillesveenDocument isn't always guaranteed (although there were several call sites, so it could be that this one is guarenteed and others weren't). I tried to pass it in wherever it was available, though. From what I found, the execution context is also sometimes fake. As a result, I decided to pass this in separately, but I do think this could likely use some further cleanup at some point since it would be nice to guarantee both and then avoid passing one of them in.
Alison MaherWhere do we create fake ExecutionContexts? And in those cases, would we have a valid Document instead? It seems to me if we can pass in a valid Document, it would be trivial to pass a real ExecutionContext too?
If the ExecutionContext is an instance of LocalDOMWindow, and the passed in document is non-null, then getting the document from the LocalDOMWindow should match the document parameter, right?
Alison MaherJust walked back all of the call sites and I think you are right. There are some cases where we pass in nullptr for the context, but we don't have access to the Document in those cases either. There was at least a few cases, example `ManifestManager::ParseManifestFromString()`, where we had a context, but it seemed to not be created via a Document [1]. But this wasn't a case of concern for what I was looking into.
I tried updating to what you had suggested in code, and we are able to still access the Document where we were hoping to, so this will simplify things drastically. Thanks for the super useful feedback. I'll get this cleaned up and re-uploaded.
Done
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. | Gerrit |
Code-Review | +1 |
This is optional, but I think API layering-wise, it would be slightly better to keep the construction of the parser context here and pass in Document* instead, unless there is plan for upcoming CLs where you already have parser context coming in from other call sites.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. | Gerrit |
Commit-Queue | +1 |
This is optional, but I think API layering-wise, it would be slightly better to keep the construction of the parser context here and pass in Document* instead, unless there is plan for upcoming CLs where you already have parser context coming in from other call sites.
Good point, yeah no immediate plans to update the parser contexts being passed in, so I've updated to pass in the Document instead.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. | Gerrit |
12 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: third_party/blink/renderer/core/preferences/preference_overrides.h
Insertions: 2, Deletions: 2.
@@ -14,7 +14,7 @@
namespace blink {
-class CSSParserContext;
+class Document;
// PreferenceOverrides represents the Web Preferences API overrides.
// Spec: https://wicg.github.io/web-preferences-api/
@@ -25,7 +25,7 @@
// When value_string is empty, or otherwise invalid, it clears the override.
void SetOverride(const AtomicString& feature,
const String& value_string,
- const CSSParserContext&);
+ const Document*);
std::optional<mojom::blink::PreferredColorScheme> GetPreferredColorScheme()
const {
```
```
The name of the file: third_party/blink/renderer/core/css/media_feature_overrides_test.cc
Insertions: 15, Deletions: 24.
@@ -7,9 +7,6 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom-blink.h"
#include "third_party/blink/renderer/core/css/media_feature_names.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"
-#include "third_party/blink/renderer/core/execution_context/security_context.h"
namespace blink {
@@ -23,84 +20,78 @@
TEST(MediaFeatureOverrides, SetOverrideInvalid) {
MediaFeatureOverrides overrides;
- const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
- kHTMLStandardMode, SecureContextMode::kInsecureContext);
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "1px", *fake_context);
+ "1px", /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "orange", *fake_context);
+ "orange", /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "orange",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPreferredColorScheme().has_value());
}
TEST(MediaFeatureOverrides, SetOverrideValid) {
MediaFeatureOverrides overrides;
- const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
- kHTMLStandardMode, SecureContextMode::kInsecureContext);
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "light", *fake_context);
+ "light", /*document=*/nullptr);
EXPECT_EQ(mojom::blink::PreferredColorScheme::kLight,
overrides.GetPreferredColorScheme());
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "dark", *fake_context);
+ "dark", /*document=*/nullptr);
EXPECT_EQ(mojom::blink::PreferredColorScheme::kDark,
overrides.GetPreferredColorScheme());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "reduce",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_TRUE(overrides.GetPrefersReducedTransparency().value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature,
- "no-preference", *fake_context);
+ "no-preference", /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPrefersReducedTransparency().value());
}
TEST(MediaFeatureOverrides, ResetOverride) {
MediaFeatureOverrides overrides;
- const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
- kHTMLStandardMode, SecureContextMode::kInsecureContext);
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "light", *fake_context);
+ "light", /*document=*/nullptr);
EXPECT_TRUE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "", *fake_context);
+ "", /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "light", *fake_context);
+ "light", /*document=*/nullptr);
EXPECT_TRUE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(media_feature_names::kPrefersColorSchemeMediaFeature,
- "invalid", *fake_context);
+ "invalid", /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPreferredColorScheme().has_value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "reduce",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_TRUE(overrides.GetPrefersReducedTransparency().has_value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPrefersReducedTransparency().has_value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "reduce",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_TRUE(overrides.GetPrefersReducedTransparency().has_value());
overrides.SetOverride(
media_feature_names::kPrefersReducedTransparencyMediaFeature, "invalid",
- *fake_context);
+ /*document=*/nullptr);
EXPECT_FALSE(overrides.GetPrefersReducedTransparency().has_value());
}
```
```
The name of the file: third_party/blink/renderer/core/preferences/preference_overrides.cc
Insertions: 2, Deletions: 2.
@@ -12,9 +12,9 @@
void PreferenceOverrides::SetOverride(const AtomicString& feature,
const String& value_string,
- const CSSParserContext& context) {
+ const Document* document) {
MediaQueryExpValue value = MediaFeatureOverrides::ParseMediaQueryValue(
- feature, value_string, context);
+ feature, value_string, document);
if (feature == media_feature_names::kPrefersColorSchemeMediaFeature) {
preferred_color_scheme_ =
```
```
The name of the file: third_party/blink/renderer/core/css/media_feature_overrides.h
Insertions: 3, Deletions: 3.
@@ -15,7 +15,7 @@
namespace blink {
enum class ColorSpaceGamut;
-class CSSParserContext;
+class Document;
enum class ForcedColors;
class MediaQueryExpValue;
@@ -25,7 +25,7 @@
public:
void SetOverride(const AtomicString& feature,
const String& value_string,
- const CSSParserContext&);
+ const Document*);
std::optional<ColorSpaceGamut> GetColorGamut() const { return color_gamut_; }
std::optional<mojom::blink::PreferredColorScheme> GetPreferredColorScheme()
@@ -59,7 +59,7 @@
static MediaQueryExpValue ParseMediaQueryValue(const AtomicString&,
const String&,
- const CSSParserContext&);
+ const Document*);
private:
std::optional<ColorSpaceGamut> color_gamut_;
```
```
The name of the file: third_party/blink/renderer/core/page/page.cc
Insertions: 2, Deletions: 6.
@@ -1292,10 +1292,8 @@
if (auto* local_frame = DynamicTo<LocalFrame>(MainFrame())) {
document = local_frame->GetDocument();
}
- const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
- kHTMLStandardMode, SecureContextMode::kInsecureContext, document);
- media_feature_overrides_->SetOverride(media_feature, value, *fake_context);
+ media_feature_overrides_->SetOverride(media_feature, value, document);
if (media_feature == "prefers-color-scheme" ||
media_feature == "forced-colors")
SettingsChanged(ChangeType::kColorScheme);
@@ -1322,10 +1320,8 @@
if (auto* local_frame = DynamicTo<LocalFrame>(MainFrame())) {
document = local_frame->GetDocument();
}
- const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
- kHTMLStandardMode, SecureContextMode::kInsecureContext, document);
- preference_overrides_->SetOverride(media_feature, value, *fake_context);
+ preference_overrides_->SetOverride(media_feature, value, document);
if (media_feature == "prefers-color-scheme") {
SettingsChanged(ChangeType::kColorScheme);
} else {
```
```
The name of the file: third_party/blink/renderer/core/css/media_feature_overrides.cc
Insertions: 10, Deletions: 4.
@@ -90,12 +90,18 @@
MediaQueryExpValue MediaFeatureOverrides::ParseMediaQueryValue(
const AtomicString& feature,
const String& value_string,
- const CSSParserContext& context) {
+ const Document* document) {
CSSTokenizer tokenizer(value_string);
auto [tokens, raw_offsets] = tokenizer.TokenizeToEOFWithOffsets();
CSSParserTokenRange range(tokens);
CSSParserTokenOffsets offsets(tokens, std::move(raw_offsets), value_string);
+ // TODO(xiaochengh): This is a fake CSSParserContext that only passes
+ // down the CSSParserMode. Plumb the real CSSParserContext through, so that
+ // web features can be counted correctly.
+ const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
+ kHTMLStandardMode, SecureContextMode::kInsecureContext, document);
+
// MediaFeatureOverrides are used to emulate various media feature values.
// These don't need to pass an ExecutionContext, since the parsing of
// the actual CSS will determine whether or not the emulated values will come
@@ -105,16 +111,16 @@
// Document to get the ExecutionContext so the extra parameter should be
// removed.
MediaQueryExpBounds bounds =
- MediaQueryExp::Create(feature, range, offsets, context).Bounds();
+ MediaQueryExp::Create(feature, range, offsets, *fake_context).Bounds();
DCHECK(!bounds.left.IsValid());
return bounds.right.value;
}
void MediaFeatureOverrides::SetOverride(const AtomicString& feature,
const String& value_string,
- const CSSParserContext& context) {
+ const Document* document) {
MediaQueryExpValue value =
- ParseMediaQueryValue(feature, value_string, context);
+ ParseMediaQueryValue(feature, value_string, document);
if (feature == media_feature_names::kColorGamutMediaFeature) {
color_gamut_ = ConvertColorGamut(value);
```
Ensure CSSParserContext has Document in MediaQueryExp
The CSSParserContext passed into MediaQueryExp is fake and doesn't
currently have access to the Document. Having access to
the Document will allow one to log console messages, etc for media
queries. Thus, ensure that the CSSParserContext passed into
MediaQueryExp has access to the Document where possible.
No behavior changes intended.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. | Gerrit |
#include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
Just realized I left this behind. Will clean this up in a follow up.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. | Gerrit |