Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.52% covered (danger)
18.52%
10 / 54
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MappingsGenerator
18.52% covered (danger)
18.52%
10 / 54
33.33% covered (danger)
33.33%
2 / 6
89.90
0.00% covered (danger)
0.00%
0 / 1
 nextSourceFile
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 consumeSource
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 outputSpace
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 outputToken
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 appendNumber
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getMap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types = 1 );
3
4/**
5 * Copyright 2022 Wikimedia Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @file
20 * @license Apache-2.0
21 * @license MIT
22 * @license GPL-2.0-or-later
23 * @license LGPL-2.1-or-later
24 */
25
26namespace Wikimedia\Minify;
27
28/**
29 * Utility class to generate the "mappings" string of a source map.
30 *
31 * @internal
32 */
33class MappingsGenerator {
34    /** @var string */
35    private $source = '';
36
37    /** @var int The current source file offset in bytes */
38    private $curSourceOffset = 0;
39
40    /** @var int The current source file index */
41    private $curSourceFile = -1;
42    /** @var int The current source file line number */
43    private $curSourceLine = 0;
44    /** @var int The current source file column in UTF-16 code units */
45    private $curSourceColumn = 0;
46    /** @var int The current output file line number */
47    private $curOutLine = 0;
48    /** @var int The current output file column in UTF-16 code units */
49    private $curOutColumn = 0;
50
51    /** @var int The base of the delta encoding for source file index */
52    private $prevSourceFile = 0;
53    /** @var int The base of the delta encoding for source file line number */
54    private $prevSourceLine = 0;
55    /** @var int The base of the delta encoding for source file line column */
56    private $prevSourceColumn = 0;
57    /** @var int The base of the delta encoding for output file line number */
58    private $prevOutLine = 0;
59    /** @var int The base of the delta encoding for output file column */
60    private $prevOutColumn = 0;
61
62    /** @var bool Whether to omit a leading separator when generating a segment */
63    private $isFirstSegment = true;
64
65    /** @var string The accumulated mapping string */
66    private $mappings = '';
67
68    /** @var string The base-64 encoding table */
69    private const BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
70
71    /**
72     * Advance to the next source file.
73     *
74     * @param string $source The contents of the new file
75     */
76    public function nextSourceFile( $source ) {
77        $this->source = $source;
78        $this->curSourceFile++;
79        $this->curSourceOffset = 0;
80        $this->curSourceLine = 0;
81        $this->curSourceColumn = 0;
82    }
83
84    /**
85     * Advance the source position by the specified number of bytes.
86     *
87     * @param int $length
88     */
89    public function consumeSource( $length ) {
90        $newOffset = $this->curSourceOffset + $length;
91        $lineCount = substr_count( $this->source, "\n", $this->curSourceOffset, $length );
92        if ( $lineCount ) {
93            $lineStartPos =
94                strrpos(
95                    substr( $this->source, $this->curSourceOffset, $length ),
96                    "\n"
97                ) + $this->curSourceOffset + 1;
98            $this->curSourceLine += $lineCount;
99            $this->curSourceColumn = Utils::getJsLength(
100                substr( $this->source, $lineStartPos, $newOffset - $lineStartPos ) );
101        } else {
102            $this->curSourceColumn += Utils::getJsLength(
103                substr( $this->source, $this->curSourceOffset, $length ) );
104        }
105        $this->curSourceOffset = $newOffset;
106    }
107
108    /**
109     * Notify the source map generator of the generated text output, which
110     * should not generate a mapping segment.
111     *
112     * @param string $out
113     */
114    public function outputSpace( $out ) {
115        $lineCount = substr_count( $out, "\n" );
116        if ( $lineCount ) {
117            $lineStartPos = strrpos( $out, "\n" ) + 1;
118            $this->curOutLine += $lineCount;
119            $this->curOutColumn = Utils::getJsLength(
120                substr( $out, $lineStartPos, strlen( $out ) - $lineStartPos ) );
121        } else {
122            $this->curOutColumn += Utils::getJsLength( $out );
123        }
124    }
125
126    /**
127     * Notify the source map generator of the generated text output, which
128     * should generate a mapping segment. Append the mapping segment to the
129     * internal buffer.
130     *
131     * @param string $out
132     */
133    public function outputToken( $out ) {
134        $outLineDelta = $this->curOutLine - $this->prevOutLine;
135        if ( $outLineDelta > 0 ) {
136            $this->mappings .= str_repeat( ';', $outLineDelta );
137            $this->prevOutColumn = 0;
138            $this->isFirstSegment = false;
139        } elseif ( $this->isFirstSegment ) {
140            $this->isFirstSegment = false;
141        } else {
142            $this->mappings .= ',';
143        }
144
145        $this->appendNumber( $this->curOutColumn - $this->prevOutColumn );
146        $this->appendNumber( $this->curSourceFile - $this->prevSourceFile );
147        $this->appendNumber( $this->curSourceLine - $this->prevSourceLine );
148        $this->appendNumber( $this->curSourceColumn - $this->prevSourceColumn );
149
150        $this->prevSourceFile = $this->curSourceFile;
151        $this->prevOutLine = $this->curOutLine;
152        $this->prevOutColumn = $this->curOutColumn;
153        $this->prevSourceLine = $this->curSourceLine;
154        $this->prevSourceColumn = $this->curSourceColumn;
155
156        $this->outputSpace( $out );
157    }
158
159    /**
160     * Append a VLQ encoded number to the buffer.
161     *
162     * @param int $n
163     */
164    private function appendNumber( $n ) {
165        $encoded = '';
166
167        // The sign bit goes in the LSB for some reason
168        $vlq = $n < 0 ? ( -$n << 1 ) | 1 : $n << 1;
169
170        do {
171            $digit = $vlq & 0x1f;
172            $vlq >>= 5;
173            if ( $vlq > 0 ) {
174                $digit |= 0x20;
175            }
176            $encoded .= self::BASE64_TABLE[$digit];
177        } while ( $vlq > 0 );
178
179        $this->mappings .= $encoded;
180    }
181
182    /**
183     * Get the generated mappings string.
184     *
185     * @return string
186     */
187    public function getMap() {
188        return $this->mappings;
189    }
190}