diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3c671e1..e7476d3e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@ Released on tbd.
- Fixed a comment in a declaration's value with a quote in it (e.g. `/* shouldn't */`) starting a string, which swallowed the rest of the style sheet (Acid2)
- Fixed an escaped `}` or `;` in a declaration's value (e.g. `error: \}`) ending the rule
- Fixed a `;` after a rule (e.g. `a { color: red; };`) being skipped instead of making the selector of the next rule invalid, which then applied
+- Fixed `! important` with whitespace after the `!` not making the declaration important
+- Fixed the `color` media feature, e.g. `(color)` and `(min-color: 0)` not matching, and `monochrome` without a value
+- Fixed `not` making a media query with an unknown feature (e.g. `not all and (bogus)`) match
# 1.5.0
diff --git a/src/AngleSharp.Css.Tests/Extensions/Acid3Tests.cs b/src/AngleSharp.Css.Tests/Extensions/Acid3Tests.cs
new file mode 100644
index 00000000..30dbca5b
--- /dev/null
+++ b/src/AngleSharp.Css.Tests/Extensions/Acid3Tests.cs
@@ -0,0 +1,68 @@
+#nullable disable
+namespace AngleSharp.Css.Tests.Extensions
+{
+ using AngleSharp.Css.Dom;
+ using AngleSharp.Css.Parser;
+ using AngleSharp.Dom;
+ using AngleSharp.Html.Parser;
+ using NUnit.Framework;
+
+ ///
+ /// Behavior the Acid3 test (acid3.acidtests.org) relies on.
+ ///
+ [TestFixture]
+ public class Acid3Tests
+ {
+ [TestCase("color: red ! important", "rgba(255, 0, 0, 1)")]
+ [TestCase("color: red !important", "rgba(255, 0, 0, 1)")]
+ [TestCase("color: red!IMPORTANT ", "rgba(255, 0, 0, 1)")]
+ [TestCase("border: 1px solid ! important", "1px solid")]
+ public void ImportantMayHaveWhitespaceAfterTheBang(System.String declaration, System.String expected)
+ {
+ var parser = new CssParser();
+ var sheet = parser.ParseStyleSheet($"p {{ {declaration} }}");
+ var style = ((ICssStyleRule)sheet.Rules[0]).Style;
+ var name = declaration.Substring(0, declaration.IndexOf(':'));
+
+ Assert.AreEqual("important", style.GetPropertyPriority(name));
+ Assert.AreEqual(expected, style.GetPropertyValue(name));
+ }
+
+ [Test]
+ public void ImportantNeedsTheBang()
+ {
+ var parser = new CssParser();
+ var sheet = parser.ParseStyleSheet("p { color: red important }");
+ var style = ((ICssStyleRule)sheet.Rules[0]).Style;
+
+ Assert.AreEqual(System.String.Empty, style.GetPropertyPriority("color"));
+ }
+
+ [TestCase("all and (min-color: 0)", true)]
+ [TestCase("(color)", true)]
+ [TestCase("(min-color: 1)", true)]
+ [TestCase("(max-color: 0)", false)]
+ [TestCase("(monochrome)", false)]
+ [TestCase("(max-monochrome: 0)", true)]
+ [TestCase("(min-monochrome: 1)", false)]
+ [TestCase("(max-color: 0) and (max-monochrome: 0)", false)]
+ [TestCase("(min-color: 1), (min-monochrome: 1)", true)]
+ [TestCase("not all and (min-color: 1), not all and (min-monochrome: 1)", true)]
+ [TestCase("not all and (bogus)", false)]
+ [TestCase("(bogus), all", true)]
+ public void MediaQueriesOfAColorScreen(System.String query, System.Boolean expected)
+ {
+ var window = CreateWindow(new DefaultRenderDevice { ColorBits = 32, MonochromeBits = 0, ViewPortWidth = 1000, ViewPortHeight = 800 });
+ Assert.AreEqual(expected, window.MatchMedia(query).IsMatched, query);
+ }
+
+ private static IWindow CreateWindow(IRenderDevice device)
+ {
+ var config = Configuration.Default.WithCss().WithRenderDevice(device);
+ var context = BrowsingContext.New(config);
+ var parser = context.GetService();
+ var document = parser.ParseDocument("Example");
+ return document.DefaultView;
+ }
+ }
+}
diff --git a/src/AngleSharp.Css/Extensions/MediaListExtensions.cs b/src/AngleSharp.Css/Extensions/MediaListExtensions.cs
index 4fb83868..c9d879aa 100644
--- a/src/AngleSharp.Css/Extensions/MediaListExtensions.cs
+++ b/src/AngleSharp.Css/Extensions/MediaListExtensions.cs
@@ -74,11 +74,21 @@ private static Boolean Validate(this IFeatureValidator validator, MediaFeature f
public static Boolean Validate(this ICssMedium medium, IRenderDevice device)
{
+ // an unknown feature makes the query unknown, which `not` doesn't turn into a match
+ // (e.g. `not all and (bogus)`, Media Queries 4, 3.2)
+ if (medium.IsInverse && medium.Features.Any(IsUnknown))
+ {
+ return false;
+ }
+
// `not` negates the whole query, i.e. the media type and all features together
var matches = medium.MatchesType(device) && medium.MatchesFeatures(device);
return matches != medium.IsInverse;
}
+ private static Boolean IsUnknown(IMediaFeature feature) =>
+ feature is not MediaCondition && (!AssociatedValidators.TryGetValue(feature, out var validator) || validator is null);
+
private static Boolean MatchesType(this ICssMedium medium, IRenderDevice device)
{
var type = medium.Type;
diff --git a/src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs b/src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs
index 427745c7..a0951ae3 100644
--- a/src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs
+++ b/src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs
@@ -2,7 +2,6 @@ namespace AngleSharp.Css.FeatureValidators
{
using AngleSharp.Css.Converters;
using AngleSharp.Css.Dom;
- using AngleSharp.Css.Values;
using System;
using static ValueConverters;
@@ -10,14 +9,19 @@ sealed class ColorFeatureValidator : IFeatureValidator
{
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
{
- var defaultValue = new CssLengthValue(1.0, CssLengthValue.Unit.None);
- var converter = feature.IsMinimum || feature.IsMaximum ? PositiveIntegerConverter : PositiveIntegerConverter.Option(defaultValue);
- var color = converter.Convert(feature);
+ // the bits per color component (or of a monochrome pixel), 0 if the device has none
+ var available = renderDevice.ColorBits >= 24 ? 8 : renderDevice.ColorBits / 3;
- if (color != null)
+ if (!feature.HasValue)
{
- var desired = color.AsInt32();
- var available = Math.Pow(renderDevice.ColorBits, 2);
+ return !feature.IsMinimum && !feature.IsMaximum && available > 0;
+ }
+
+ var value = NaturalIntegerConverter.Convert(feature);
+
+ if (value != null)
+ {
+ var desired = value.AsInt32();
if (feature.IsMaximum)
{
diff --git a/src/AngleSharp.Css/FeatureValidators/MonochromeFeatureValidator.cs b/src/AngleSharp.Css/FeatureValidators/MonochromeFeatureValidator.cs
index 60a5b8ee..c0ea30b9 100644
--- a/src/AngleSharp.Css/FeatureValidators/MonochromeFeatureValidator.cs
+++ b/src/AngleSharp.Css/FeatureValidators/MonochromeFeatureValidator.cs
@@ -2,7 +2,6 @@ namespace AngleSharp.Css.FeatureValidators
{
using AngleSharp.Css.Converters;
using AngleSharp.Css.Dom;
- using AngleSharp.Css.Values;
using System;
using static ValueConverters;
@@ -10,14 +9,19 @@ sealed class MonochromeFeatureValidator : IFeatureValidator
{
public Boolean Validate(IMediaFeature feature, IRenderDevice renderDevice)
{
- var defaultValue = new CssLengthValue(1.0, CssLengthValue.Unit.None);
- var converter = feature.IsMinimum || feature.IsMaximum ? NaturalIntegerConverter : NaturalIntegerConverter.Option(defaultValue);
- var index = converter.Convert(feature);
+ // the bits per color component (or of a monochrome pixel), 0 if the device has none
+ var available = renderDevice.MonochromeBits;
- if (index != null)
+ if (!feature.HasValue)
{
- var desired = index.AsInt32();
- var available = renderDevice.MonochromeBits;
+ return !feature.IsMinimum && !feature.IsMaximum && available > 0;
+ }
+
+ var value = NaturalIntegerConverter.Convert(feature);
+
+ if (value != null)
+ {
+ var desired = value.AsInt32();
if (feature.IsMaximum)
{
diff --git a/src/AngleSharp.Css/Parser/CssBuilder.cs b/src/AngleSharp.Css/Parser/CssBuilder.cs
index 7c685c42..569ed0df 100644
--- a/src/AngleSharp.Css/Parser/CssBuilder.cs
+++ b/src/AngleSharp.Css/Parser/CssBuilder.cs
@@ -1148,11 +1148,26 @@ private void RaiseErrorOccurred(CssParseError code, TextPosition position) =>
private String CreateValue(ref CssToken token, out Boolean important)
{
- var keyword = CssKeywords.BangImportant;
+ const String keyword = "important";
var value = _tokenizer.ContentFrom(token.Position.Position);
- var keywordStart = value.Length - keyword.Length;
- important = keywordStart >= 0 &&
- value[keywordStart] == Symbols.ExclamationMark &&
+ var end = value.Length;
+
+ while (end > 0 && value[end - 1].IsSpaceCharacter())
+ {
+ end--;
+ }
+
+ var keywordStart = end - keyword.Length;
+ var bang = keywordStart - 1;
+
+ // whitespace may separate the "!" from "important" (e.g., "! important")
+ while (bang >= 0 && value[bang].IsSpaceCharacter())
+ {
+ bang--;
+ }
+
+ important = keywordStart > 0 && bang >= 0 &&
+ value[bang] == Symbols.ExclamationMark &&
String.Compare(value, keywordStart, keyword, 0, keyword.Length, StringComparison.OrdinalIgnoreCase) == 0;
token = NextToken();
@@ -1161,7 +1176,7 @@ private String CreateValue(ref CssToken token, out Boolean important)
return value;
}
- var end = keywordStart;
+ end = bang;
while (end > 0 && value[end - 1].IsSpaceCharacter())
{