Merge branch 'MDL-60125-master' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / lessphp / Output / Mapped.php
blob9e4cc46194ed67471c40d816609121b49953532e
1 <?php
3 /**
4 * Parser output with source map
6 * @package Less
7 * @subpackage Output
8 */
9 class Less_Output_Mapped extends Less_Output {
11 /**
12 * The source map generator
14 * @var Less_SourceMap_Generator
16 protected $generator;
18 /**
19 * Current line
21 * @var integer
23 protected $lineNumber = 0;
25 /**
26 * Current column
28 * @var integer
30 protected $column = 0;
32 /**
33 * Array of contents map (file and its content)
35 * @var array
37 protected $contentsMap = array();
39 /**
40 * Constructor
42 * @param array $contentsMap Array of filename to contents map
43 * @param Less_SourceMap_Generator $generator
45 public function __construct(array $contentsMap, $generator){
46 $this->contentsMap = $contentsMap;
47 $this->generator = $generator;
50 /**
51 * Adds a chunk to the stack
52 * The $index for less.php may be different from less.js since less.php does not chunkify inputs
54 * @param string $chunk
55 * @param string $fileInfo
56 * @param integer $index
57 * @param mixed $mapLines
59 public function add($chunk, $fileInfo = null, $index = 0, $mapLines = null){
61 //ignore adding empty strings
62 if( $chunk === '' ){
63 return;
67 $sourceLines = array();
68 $sourceColumns = ' ';
71 if( $fileInfo ){
73 $url = $fileInfo['currentUri'];
75 if( isset($this->contentsMap[$url]) ){
76 $inputSource = substr($this->contentsMap[$url], 0, $index);
77 $sourceLines = explode("\n", $inputSource);
78 $sourceColumns = end($sourceLines);
79 }else{
80 throw new Exception('Filename '.$url.' not in contentsMap');
85 $lines = explode("\n", $chunk);
86 $columns = end($lines);
88 if($fileInfo){
90 if(!$mapLines){
91 $this->generator->addMapping(
92 $this->lineNumber + 1, // generated_line
93 $this->column, // generated_column
94 count($sourceLines), // original_line
95 strlen($sourceColumns), // original_column
96 $fileInfo
98 }else{
99 for($i = 0, $count = count($lines); $i < $count; $i++){
100 $this->generator->addMapping(
101 $this->lineNumber + $i + 1, // generated_line
102 $i === 0 ? $this->column : 0, // generated_column
103 count($sourceLines) + $i, // original_line
104 $i === 0 ? strlen($sourceColumns) : 0, // original_column
105 $fileInfo
111 if(count($lines) === 1){
112 $this->column += strlen($columns);
113 }else{
114 $this->lineNumber += count($lines) - 1;
115 $this->column = strlen($columns);
118 // add only chunk
119 parent::add($chunk);