MediaWiki REL1_37
RemexDriver.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Tidy;
4
6use Wikimedia\RemexHtml\HTMLData;
7use Wikimedia\RemexHtml\Serializer\Serializer;
8use Wikimedia\RemexHtml\Serializer\SerializerWithTracer;
9use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
10use Wikimedia\RemexHtml\TreeBuilder\Dispatcher;
11use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder;
12use Wikimedia\RemexHtml\TreeBuilder\TreeMutationTracer;
13
17 private $mungerTrace;
18 private $pwrap;
19
21 public const CONSTRUCTOR_OPTIONS = [
22 'TidyConfig',
23 ];
24
28 public function __construct( $options ) {
29 if ( is_array( $options ) ) {
30 wfDeprecated( __METHOD__ . " with array argument", '1.36' );
31 $config = $options;
32 } else {
33 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
34 $config = $options->get( 'TidyConfig' );
35 }
36 $config += [
37 'treeMutationTrace' => false,
38 'serializerTrace' => false,
39 'mungerTrace' => false,
40 'pwrap' => true
41 ];
42 $this->treeMutationTrace = $config['treeMutationTrace'];
43 $this->serializerTrace = $config['serializerTrace'];
44 $this->mungerTrace = $config['mungerTrace'];
45 $this->pwrap = $config['pwrap'];
46 parent::__construct( $config );
47 }
48
50 public function tidy( $text, ?callable $textProcessor = null ) {
51 $traceCallback = static function ( $msg ) {
52 wfDebug( "RemexHtml: $msg" );
53 };
54 $formatter = new RemexCompatFormatter( [ 'textProcessor' => $textProcessor ] );
55 if ( $this->serializerTrace ) {
56 $serializer = new SerializerWithTracer( $formatter, null, $traceCallback );
57 } else {
58 $serializer = new Serializer( $formatter );
59 }
60 if ( $this->pwrap ) {
61 $munger = new RemexCompatMunger( $serializer, $this->mungerTrace );
62 } else {
63 $munger = $serializer;
64 }
65 if ( $this->treeMutationTrace ) {
66 $tracer = new TreeMutationTracer( $munger, $traceCallback );
67 } else {
68 $tracer = $munger;
69 }
70 $treeBuilder = new TreeBuilder( $tracer, [
71 'ignoreErrors' => true,
72 'ignoreNulls' => true,
73 ] );
74 $dispatcher = new Dispatcher( $treeBuilder );
75 $tokenizer = new Tokenizer( $dispatcher, $text, [
76 'ignoreErrors' => true,
77 'ignoreCharRefs' => true,
78 'ignoreNulls' => true,
79 'skipPreprocess' => true,
80 ] );
81
82 $tokenizer->execute( [
83 'fragmentNamespace' => HTMLData::NS_HTML,
84 'fragmentName' => 'body'
85 ] );
86 return $serializer->getResult();
87 }
88}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
A class for passing options to services.
tidy( $text, ?callable $textProcessor=null)
Clean up HTML.string The corrected HTML output
Base class for HTML cleanup utilities.
return true
Definition router.php:92