Skip to content

Commit cea80cc

Browse files
committed
Fix UnknownProperty being used for known properties
If the parser options IncludeUnknownDeclarations or AllowInvalidValues are set to true, StylesheetComposer will incorrectly use UnknownProperty for everything, even when the property name in question is a known one. Fix by first trying to create the property with the specified name and only falling back to UnknownProperty if the created property is null and one of IncludeUnknownDeclarations or AllowInvalidValues are set.
1 parent a0f9914 commit cea80cc

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/ExCSS.Tests/Cases.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,5 +1013,14 @@ public void StyleSheetWithInitialCommentShouldWorkWithTriviaActive()
10131013
Assert.IsType<Comment>(comment);
10141014
Assert.Equal(" Comment at the start ", ((Comment)comment).Data);
10151015
}
1016+
1017+
[Fact]
1018+
public void StylesheetIncludeUnknownDeclarationsWithKnownPropertyShouldNotUseUnknownProperty()
1019+
{
1020+
var parser = new StylesheetParser(includeUnknownDeclarations: true);
1021+
var document = parser.Parse(@"body { border-width: 0; }");
1022+
1023+
Assert.IsNotType<UnknownProperty>(((StyleRule)document.Rules[0]).Style.Children.First());
1024+
}
10161025
}
10171026
}

src/ExCSS/Parser/StylesheetComposer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,13 @@ public Property CreateDeclarationWith(Func<string, Property> createProperty, ref
620620

621621
if (propertyName.Length > 0)
622622
{
623-
property = _parser.Options.IncludeUnknownDeclarations ||
624-
_parser.Options.AllowInvalidValues
625-
? new UnknownProperty(propertyName)
626-
: createProperty(propertyName);
623+
property = createProperty(propertyName);
624+
625+
if (property == null
626+
&& (_parser.Options.IncludeUnknownDeclarations || _parser.Options.AllowInvalidValues))
627+
{
628+
property = new UnknownProperty(propertyName);
629+
}
627630

628631
if (property == null)
629632
RaiseErrorOccurred(ParseError.UnknownDeclarationName, start);

0 commit comments

Comments
 (0)