Mapped.php 2.5 KB

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