Skip to content

Commit 8b8482a

Browse files
committed
Fixed invalid selector handling #98
1 parent 60b4a59 commit 8b8482a

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Released on Wednesday, January 5 2022.
44

55
- Fixed issue with `text-shadow` missing the color part (#97)
6+
- Fixed support for `IsToleratingInvalidSelectors` parser option (#98)
67
- Added `Color.UseHex` to change color output format (#96)
78

89
# 0.16.2
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace AngleSharp.Css.Tests.Parsing
2+
{
3+
using AngleSharp.Css.Dom;
4+
using AngleSharp.Css.Parser;
5+
using AngleSharp.Html.Parser;
6+
using NUnit.Framework;
7+
using System.Linq;
8+
9+
[TestFixture]
10+
public class PseudoElements
11+
{
12+
[Test]
13+
public void VendorPrefixedPseudoElementShouldBeAvailable_Issue98()
14+
{
15+
var html = @"<html><head><style>
16+
p::-webkit-scrollbar-thumb {
17+
background: #888
18+
}
19+
</style></head><body><p>test</p></body></html>";
20+
var parser = new HtmlParser(new HtmlParserOptions(), BrowsingContext.New(new Configuration().WithCss(new CssParserOptions
21+
{
22+
IsIncludingUnknownDeclarations = true,
23+
IsIncludingUnknownRules = true,
24+
IsToleratingInvalidSelectors = true,
25+
})));
26+
var document = parser.ParseDocument(html);
27+
var stylesheet = document.StyleSheets.OfType<ICssStyleSheet>().First();
28+
var rule = stylesheet.Rules.OfType<ICssStyleRule>().First();
29+
var selectorText = rule.SelectorText;
30+
Assert.AreEqual("p::-webkit-scrollbar-thumb", selectorText);
31+
}
32+
}
33+
}

src/AngleSharp.Css.Tests/Styling/CssCases.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private static ICssStyleSheet ParseSheet(String text)
1515
{
1616
IsIncludingUnknownDeclarations = true,
1717
IsIncludingUnknownRules = true,
18-
IsToleratingInvalidSelectors = true
18+
IsToleratingInvalidSelectors = false,
1919
});
2020
}
2121

src/AngleSharp.Css/Dom/Internal/Rules/CssPageRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal CssPageRule(ICssStyleSheet owner)
3232
public String SelectorText
3333
{
3434
get => _selector?.Text;
35-
set { _selector = ParseSelector(value); ; }
35+
set { _selector = ParseSelector(value); }
3636
}
3737

3838
public ISelector Selector => _selector;

src/AngleSharp.Css/Dom/Internal/Rules/CssStyleRule.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace AngleSharp.Css.Dom
22
{
3+
using AngleSharp.Css;
4+
using AngleSharp.Dom;
35
using System;
46
using System.Diagnostics;
57
using System.IO;
@@ -45,6 +47,11 @@ public String SelectorText
4547

4648
#region Methods
4749

50+
internal void SetInvalidSelector(String selectorText)
51+
{
52+
_selector = new InvalidSelector(selectorText);
53+
}
54+
4855
protected override void ReplaceWith(ICssRule rule)
4956
{
5057
var newRule = (ICssStyleRule)rule;
@@ -59,5 +66,29 @@ public override void ToCss(TextWriter writer, IStyleFormatter formatter)
5966
}
6067

6168
#endregion
62-
}
69+
70+
#region Selector
71+
72+
class InvalidSelector : ISelector
73+
{
74+
private readonly String _text;
75+
76+
public InvalidSelector(String text)
77+
{
78+
_text = text;
79+
}
80+
81+
public String Text => _text;
82+
83+
public Priority Specificity => Priority.Zero;
84+
85+
public void Accept(ISelectorVisitor visitor)
86+
{
87+
}
88+
89+
public Boolean Match(IElement element, IElement scope) => false;
90+
}
91+
92+
#endregion
93+
}
6394
}

src/AngleSharp.Css/Parser/CssBuilder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ private CssSupportsRule CreateSupports(CssSupportsRule rule, CssToken current)
307307
public CssStyleRule CreateStyle(CssStyleRule rule, CssToken current)
308308
{
309309
CollectTrivia(ref current);
310-
rule.SelectorText = GetArgument(ref current);
310+
var selectorText = GetArgument(ref current);
311+
312+
rule.SelectorText = selectorText;
313+
314+
if (rule.Selector is null && _options.IsToleratingInvalidSelectors)
315+
{
316+
rule.SetInvalidSelector(selectorText);
317+
}
311318

312319
if (current.Type != CssTokenType.CurlyBracketOpen)
313320
{

0 commit comments

Comments
 (0)