MediaWiki  master
RemexDriver.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Tidy;
4 
11 
12 class RemexDriver extends TidyDriverBase {
15  private $mungerTrace;
16  private $pwrap;
17 
18  public function __construct( array $config ) {
19  $config += [
20  'treeMutationTrace' => false,
21  'serializerTrace' => false,
22  'mungerTrace' => false,
23  'pwrap' => true
24  ];
25  $this->treeMutationTrace = $config['treeMutationTrace'];
26  $this->serializerTrace = $config['serializerTrace'];
27  $this->mungerTrace = $config['mungerTrace'];
28  $this->pwrap = $config['pwrap'];
29  parent::__construct( $config );
30  }
31 
32  public function tidy( $text ) {
33  $traceCallback = function ( $msg ) {
34  wfDebug( "RemexHtml: $msg" );
35  };
36 
37  $formatter = new RemexCompatFormatter;
38  if ( $this->serializerTrace ) {
39  $serializer = new SerializerWithTracer( $formatter, null, $traceCallback );
40  } else {
41  $serializer = new Serializer( $formatter );
42  }
43  if ( $this->pwrap ) {
44  $munger = new RemexCompatMunger( $serializer, $this->mungerTrace );
45  } else {
46  $munger = $serializer;
47  }
48  if ( $this->treeMutationTrace ) {
49  $tracer = new TreeMutationTracer( $munger, $traceCallback );
50  } else {
51  $tracer = $munger;
52  }
53  $treeBuilder = new TreeBuilder( $tracer, [
54  'ignoreErrors' => true,
55  'ignoreNulls' => true,
56  ] );
57  $dispatcher = new Dispatcher( $treeBuilder );
58  $tokenizer = new Tokenizer( $dispatcher, $text, [
59  'ignoreErrors' => true,
60  'ignoreCharRefs' => true,
61  'ignoreNulls' => true,
62  'skipPreprocess' => true,
63  ] );
64 
65  $tokenizer->execute( [
66  'fragmentNamespace' => \RemexHtml\HTMLData::NS_HTML,
67  'fragmentName' => 'body'
68  ] );
69  return $serializer->getResult();
70  }
71 }
__construct(array $config)
Definition: RemexDriver.php:18
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Base class for HTML cleanup utilities.
return true
Definition: router.php:92