Browse Source

querySelector no longer throws exceptions if not syntax error

wrapper-classes
Dustin Wilson 3 years ago
parent
commit
8d9249ddea
  1. 6
      lib/NodeList.php
  2. 11
      lib/traits/ParentNode.php
  3. 3
      tests/cases/TestParentNode.php

6
lib/NodeList.php

@ -26,7 +26,11 @@ class NodeList implements \ArrayAccess, \Countable, \Iterator {
}
public function __construct(iterable $iterable) {
public function __construct(?iterable $iterable = []) {
if ($iterable === null) {
$iterable = [];
}
// Per the specification one cannot create a NodeList via its constructor, but
// this implementation is not going to build up the framework for that.

11
lib/traits/ParentNode.php

@ -7,7 +7,8 @@
declare(strict_types=1);
namespace MensBeam\HTML\DOM;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\CssSelector\CssSelectorConverter,
Symfony\Component\CssSelector\Exception\SyntaxErrorException as SymfonySyntaxErrorException;
# 4.2.6. Mixin ParentNode
@ -312,7 +313,13 @@ trait ParentNode {
$s = $converter->toXPath($selectors);
} catch (\Exception $e) {
# 2. If s is failure, then throw a "SyntaxError" DOMException.
throw new DOMException(DOMException::SYNTAX_ERROR);
// Symfony's library will throw an exception if something is unsupported, too,
// so only throw exception when an actual syntax error, otherwise return null.
if ($e instanceof SymfonySyntaxErrorException) {
throw new DOMException(DOMException::SYNTAX_ERROR);
}
return null;
}
# 3. Return the result of match a selector against a tree with s and node’s root

3
tests/cases/TestParentNode.php

@ -163,12 +163,13 @@ class TestParentNode extends \PHPUnit\Framework\TestCase {
$d = new Document('<!DOCTYPE html><html><body><div>ook</div><div id="eek">eek</div></body></html>');
$div = $d->body->querySelector('div');
$this->assertSame('div', $div->nodeName);
$this->assertNull($d->querySelector('body::before'));
$divs = $d->body->querySelectorAll('div');
$this->assertEquals(2, $divs->length);
$this->assertSame('eek', $divs->item(1)->getAttribute('id'));
$this->assertNull($d->querySelector('.ook'));
$this->assertEquals(0, $d->querySelectorAll('body::before')->length);
}

Loading…
Cancel
Save