Debian Codegraph @

vendor/wikimedia/parsoid/src/Wt2Html/DOM/Processors/ConvertOffsets.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\Wt2Html\DOM\Processors;
5
6 use Wikimedia\Assert\Assert;
7 use Wikimedia\Parsoid\Config\Env;
8 use Wikimedia\Parsoid\DOM\Node;
9 use Wikimedia\Parsoid\Logger\LintLogger;
10 use Wikimedia\Parsoid\Utils\ContentUtils;
11 use Wikimedia\Parsoid\Utils\DOMDataUtils;
12 use Wikimedia\Parsoid\Wt2Html\Wt2HtmlDOMProcessor;
13
14 /**
15 * Very thin shim to call ContentUtils::convertOffsets where requested
16 * in the environment.
17 */
18 class ConvertOffsets implements Wt2HtmlDOMProcessor {
19 /**
20 * DOM Postprocessor entry function to walk DOM rooted at $root
21 * and convert the DSR offsets as needed.
22 * @see ConvertUtils::convertOffsets
23 *
24 * @inheritDoc
25 */
26 public function run(
27 Env $env, Node $root, array $options = [], bool $atTopLevel = false
28 ): void {
29 Assert::invariant( $atTopLevel, 'This pass should only be run on the top-level' );
30 $doc = $root->ownerDocument;
31 $offsetType = $env->getRequestOffsetType();
32 ContentUtils::convertOffsets(
33 $env, $doc, 'byte', $offsetType
34 );
35 // Because linter runs before this DOM pass, we need to convert offsets
36 // of collected lints from 'byte' to the requested type
37 if ( $offsetType !== 'byte' ) {
38 $lints = $env->getLints();
39 LintLogger::convertDSROffsets( $env, $lints, 'byte', $offsetType );
40 $env->setLints( $lints );
41 }
42 DOMDataUtils::getPageBundle( $doc )->parsoid['offsetType'] = $offsetType;
43 }
44 }