Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
68 changes: 68 additions & 0 deletions src/AngleSharp.Css.Tests/Extensions/Acid3Tests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Behavior the Acid3 test (acid3.acidtests.org) relies on.
/// </summary>
[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<IHtmlParser>();
var document = parser.ParseDocument("<!doctype html><title>Example</title>");
return document.DefaultView;
}
}
}
10 changes: 10 additions & 0 deletions src/AngleSharp.Css/Extensions/MediaListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions src/AngleSharp.Css/FeatureValidators/ColorFeatureValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ namespace AngleSharp.Css.FeatureValidators
{
using AngleSharp.Css.Converters;
using AngleSharp.Css.Dom;
using AngleSharp.Css.Values;
using System;
using static ValueConverters;

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)
{
Expand Down
18 changes: 11 additions & 7 deletions src/AngleSharp.Css/FeatureValidators/MonochromeFeatureValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ namespace AngleSharp.Css.FeatureValidators
{
using AngleSharp.Css.Converters;
using AngleSharp.Css.Dom;
using AngleSharp.Css.Values;
using System;
using static ValueConverters;

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)
{
Expand Down
25 changes: 20 additions & 5 deletions src/AngleSharp.Css/Parser/CssBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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())
{
Expand Down
Loading