Skip to content

Commit 5f5f109

Browse files
committed
Merge pull request #22 from Vitallium/master
The selector engine doesn't support the HEX escaped characters
2 parents 6cc9d53 + 91c008a commit 5f5f109

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

source/CsQuery.Tests/Csharp/Selectors/MiscellaneousSelectors.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public override void FixtureSetUp()
132132
Dom = TestDom("TestHtml");
133133
}
134134

135+
[Test, TestMethod]
136+
public void HexWith6DigitsEscapedSelector()
137+
{
138+
string html = "<div id=\"B&W?\"><p>test</p></div>";
139+
var dom = CsQuery.CQ.CreateFragment(html);
140+
var result = dom.Select("#B\\000026W\\3F");
141+
Assert.Greater(result.Length, 0);
142+
}
143+
144+
[Test, TestMethod]
145+
public void HexWith2DigitsEscapedSelector()
146+
{
147+
string html = "<div id=\"B&W?\"><p>test</p></div>";
148+
var dom = CsQuery.CQ.CreateFragment(html);
149+
var result = dom.Select("#B\\26 W\\3F");
150+
Assert.Greater(result.Length, 0);
151+
}
152+
135153
protected string WriteDOMObject(IDomElement obj)
136154
{
137155
string result = "";

source/CsQuery/StringScanner/Patterns/EscapedString.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Globalization;
56
using CsQuery.StringScanner.Implementation;
67

78
namespace CsQuery.StringScanner.Patterns
@@ -37,6 +38,30 @@ public override bool Validate()
3738
{
3839
if (Escaped)
3940
{
41+
// process unicode char code point, if presented
42+
int tempIndex = index;
43+
StringBuilder sb = new StringBuilder();
44+
while (Source[tempIndex] != ' ')
45+
{
46+
sb.Append(Source[tempIndex]);
47+
48+
if (tempIndex == Source.Length - 1 || // end of string?
49+
tempIndex - index == 5) // only 6 hexadecimal digits are allowed
50+
break;
51+
52+
tempIndex++;
53+
}
54+
55+
if (sb.Length == 2 || sb.Length == 6)
56+
{
57+
int value = 0;
58+
if (Int32.TryParse(sb.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out value))
59+
{
60+
character = (char)value;
61+
index = tempIndex;
62+
}
63+
}
64+
4065
Escaped = false;
4166
}
4267
else

0 commit comments

Comments
 (0)