Skip to content

Commit a464aad

Browse files
committed
Fix NRE in ToCss for a PageRule with no selector
@page rules don't have to have a selector, but calling ToCss on such an instance of PageRule would trigger a NullReferenceException due to SelectorText unconditionally dereferencing Selector. Fixed by checking if we have a selector or not in ToCss before trying to output it.
1 parent 363b951 commit a464aad

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/ExCSS.Tests/Cases.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,5 +1022,15 @@ public void StylesheetIncludeUnknownDeclarationsWithKnownPropertyShouldNotUseUnk
10221022

10231023
Assert.IsNotType<UnknownProperty>(((StyleRule)document.Rules[0]).Style.Children.First());
10241024
}
1025+
1026+
[Theory]
1027+
[InlineData("@page { margin-bottom: 5pt; margin-top: 5pt }", "@page {margin-bottom: 5pt; margin-top: 5pt; }")]
1028+
public void PageRuleCSSOutput(string input, string expected)
1029+
{
1030+
var parser = new StylesheetParser();
1031+
var document = parser.Parse(input);
1032+
1033+
Assert.Equal(expected, document.ToCss());
1034+
}
10251035
}
10261036
}

src/ExCSS/Rules/PageRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal PageRule(StylesheetParser parser)
1616

1717
public override void ToCss(TextWriter writer, IStyleFormatter formatter)
1818
{
19-
writer.Write(formatter.Rule("@page", SelectorText, "{"));
19+
writer.Write(formatter.Rule("@page", Selector == null ? "" : SelectorText, "{"));
2020

2121
Style.ToCss(writer, formatter);
2222

0 commit comments

Comments
 (0)