| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Parser; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
use Wikimedia\RemexHtml\Tokenizer\PlainAttributes; |
| 7 |
use Wikimedia\RemexHtml\Tokenizer\RelayTokenHandler; |
| 8 |
use Wikimedia\RemexHtml\Tokenizer\TokenHandler; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class RemexRemoveTagHandler extends RelayTokenHandler { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private $source; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private $htmlsingle; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private $htmlsingleonly; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private $htmlelements; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private $attrCallback; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private $callbackArgs; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
TokenHandler $nextHandler, |
| 59 |
string $source, |
| 60 |
array $tagData, |
| 61 |
?callable $attrCallback, |
| 62 |
?array $callbackArgs |
| 63 |
) { |
| 64 |
parent::__construct( $nextHandler ); |
| 65 |
$this->source = $source; |
| 66 |
$this->htmlsingle = $tagData['htmlsingle']; |
| 67 |
$this->htmlsingleonly = $tagData['htmlsingleonly']; |
| 68 |
$this->htmlelements = $tagData['htmlelements']; |
| 69 |
$this->attrCallback = $attrCallback; |
| 70 |
$this->callbackArgs = $callbackArgs ?? []; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function comment( $text, $sourceStart, $sourceLength ) { |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private static function validateTag( string $element, Attributes $attrs ): bool { |
| 95 |
if ( $element == 'meta' || $element == 'link' ) { |
| 96 |
$params = $attrs->getValues(); |
| 97 |
if ( !isset( $params['itemprop'] ) ) { |
| 98 |
|
| 99 |
return false; |
| 100 |
} |
| 101 |
if ( $element == 'meta' && !isset( $params['content'] ) ) { |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
if ( $element == 'link' && !isset( $params['href'] ) ) { |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
$badtag = false; |
| 122 |
$t = strtolower( $name ); |
| 123 |
if ( isset( $this->htmlelements[$t] ) ) { |
| 124 |
if ( $this->attrCallback ) { |
| 125 |
$attrs = ( $this->attrCallback )( $attrs, ...$this->callbackArgs ); |
| 126 |
} |
| 127 |
if ( $selfClose && !( isset( $this->htmlsingle[$t] ) || isset( $this->htmlsingleonly[$t] ) ) ) { |
| 128 |
|
| 129 |
|
| 130 |
$selfClose = false; |
| 131 |
} |
| 132 |
if ( !self::validateTag( $t, $attrs ) ) { |
| 133 |
$badtag = true; |
| 134 |
} |
| 135 |
$fixedAttrs = Sanitizer::validateTagAttributes( $attrs->getValues(), $t ); |
| 136 |
$attrs = new PlainAttributes( $fixedAttrs ); |
| 137 |
if ( !$badtag ) { |
| 138 |
if ( $selfClose && !isset( $this->htmlsingleonly[$t] ) ) { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
$this->nextHandler->startTag( $name, $attrs, false, $sourceStart, $sourceLength ); |
| 143 |
$this->nextHandler->endTag( $name, $sourceStart + $sourceLength, 0 ); |
| 144 |
} else { |
| 145 |
$this->nextHandler->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 146 |
} |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$this->nextHandler->characters( $this->source, $sourceStart, $sourceLength, $sourceStart, $sourceLength ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
$t = strtolower( $name ); |
| 162 |
if ( isset( $this->htmlelements[$t] ) ) { |
| 163 |
|
| 164 |
$this->nextHandler->endTag( $name, $sourceStart, $sourceLength ); |
| 165 |
} else { |
| 166 |
|
| 167 |
$this->nextHandler->characters( $this->source, $sourceStart, $sourceLength, $sourceStart, $sourceLength ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} |