| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Html2Wt; |
| 5 |
|
| 6 |
use stdClass; |
| 7 |
use Wikimedia\Parsoid\Config\Env; |
| 8 |
use Wikimedia\Parsoid\DOM\Comment; |
| 9 |
use Wikimedia\Parsoid\DOM\Element; |
| 10 |
use Wikimedia\Parsoid\DOM\Node; |
| 11 |
use Wikimedia\Parsoid\DOM\Text; |
| 12 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 13 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 15 |
|
| 16 |
class DiffUtils { |
| 17 |
/** |
| 18 |
* Get a node's diff marker. |
| 19 |
* |
| 20 |
* @param Node $node |
| 21 |
* @return stdClass|null |
| 22 |
*/ |
| 23 |
public static function getDiffMark( Node $node ): ?stdClass { |
| 24 |
if ( !( $node instanceof Element ) ) { |
| 25 |
return null; |
| 26 |
} |
| 27 |
$data = DOMDataUtils::getNodeData( $node ); |
| 28 |
return $data->parsoid_diff ?? null; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check that the diff markers on the node exist. |
| 33 |
* |
| 34 |
* @param Node $node |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
public static function hasDiffMarkers( Node $node ): bool { |
| 38 |
return self::getDiffMark( $node ) !== null || self::isDiffMarker( $node ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function hasDiffMark( Node $node, string $mark ): bool { |
| 42 |
// For 'deletion' and 'insertion' markers on non-element nodes, |
| 43 |
// a mw:DiffMarker meta is added |
| 44 |
if ( $mark === DiffMarkers::DELETED || ( $mark === DiffMarkers::INSERTED && !( $node instanceof Element ) ) ) { |
| 45 |
return self::isDiffMarker( $node->previousSibling, $mark ); |
| 46 |
} else { |
| 47 |
$diffMark = self::getDiffMark( $node ); |
| 48 |
return $diffMark && in_array( $mark, $diffMark->diff, true ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public static function hasInsertedDiffMark( Node $node ): bool { |
| 53 |
return self::hasDiffMark( $node, DiffMarkers::INSERTED ); |
| 54 |
} |
| 55 |
|
| 56 |
public static function maybeDeletedNode( ?Node $node ): bool { |
| 57 |
return $node instanceof Element && self::isDiffMarker( $node, DiffMarkers::DELETED ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Is node a mw:DiffMarker node that represents a deleted block node? |
| 62 |
* This annotation is added by the DOMDiff pass. |
| 63 |
* |
| 64 |
* @param ?Node $node |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public static function isDeletedBlockNode( ?Node $node ): bool { |
| 68 |
return $node instanceof Element && self::maybeDeletedNode( $node ) && |
| 69 |
$node->hasAttribute( 'data-is-block' ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function directChildrenChanged( Node $node ): bool { |
| 73 |
return self::hasDiffMark( $node, DiffMarkers::CHILDREN_CHANGED ); |
| 74 |
} |
| 75 |
|
| 76 |
public static function onlySubtreeChanged( Element $node ): bool { |
| 77 |
$dmark = self::getDiffMark( $node ); |
| 78 |
if ( !$dmark ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
foreach ( $dmark->diff as $mark ) { |
| 83 |
if ( $mark !== DiffMarkers::SUBTREE_CHANGED && $mark !== DiffMarkers::CHILDREN_CHANGED ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
public static function subtreeUnchanged( Element $node ): bool { |
| 92 |
$dmark = self::getDiffMark( $node ); |
| 93 |
if ( !$dmark ) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
foreach ( $dmark->diff as $mark ) { |
| 98 |
if ( $mark !== DiffMarkers::MODIFIED_WRAPPER ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
public static function addDiffMark( Node $node, Env $env, string $mark ): ?Element { |
| 107 |
static $ignoreableNodeTypes = [ XML_DOCUMENT_NODE, XML_DOCUMENT_TYPE_NODE, XML_DOCUMENT_FRAG_NODE ]; |
| 108 |
|
| 109 |
if ( $mark === DiffMarkers::DELETED || $mark === DiffMarkers::MOVED ) { |
| 110 |
return self::prependTypedMeta( $node, 'mw:DiffMarker/' . $mark ); |
| 111 |
} elseif ( $node instanceof Text || $node instanceof Comment ) { |
| 112 |
if ( $mark !== DiffMarkers::INSERTED ) { |
| 113 |
$env->log( 'error', 'BUG! CHANGE-marker for ', $node->nodeType, ' node is: ', $mark ); |
| 114 |
} |
| 115 |
return self::prependTypedMeta( $node, 'mw:DiffMarker/' . $mark ); |
| 116 |
} elseif ( $node instanceof Element ) { |
| 117 |
self::setDiffMark( $node, $mark ); |
| 118 |
} elseif ( !in_array( $node->nodeType, $ignoreableNodeTypes, true ) ) { |
| 119 |
$env->log( 'error', 'Unhandled node type', $node->nodeType, 'in addDiffMark!' ); |
| 120 |
} |
| 121 |
|
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Set a diff marker on a node. |
| 127 |
* |
| 128 |
* @param Node $node |
| 129 |
* @param string $change |
| 130 |
*/ |
| 131 |
private static function setDiffMark( Node $node, string $change ): void { |
| 132 |
if ( !( $node instanceof Element ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
$dpd = self::getDiffMark( $node ); |
| 136 |
if ( !$dpd ) { |
| 137 |
$dpd = (object)[ // FIXME object or array? |
| 138 |
'diff' => [ $change ] |
| 139 |
]; |
| 140 |
} elseif ( !in_array( $change, $dpd->diff, true ) ) { |
| 141 |
$dpd->diff[] = $change; |
| 142 |
} |
| 143 |
DOMDataUtils::getNodeData( $node )->parsoid_diff = $dpd; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Insert a meta element with the passed-in typeof attribute before a node. |
| 148 |
* |
| 149 |
* @param Node $node |
| 150 |
* @param string $type |
| 151 |
* @return Element |
| 152 |
*/ |
| 153 |
private static function prependTypedMeta( Node $node, string $type ): Element { |
| 154 |
$meta = $node->ownerDocument->createElement( 'meta' ); |
| 155 |
DOMUtils::addTypeOf( $meta, $type ); |
| 156 |
$node->parentNode->insertBefore( $meta, $node ); |
| 157 |
return $meta; |
| 158 |
} |
| 159 |
|
| 160 |
private static function getAttributes( Element $node, array $ignoreableAttribs ): array { |
| 161 |
$h = DOMUtils::attributes( $node ); |
| 162 |
foreach ( $h as $name => $value ) { |
| 163 |
if ( in_array( $name, $ignoreableAttribs, true ) ) { |
| 164 |
unset( $h[$name] ); |
| 165 |
} |
| 166 |
} |
| 167 |
// If there's no special attribute handler, we want a straight |
| 168 |
// comparison of these. |
| 169 |
if ( !in_array( 'data-parsoid', $ignoreableAttribs, true ) ) { |
| 170 |
$h['data-parsoid'] = DOMDataUtils::getDataParsoid( $node ); |
| 171 |
} |
| 172 |
if ( !in_array( 'data-mw', $ignoreableAttribs, true ) && DOMDataUtils::validDataMw( $node ) ) { |
| 173 |
$h['data-mw'] = DOMDataUtils::getDataMw( $node ); |
| 174 |
} |
| 175 |
return $h; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Attribute equality test. |
| 180 |
* |
| 181 |
* @param Element $nodeA |
| 182 |
* @param Element $nodeB |
| 183 |
* @param array $ignoreableAttribs |
| 184 |
* @param array $specializedAttribHandlers |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
public static function attribsEquals( |
| 188 |
Element $nodeA, Element $nodeB, array $ignoreableAttribs, array $specializedAttribHandlers |
| 189 |
): bool { |
| 190 |
$hA = self::getAttributes( $nodeA, $ignoreableAttribs ); |
| 191 |
$hB = self::getAttributes( $nodeB, $ignoreableAttribs ); |
| 192 |
|
| 193 |
if ( count( $hA ) !== count( $hB ) ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
$keysA = array_keys( $hA ); |
| 198 |
sort( $keysA ); |
| 199 |
$keysB = array_keys( $hB ); |
| 200 |
sort( $keysB ); |
| 201 |
|
| 202 |
foreach ( $keysA as $i => $k ) { |
| 203 |
if ( $k !== $keysB[$i] ) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
$attribEquals = $specializedAttribHandlers[$k] ?? null; |
| 208 |
if ( $attribEquals ) { |
| 209 |
// Use a specialized compare function, if provided |
| 210 |
if ( !$hA[$k] || !$hB[$k] || !$attribEquals( $nodeA, $hA[$k], $nodeB, $hB[$k] ) ) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
} elseif ( $hA[$k] !== $hB[$k] ) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Check a node to see whether it's a diff marker. |
| 223 |
* |
| 224 |
* @param ?Node $node |
| 225 |
* @param ?string $mark |
| 226 |
* @return bool |
| 227 |
*/ |
| 228 |
public static function isDiffMarker( |
| 229 |
?Node $node, ?string $mark = null |
| 230 |
): bool { |
| 231 |
if ( !$node ) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
if ( $mark ) { |
| 236 |
return DOMUtils::isMarkerMeta( $node, 'mw:DiffMarker/' . $mark ); |
| 237 |
} else { |
| 238 |
return DOMCompat::nodeName( $node ) === 'meta' && |
| 239 |
DOMUtils::matchTypeOf( $node, '#^mw:DiffMarker/#' ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |