Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Output/Mapped.php

raw
1 <?php
2 /**
3 * Parser output with source map
4 *
5 * @private
6 */
7 class Less_Output_Mapped extends Less_Output {
8
9 /**
10 * The source map generator
11 *
12 * @var Less_SourceMap_Generator
13 */
14 protected $generator;
15
16 /**
17 * Current line
18 *
19 * @var int
20 */
21 protected $lineNumber = 0;
22
23 /**
24 * Current column
25 *
26 * @var int
27 */
28 protected $column = 0;
29
30 /**
31 * Array of contents map (file and its content)
32 *
33 * @var array
34 */
35 protected $contentsMap = [];
36
37 /**
38 * Constructor
39 *
40 * @param array $contentsMap Array of filename to contents map
41 * @param Less_SourceMap_Generator $generator
42 */
43 public function __construct( array $contentsMap, $generator ) {
44 $this->contentsMap = $contentsMap;
45 $this->generator = $generator;
46 }
47
48 /**
49 * Adds a chunk to the stack
50 * The $index for less.php may be different from less.js since less.php does not chunkify inputs
51 *
52 * @param string $chunk
53 * @param array|null $fileInfo
54 * @param int $index
55 * @param bool|null $mapLines
56 */
57 public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
58 // ignore adding empty strings
59 if ( $chunk === '' ) {
60 return;
61 }
62
63 $sourceLines = [];
64 $sourceColumns = ' ';
65
66 if ( isset( $fileInfo['currentUri'] ) ) {
67 $url = $fileInfo['currentUri'];
68
69 if ( isset( $this->contentsMap[$url] ) ) {
70 $inputSource = substr( $this->contentsMap[$url], 0, $index );
71 $sourceLines = explode( "\n", $inputSource );
72 $sourceColumns = end( $sourceLines );
73 } else {
74 throw new Exception( 'Filename ' . $url . ' not in contentsMap' );
75 }
76
77 }
78
79 $lines = explode( "\n", $chunk );
80 $columns = end( $lines );
81
82 if ( $fileInfo ) {
83
84 if ( !$mapLines ) {
85 $this->generator->addMapping(
86 $this->lineNumber + 1, // generated_line
87 $this->column, // generated_column
88 count( $sourceLines ), // original_line
89 strlen( $sourceColumns ), // original_column
90 $fileInfo
91 );
92 } else {
93 for ( $i = 0, $count = count( $lines ); $i < $count; $i++ ) {
94 $this->generator->addMapping(
95 $this->lineNumber + $i + 1, // generated_line
96 $i === 0 ? $this->column : 0, // generated_column
97 count( $sourceLines ) + $i, // original_line
98 $i === 0 ? strlen( $sourceColumns ) : 0, // original_column
99 $fileInfo
100 );
101 }
102 }
103 }
104
105 if ( count( $lines ) === 1 ) {
106 $this->column += strlen( $columns );
107 } else {
108 $this->lineNumber += count( $lines ) - 1;
109 $this->column = strlen( $columns );
110 }
111
112 // add only chunk
113 parent::add( $chunk );
114 }
115
116 }