Page MenuHomeWickedGov Phorge

BatchModifyElements.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

BatchModifyElements.php

<?php
namespace MediaWiki\Extension\DiscussionTools;
use LogicException;
use MediaWiki\Html\HtmlHelper;
use Wikimedia\RemexHtml\Serializer\SerializerNode;
class BatchModifyElements {
/** @var array<array{callable, callable}> */
private array $modifications = [];
/**
* Add a modification to the queue.
*/
public function add( callable $shouldModifyCallback, callable $modifyCallback ): void {
$this->modifications[] = [ $shouldModifyCallback, $modifyCallback ];
}
/**
* Apply all modifications to a fragment.
*/
public function apply( string $htmlFragment, bool $html5format = true ): string {
if ( !count( $this->modifications ) ) {
return $htmlFragment;
}
return HtmlHelper::modifyElements(
$htmlFragment,
function ( SerializerNode $node ) {
foreach ( $this->modifications as [ $shouldModify, $modify ] ) {
if ( $shouldModify( $node ) ) {
return true;
}
}
return false;
},
function ( SerializerNode $node ) {
$modified = null;
foreach ( $this->modifications as [ $shouldModify, $modify ] ) {
if ( $shouldModify( $node ) ) {
if ( !$modified ) {
$modified = $modify( $node );
} else {
// Ideally we would support matching mulitple modifiers, but as a modifier
// can return a string but has to be passed a SerizliazerNode, this is
// not trivial.
throw new LogicException( 'Node matches multiple modifiers.' );
}
}
}
return $modified ?? $node;
},
$html5format
);
}
}

File Metadata

Mime Type
text/x-php
Expires
Aug 16 2026, 05:19 (12 w, 20 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ed/ac/b7f5dd97a5335c9003a544e7852c
Default Alt Text
BatchModifyElements.php (1 KB)

Event Timeline