Skip to content

Is() never matches a CssConstantValue, so orientation and scan evaluate wrongly #232

Description

@lahma

CssValueExtensions.Is(ICssValue, String) has this branch for the values that ValueConverters.Toggle / Assign produce:

else if (value?.GetType() == typeof(CssConstantValue<>) && value.CssText.Isi(keyword))

GetType() returns the closed type CssConstantValue<Boolean>; typeof(CssConstantValue<>) is the open generic definition, so the comparison is never true and Is answers false for every constant value.

Two media features evaluate through exactly that branch and are therefore wrong on every device:

  • orientation: OrientationModeConverter is Toggle(portrait, landscape), and OrientationFeatureValidator does portrait.Is(CssKeywords.Portrait), so desired is always false. On a landscape device both (orientation: portrait) and (orientation: landscape) match; on a portrait device neither does.
  • scan: ScanFeatureValidator does interlace.Is(CssKeywords.Interlace) on a Toggle(interlace, progressive) value, so (scan: interlace) and (scan: progressive) both answer the negation of IsInterlaced.
var landscape = new DefaultRenderDevice { DeviceWidth = 1024, DeviceHeight = 768 };
new CssParser().ParseMedia("(orientation: portrait)").Validate(landscape);  // true; should be false
new CssParser().ParseMedia("(orientation: landscape)").Validate(landscape); // true

The one-line fix is to compare the generic type definition (value?.GetType() is { IsGenericType: true } t && t.GetGenericTypeDefinition() == typeof(CssConstantValue<>)), or to give CssConstantValue<T> a non-generic marker interface; every other caller of Is on a Toggle result is fixed with it.

src/AngleSharp.Css/Extensions/CssValueExtensions.cs, src/AngleSharp.Css/FeatureValidators/OrientationFeatureValidator.cs, ScanFeatureValidator.cs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions