Skip to content

Commit 4dc611b

Browse files
[DomCrawler] Support classes from the new DOM extension
1 parent 32aab6e commit 4dc611b

18 files changed

+3642
-319
lines changed

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@
1818
*/
1919
abstract class AbstractUriElement
2020
{
21+
/**
22+
* @deprecated since Symfony 7.1, use `$domeNode` instead
23+
*/
2124
protected \DOMElement $node;
25+
protected \DOMElement|\DOM\Element $domNode;
2226
protected ?string $method;
2327

2428
/**
25-
* @param \DOMElement $node A \DOMElement instance
26-
* @param string|null $currentUri The URI of the page where the link is embedded (or the base href)
27-
* @param string|null $method The method to use for the link (GET by default)
29+
* @param \DOMElement|\DOM\Element $node A \DOMElement or a \DOM\Element instance
30+
* @param string|null $currentUri The URI of the page where the link is embedded (or the base href)
31+
* @param string|null $method The method to use for the link (GET by default)
2832
*
2933
* @throws \InvalidArgumentException if the node is not a link
3034
*/
3135
public function __construct(
32-
\DOMElement $node,
36+
\DOMElement|\DOM\Element $node,
3337
protected ?string $currentUri = null,
3438
?string $method = 'GET',
3539
) {
36-
$this->setNode($node);
40+
$this->setDomNode($node);
3741
$this->method = $method ? strtoupper($method) : null;
3842

3943
$elementUriIsRelative = null === parse_url(trim($this->getRawUri()), \PHP_URL_SCHEME);
@@ -44,11 +48,25 @@ public function __construct(
4448
}
4549

4650
/**
47-
* Gets the node associated with this link.
51+
* @deprecated since Symfony 7.1, use `getDomNode()` instead
4852
*/
4953
public function getNode(): \DOMElement
5054
{
51-
return $this->node;
55+
trigger_deprecation('symfony/dom-crawler', '7.1', 'The "%s()" method is deprecated, use "%s::getDomNode()" instead.', __METHOD__, __CLASS__);
56+
57+
if ($this->domNode instanceof \DOM\Element) {
58+
throw new \LogicException('The node is not an instance of legacy \DOMElement. Use "getDomNode()" instead.');
59+
}
60+
61+
return $this->domNode;
62+
}
63+
64+
/**
65+
* Gets the node associated with this link.
66+
*/
67+
public function getDomNode(): \DOMElement|\DOM\Element
68+
{
69+
return $this->domNode;
5270
}
5371

5472
/**
@@ -108,4 +126,20 @@ protected function canonicalizePath(string $path): string
108126
* @throws \LogicException If given node is not an anchor
109127
*/
110128
abstract protected function setNode(\DOMElement $node): void;
129+
130+
/**
131+
* Sets current \DOMElement or \DOM\Element instance.
132+
*
133+
* @param \DOMElement|\DOM\Element $node A \DOMElement or \DOM\Element instance
134+
*
135+
* @throws \LogicException If given node is not an anchor
136+
*/
137+
protected function setDomNode(\DOMElement|\DOM\Element $node): void
138+
{
139+
$this->domNode = $node;
140+
141+
if ($node instanceof \DOMElement) {
142+
$this->setNode($node);
143+
}
144+
}
111145
}

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add `DomCrawler` to parse HTML and XML with native capabilities
8+
49
7.0
510
---
611

0 commit comments

Comments
 (0)