MediaWiki  1.34.0
RemexDriver.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Tidy;
4 
5 use RemexHtml\Serializer\Serializer;
6 use RemexHtml\Serializer\SerializerWithTracer;
7 use RemexHtml\Tokenizer\Tokenizer;
8 use RemexHtml\TreeBuilder\Dispatcher;
9 use RemexHtml\TreeBuilder\TreeBuilder;
10 use RemexHtml\TreeBuilder\TreeMutationTracer;
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 }
MediaWiki\Tidy\RemexCompatMunger
Definition: RemexCompatMunger.php:17
true
return true
Definition: router.php:92
MediaWiki\Tidy\RemexDriver\$treeMutationTrace
$treeMutationTrace
Definition: RemexDriver.php:13
MediaWiki\Tidy\RemexCompatFormatter
Definition: RemexCompatFormatter.php:12
MediaWiki\Tidy\RemexDriver\$pwrap
$pwrap
Definition: RemexDriver.php:16
MediaWiki\Tidy\RemexDriver
Definition: RemexDriver.php:12
MediaWiki\Tidy\RemexDriver\$serializerTrace
$serializerTrace
Definition: RemexDriver.php:14
MediaWiki\Tidy\RemexDriver\$mungerTrace
$mungerTrace
Definition: RemexDriver.php:15
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
MediaWiki\Tidy\TidyDriverBase
Base class for HTML cleanup utilities.
Definition: TidyDriverBase.php:8
MediaWiki\Tidy\RemexDriver\tidy
tidy( $text)
Clean up HTML.
Definition: RemexDriver.php:32
MediaWiki\Tidy\RemexDriver\__construct
__construct(array $config)
Definition: RemexDriver.php:18
MediaWiki\Tidy\TidyDriverBase\$config
$config
Definition: TidyDriverBase.php:9
MediaWiki\Tidy
Definition: RemexCompatFormatter.php:3