Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DOMFragmentBuilder | |
0.00% |
0 / 18 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| getFragment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createDocument | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| createNode | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| insertNode | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\DOM; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\HTMLData; |
| 7 | use Wikimedia\RemexHtml\TreeBuilder\Element; |
| 8 | use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder; |
| 9 | |
| 10 | /** |
| 11 | * A TreeHandler which constructs a DOMDocumentFragment. |
| 12 | * |
| 13 | * Note that this class permits third-party `DOMImplementation`s |
| 14 | * (documents other than `\DOMDocument`, nodes other than `\DOMNode`, |
| 15 | * etc) and so no enforced PHP type hints are used which name these |
| 16 | * classes directly. For the sake of static type checking, the |
| 17 | * types *in comments* are given as if the standard PHP `\DOM*` |
| 18 | * classes are being used but at runtime everything is duck-typed. |
| 19 | */ |
| 20 | class DOMFragmentBuilder extends DOMBuilder { |
| 21 | /** @var \DOMDocumentFragment */ |
| 22 | private $documentFragment; |
| 23 | |
| 24 | /** |
| 25 | * @param \DOMDocument $ownerDocument |
| 26 | * @param array $options See options for DOMBuilder::__construct |
| 27 | */ |
| 28 | public function __construct( $ownerDocument, $options = [] ) { |
| 29 | $options += [ |
| 30 | 'domImplementation' => $ownerDocument->implementation, |
| 31 | // @phan-suppress-next-line PhanRedundantCondition (could be a different implementation) |
| 32 | 'suppressHtmlNamespace' => $ownerDocument->implementation |
| 33 | instanceof \DOMImplementation, |
| 34 | ]; |
| 35 | parent::__construct( $options ); |
| 36 | $this->documentFragment = $ownerDocument->createDocumentFragment(); |
| 37 | } |
| 38 | |
| 39 | /** @inheritDoc */ |
| 40 | public function getFragment() { |
| 41 | return $this->documentFragment; |
| 42 | } |
| 43 | |
| 44 | /** @inheritDoc */ |
| 45 | protected function createDocument( |
| 46 | ?string $doctypeName = null, |
| 47 | ?string $public = null, |
| 48 | ?string $system = null |
| 49 | ) { |
| 50 | $doc = $this->documentFragment->ownerDocument; |
| 51 | // phan thinks $doc is nullable |
| 52 | '@phan-var \DOMDocument $doc'; |
| 53 | return $doc; |
| 54 | } |
| 55 | |
| 56 | /** @inheritDoc */ |
| 57 | protected function createNode( Element $element ) { |
| 58 | if ( $element->namespace === HTMLData::NS_HTML && |
| 59 | $element->name === 'html' ) { |
| 60 | // Use the DocumentFragment instead of the <html> documentElement |
| 61 | $element->userData = $this->documentFragment; |
| 62 | return $this->documentFragment; |
| 63 | } |
| 64 | return parent::createNode( $element ); |
| 65 | } |
| 66 | |
| 67 | /** @inheritDoc */ |
| 68 | protected function insertNode( $preposition, $refElement, $node ) { |
| 69 | if ( $preposition === TreeBuilder::ROOT ) { |
| 70 | // We don't need to insert the DocumentFragment into anything. |
| 71 | return; |
| 72 | } |
| 73 | parent::insertNode( $preposition, $refElement, $node ); |
| 74 | } |
| 75 | } |