| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\DOM; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Serializer\AbstractSerializer; |
| 6 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 7 |
use Wikimedia\RemexHtml\TreeBuilder\Element; |
| 8 |
|
| 9 |
/** |
| 10 |
* This class providers a Serializer-like interface to DOMBuilder, allowing |
| 11 |
* DOMBuilder and direct serialization to be used interchangeably. |
| 12 |
* |
| 13 |
* HtmlFormatter::formatDOMNode() can be used directly if this interface is |
| 14 |
* not required. |
| 15 |
*/ |
| 16 |
class DOMSerializer implements AbstractSerializer { |
| 17 |
private DOMFormatter $formatter; |
| 18 |
private DOMBuilder $builder; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param DOMBuilder $builder |
| 22 |
* @param DOMFormatter $formatter This may be, for example, an HtmlFormatter object |
| 23 |
*/ |
| 24 |
public function __construct( DOMBuilder $builder, DOMFormatter $formatter ) { |
| 25 |
$this->builder = $builder; |
| 26 |
$this->formatter = $formatter; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @inheritDoc |
| 31 |
*/ |
| 32 |
public function getResult() { |
| 33 |
$fragment = $this->builder->getFragment(); |
| 34 |
$s = ''; |
| 35 |
foreach ( $fragment->childNodes as $child ) { |
| 36 |
$s .= $this->formatter->formatDOMNode( $child ); |
| 37 |
} |
| 38 |
return $s; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @inheritDoc |
| 43 |
*/ |
| 44 |
public function startDocument( $fragmentNamespace, $fragmentName ) { |
| 45 |
$this->builder->startDocument( $fragmentNamespace, $fragmentName ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @inheritDoc |
| 50 |
*/ |
| 51 |
public function endDocument( $pos ) { |
| 52 |
$this->builder->endDocument( $pos ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @inheritDoc |
| 57 |
*/ |
| 58 |
public function characters( $preposition, $refElement, $text, $start, $length, |
| 59 |
$sourceStart, $sourceLength |
| 60 |
) { |
| 61 |
$this->builder->characters( $preposition, $refElement, $text, $start, $length, |
| 62 |
$sourceStart, $sourceLength ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @inheritDoc |
| 67 |
*/ |
| 68 |
public function insertElement( $preposition, $refElement, Element $element, $void, |
| 69 |
$sourceStart, $sourceLength |
| 70 |
) { |
| 71 |
$this->builder->insertElement( $preposition, $refElement, $element, $void, |
| 72 |
$sourceStart, $sourceLength ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @inheritDoc |
| 77 |
*/ |
| 78 |
public function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 79 |
$this->builder->endTag( $element, $sourceStart, $sourceLength ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @inheritDoc |
| 84 |
*/ |
| 85 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 86 |
$this->builder->doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @inheritDoc |
| 91 |
*/ |
| 92 |
public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) { |
| 93 |
$this->builder->comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @inheritDoc |
| 98 |
*/ |
| 99 |
public function error( $text, $pos ) { |
| 100 |
$this->builder->error( $text, $pos ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @inheritDoc |
| 105 |
*/ |
| 106 |
public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 107 |
$this->builder->mergeAttributes( $element, $attrs, $sourceStart ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @inheritDoc |
| 112 |
*/ |
| 113 |
public function removeNode( Element $element, $sourceStart ) { |
| 114 |
$this->builder->removeNode( $element, $sourceStart ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @inheritDoc |
| 119 |
*/ |
| 120 |
public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 121 |
$this->builder->reparentChildren( $element, $newParent, $sourceStart ); |
| 122 |
} |
| 123 |
} |