MediaWiki REL1_34
RemexDriver.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Tidy;
4
5use RemexHtml\Serializer\Serializer;
6use RemexHtml\Serializer\SerializerWithTracer;
7use RemexHtml\Tokenizer\Tokenizer;
8use RemexHtml\TreeBuilder\Dispatcher;
9use RemexHtml\TreeBuilder\TreeBuilder;
10use RemexHtml\TreeBuilder\TreeMutationTracer;
11
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
tidy( $text)
Clean up HTML.
Base class for HTML cleanup utilities.
return true
Definition router.php:94