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