Page MenuHomeWickedGov Phorge

MMLDomVisitor.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

MMLDomVisitor.php

<?php
namespace MediaWiki\Extension\Math\WikiTexVC\MMLnodes;
use DOMDocument;
use DOMElement;
use DOMNode;
class MMLDomVisitor implements MMLVisitor {
private DOMDocument $dom;
/** @var DOMNode[] */
private array $elementStack = [];
public function __construct() {
$this->dom = new DOMDocument( '1.0', 'UTF-8' );
$this->dom->formatOutput = true;
$this->dom->preserveWhiteSpace = false;
$this->elementStack[] = $this->dom;
}
/**
* Visit an MMLbase node and process it.
* @param MMLbase $node Node to visit
* @throws \DOMException
*/
public function visit( MMLbase $node ): void {
$element = $this->createElement( $node );
end( $this->elementStack )->appendChild( $element );
if ( $node instanceof MMLleaf ) {
$textNode = $this->dom->createTextNode( $node->getText() );
$element->appendChild( $textNode );
return;
}
$this->elementStack[] = $element;
foreach ( $node->getChildren() as $child ) {
if ( $child !== null ) {
// implicitly calls visit
$child->accept( $this );
}
}
array_pop( $this->elementStack );
}
/**
* Get HTML from current node
* @return string
*/
public function getHTML(): string {
return trim( $this->dom->saveHTML() );
}
/**
* Create DOMElement from MMLbase node
* @param MMLbase $node
* @return DOMElement
* @throws \DOMException
*/
private function createElement( MMLbase $node ): DOMElement {
$element = $this->dom->createElement( $node->getName() );
foreach ( $node->getAttributes() as $name => $value ) {
$element->setAttribute( $name, htmlspecialchars( $value, ENT_QUOTES ) );
}
return $element;
}
}

File Metadata

Mime Type
text/x-php
Expires
Wed, Aug 19, 14:32 (3 w, 4 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
fe/16/4d9b211660cd2535660f89e0b36b
Default Alt Text
MMLDomVisitor.php (1 KB)

Event Timeline