6 use Wikimedia\RemexHtml\Tokenizer\Attributes;
7 use Wikimedia\RemexHtml\Tokenizer\PlainAttributes;
8 use Wikimedia\RemexHtml\Tokenizer\RelayTokenHandler;
9 use Wikimedia\RemexHtml\Tokenizer\TokenHandler;
31 private $htmlsingleonly;
36 private $htmlelements;
42 private $attrCallback;
48 private $callbackArgs;
59 TokenHandler $nextHandler,
62 ?callable $attrCallback,
65 parent::__construct( $nextHandler );
67 $this->htmlsingle = $tagData[
'htmlsingle'];
68 $this->htmlsingleonly = $tagData[
'htmlsingleonly'];
69 $this->htmlelements = $tagData[
'htmlelements'];
70 $this->attrCallback = $attrCallback;
71 $this->callbackArgs = $callbackArgs ?? [];
77 public function comment( $text, $sourceStart, $sourceLength ) {
95 private static function validateTag(
string $element, Attributes $attrs ): bool {
96 if ( $element ==
'meta' || $element ==
'link' ) {
97 $params = $attrs->getValues();
98 if ( !isset( $params[
'itemprop'] ) ) {
102 if ( $element ==
'meta' && !isset( $params[
'content'] ) ) {
106 if ( $element ==
'link' && !isset( $params[
'href'] ) ) {
118 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
123 $t = strtolower( $name );
124 if ( isset( $this->htmlelements[
$t] ) ) {
125 if ( $this->attrCallback ) {
126 $attrs = ( $this->attrCallback )( $attrs, ...$this->callbackArgs );
128 if ( $selfClose && !( isset( $this->htmlsingle[
$t] ) || isset( $this->htmlsingleonly[
$t] ) ) ) {
133 if ( !self::validateTag(
$t, $attrs ) ) {
137 $attrs =
new PlainAttributes( $fixedAttrs );
139 if ( $selfClose && !isset( $this->htmlsingleonly[
$t] ) ) {
143 $this->nextHandler->startTag( $name, $attrs,
false, $sourceStart, $sourceLength );
144 $this->nextHandler->endTag( $name, $sourceStart + $sourceLength, 0 );
146 $this->nextHandler->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
152 $this->nextHandler->characters( $this->source, $sourceStart, $sourceLength, $sourceStart, $sourceLength );
158 public function endTag( $name, $sourceStart, $sourceLength ) {
162 $t = strtolower( $name );
163 if ( isset( $this->htmlelements[
$t] ) ) {
165 $this->nextHandler->endTag( $name, $sourceStart, $sourceLength );
168 $this->nextHandler->characters( $this->source, $sourceStart, $sourceLength, $sourceStart, $sourceLength );
if(!defined('MW_SETUP_CALLBACK'))
HTML sanitizer for MediaWiki.
static validateTagAttributes( $attribs, $element)
Take an array of attribute names and values and normalize or discard illegal values for the given ele...