MediaWiki  master
RemexDriver.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Tidy;
4 
7 use Wikimedia\RemexHtml\HTMLData;
8 use Wikimedia\RemexHtml\Serializer\Serializer;
9 use Wikimedia\RemexHtml\Serializer\SerializerWithTracer;
10 use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
11 use Wikimedia\RemexHtml\TreeBuilder\Dispatcher;
12 use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder;
13 use Wikimedia\RemexHtml\TreeBuilder\TreeMutationTracer;
14 
15 class RemexDriver extends TidyDriverBase {
18  private $mungerTrace;
19  private $pwrap;
20 
22  public const CONSTRUCTOR_OPTIONS = [
24  ];
25 
29  public function __construct( $options ) {
30  if ( is_array( $options ) ) {
31  wfDeprecated( __METHOD__ . " with array argument", '1.36' );
32  $config = $options;
33  } else {
34  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
35  $config = $options->get( MainConfigNames::TidyConfig );
36  }
37  $config += [
38  'treeMutationTrace' => false,
39  'serializerTrace' => false,
40  'mungerTrace' => false,
41  'pwrap' => true
42  ];
43  $this->treeMutationTrace = $config['treeMutationTrace'];
44  $this->serializerTrace = $config['serializerTrace'];
45  $this->mungerTrace = $config['mungerTrace'];
46  $this->pwrap = $config['pwrap'];
47  parent::__construct( $config );
48  }
49 
51  public function tidy( $text, ?callable $textProcessor = null ) {
52  $traceCallback = static function ( $msg ) {
53  wfDebug( "RemexHtml: $msg" );
54  };
55  $formatter = new RemexCompatFormatter( [ 'textProcessor' => $textProcessor ] );
56  if ( $this->serializerTrace ) {
57  $serializer = new SerializerWithTracer( $formatter, null, $traceCallback );
58  } else {
59  $serializer = new Serializer( $formatter );
60  }
61  if ( $this->pwrap ) {
62  $munger = new RemexCompatMunger( $serializer, $this->mungerTrace );
63  } else {
64  $munger = $serializer;
65  }
66  if ( $this->treeMutationTrace ) {
67  $tracer = new TreeMutationTracer( $munger, $traceCallback );
68  } else {
69  $tracer = $munger;
70  }
71  $treeBuilder = new TreeBuilder( $tracer, [
72  'ignoreErrors' => true,
73  'ignoreNulls' => true,
74  ] );
75  $dispatcher = new Dispatcher( $treeBuilder );
76  $tokenizer = new Tokenizer( $dispatcher, $text, [
77  'ignoreErrors' => true,
78  'ignoreCharRefs' => true,
79  'ignoreNulls' => true,
80  'skipPreprocess' => true,
81  ] );
82 
83  $tokenizer->execute( [
84  'fragmentNamespace' => HTMLData::NS_HTML,
85  'fragmentName' => 'body'
86  ] );
87  return $serializer->getResult();
88  }
89 }
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.
A class containing constants representing the names of configuration variables.
const TidyConfig
Name constant for the TidyConfig setting, for use with Config::get()
tidy( $text, ?callable $textProcessor=null)
Clean up HTML.HTML document fragment to clean up $textProcessor A callback to run on the contents of ...
Definition: RemexDriver.php:51
Base class for HTML cleanup utilities.
return true
Definition: router.php:90