Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.68% covered (warning)
75.68%
28 / 37
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Less_Output_Mapped
75.68% covered (warning)
75.68%
28 / 37
50.00% covered (danger)
50.00%
1 / 2
12.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 add
74.29% covered (warning)
74.29%
26 / 35
0.00% covered (danger)
0.00%
0 / 1
11.70
1<?php
2/**
3 * Parser output with source map
4 *
5 * @private
6 */
7class 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 ( $fileInfo ) {
67
68            $url = $fileInfo['currentUri'];
69
70            if ( isset( $this->contentsMap[$url] ) ) {
71                $inputSource = substr( $this->contentsMap[$url], 0, $index );
72                $sourceLines = explode( "\n", $inputSource );
73                $sourceColumns = end( $sourceLines );
74            } else {
75                throw new Exception( 'Filename ' . $url . ' not in contentsMap' );
76            }
77
78        }
79
80        $lines = explode( "\n", $chunk );
81        $columns = end( $lines );
82
83        if ( $fileInfo ) {
84
85            if ( !$mapLines ) {
86                $this->generator->addMapping(
87                        $this->lineNumber + 1, // generated_line
88                        $this->column, // generated_column
89                        count( $sourceLines ), // original_line
90                        strlen( $sourceColumns ), // original_column
91                        $fileInfo
92                );
93            } else {
94                for ( $i = 0, $count = count( $lines ); $i < $count; $i++ ) {
95                    $this->generator->addMapping(
96                        $this->lineNumber + $i + 1, // generated_line
97                        $i === 0 ? $this->column : 0, // generated_column
98                        count( $sourceLines ) + $i, // original_line
99                        $i === 0 ? strlen( $sourceColumns ) : 0, // original_column
100                        $fileInfo
101                    );
102                }
103            }
104        }
105
106        if ( count( $lines ) === 1 ) {
107            $this->column += strlen( $columns );
108        } else {
109            $this->lineNumber += count( $lines ) - 1;
110            $this->column = strlen( $columns );
111        }
112
113        // add only chunk
114        parent::add( $chunk );
115    }
116
117}