RusKnyaz / Optimus

Optimus is headless Web Browser fully implemented on .net.
MIT License
81 stars 8 forks source link

Css selectors not fully supported #47

Closed joshua211 closed 3 years ago

joshua211 commented 3 years ago

Hello, I've noticed that css selectors that have brackets or other symbols that are represented with a backslash are not found.

Consider this html

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <div>
      <span class="Fw(b) testclass t(0.1)">
        <h1>Test</h1>
        <a href="test.de">Test link</a>
      </span>
    </div>
    <div>
      <h1>Test 2</h1>
      <span> </span>
    </div>
  </body>
</html>

goal is to find the span with the 3 classes. The code looks like this:

var engine = EngineBuilder.New().UseJint().EnableCss().Build();
await engine.OpenUrl("http://127.0.0.1:5500/Test.html");

var selector1 = engine.Document.QuerySelector(@"span.Fw\(b\)");
var selector2 = engine.Document.QuerySelector(@"span.Fw(b)");
var selector3 = engine.Document.QuerySelector(@"span.t\(0\.1\)");
var selector4 = engine.Document.QuerySelector(@".testclass");

if we evaluate the results like this

if (selector1 != null)
     System.Console.WriteLine("Selector 1 pass");
if (selector2 != null)
     System.Console.WriteLine("Selector 2 pass");
 if (selector3 != null)
     System.Console.WriteLine("Selector 3 pass");
if (selector4 != null)
     System.Console.WriteLine("Selector 4 pass");

the only selector that passes is selector 4, which does not use brackets or dots.

RusKnyaz commented 3 years ago

Thank you for the detailed report. I researched this a bit and found out that the class can actually contain any character (https://stackoverflow.com/a/6732899/5301097). I'm going to fix this.