MediaWiki master
RemexDriver.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Tidy;
4
7use Wikimedia\RemexHtml\HTMLData;
8use Wikimedia\RemexHtml\Serializer\Serializer;
9use Wikimedia\RemexHtml\Serializer\SerializerWithTracer;
10use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
11use Wikimedia\RemexHtml\TreeBuilder\Dispatcher;
12use Wikimedia\RemexHtml\TreeBuilder\TreeMutationTracer;
13
16 private $treeMutationTrace;
18 private $serializerTrace;
20 private $mungerTrace;
22 private $pwrap;
23
25 public const CONSTRUCTOR_OPTIONS = [
27 ];
28
29 public function __construct( ServiceOptions $options ) {
30 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
32 $config += [
33 'treeMutationTrace' => false,
34 'serializerTrace' => false,
35 'mungerTrace' => false,
36 'pwrap' => true
37 ];
38 $this->treeMutationTrace = $config['treeMutationTrace'];
39 $this->serializerTrace = $config['serializerTrace'];
40 $this->mungerTrace = $config['mungerTrace'];
41 $this->pwrap = $config['pwrap'];
42 parent::__construct( $config );
43 }
44
46 public function tidy( $text, ?callable $textProcessor = null ) {
47 $traceCallback = static function ( $msg ) {
48 wfDebug( "RemexHtml: $msg" );
49 };
50 $formatter = new RemexCompatFormatter( [ 'textProcessor' => $textProcessor ] );
51 if ( $this->serializerTrace ) {
52 $serializer = new SerializerWithTracer( $formatter, null, $traceCallback );
53 } else {
54 $serializer = new Serializer( $formatter );
55 }
56 if ( $this->pwrap ) {
57 $munger = new RemexCompatMunger( $serializer, $this->mungerTrace );
58 } else {
59 $munger = $serializer;
60 }
61 if ( $this->treeMutationTrace ) {
62 $tracer = new TreeMutationTracer( $munger, $traceCallback );
63 } else {
64 $tracer = $munger;
65 }
66 $treeBuilder = new RemexCompatBuilder( $tracer, [
67 'ignoreErrors' => true,
68 'ignoreNulls' => true,
69 ] );
70 $dispatcher = new Dispatcher( $treeBuilder );
71 $tokenizer = new Tokenizer( $dispatcher, $text, [
72 'ignoreErrors' => true,
73 'ignoreCharRefs' => true,
74 'ignoreNulls' => true,
75 'skipPreprocess' => true,
76 ] );
77
78 $tokenizer->execute( [
79 'fragmentNamespace' => HTMLData::NS_HTML,
80 'fragmentName' => 'body'
81 ] );
82 return $serializer->getResult();
83 }
84}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const TidyConfig
Name constant for the TidyConfig setting, for use with Config::get()
__construct(ServiceOptions $options)
tidy( $text, ?callable $textProcessor=null)
Clean up HTML.string The corrected HTML output
Base class for HTML cleanup utilities.