32 callable $shouldModifyCallback,
33 callable $modifyCallback
35 $formatter =
new class( $options = [], $shouldModifyCallback, $modifyCallback ) extends HtmlFormatter {
37 private $shouldModifyCallback;
40 private $modifyCallback;
42 public function __construct( $options, $shouldModifyCallback, $modifyCallback ) {
43 parent::__construct( $options );
44 $this->shouldModifyCallback = $shouldModifyCallback;
45 $this->modifyCallback = $modifyCallback;
48 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
49 if ( ( $this->shouldModifyCallback )( $node ) ) {
51 $node->attrs = clone $node->attrs;
52 $newNode = ( $this->modifyCallback )( $node );
53 Assert::parameterType( SerializerNode::class, $newNode,
'return value' );
54 return parent::element( $parent, $newNode, $contents );
56 return parent::element( $parent, $node, $contents );
60 public function startDocument( $fragmentNamespace, $fragmentName ) {
64 $serializer =
new Serializer( $formatter );
65 $treeBuilder =
new TreeBuilder( $serializer );
66 $dispatcher =
new Dispatcher( $treeBuilder );
67 $tokenizer =
new Tokenizer( $dispatcher, $htmlFragment );
69 $tokenizer->execute( [
70 'fragmentNamespace' => HTMLData::NS_HTML,
71 'fragmentName' =>
'body',
74 return $serializer->getResult();