summaryrefslogtreecommitdiffstats
path: root/vendor/wikimedia/less.php/lib/Less/SourceMap
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/wikimedia/less.php/lib/Less/SourceMap')
-rw-r--r--vendor/wikimedia/less.php/lib/Less/SourceMap/Base64VLQ.php185
-rw-r--r--vendor/wikimedia/less.php/lib/Less/SourceMap/Generator.php354
2 files changed, 539 insertions, 0 deletions
diff --git a/vendor/wikimedia/less.php/lib/Less/SourceMap/Base64VLQ.php b/vendor/wikimedia/less.php/lib/Less/SourceMap/Base64VLQ.php
new file mode 100644
index 0000000..c27c6ed
--- /dev/null
+++ b/vendor/wikimedia/less.php/lib/Less/SourceMap/Base64VLQ.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * Encode / Decode Base64 VLQ.
+ *
+ * @private
+ */
+class Less_SourceMap_Base64VLQ {
+
+ /**
+ * Shift
+ *
+ * @var int
+ */
+ private $shift = 5;
+
+ /**
+ * Mask
+ *
+ * @var int
+ */
+ private $mask = 0x1F; // == (1 << shift) == 0b00011111
+
+ /**
+ * Continuation bit
+ *
+ * @var int
+ */
+ private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000
+
+ /**
+ * Char to integer map
+ *
+ * @var array
+ */
+ private $charToIntMap = [
+ 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
+ 'H' => 7,'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
+ 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
+ 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, 'a' => 26, 'b' => 27,
+ 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, 'g' => 32, 'h' => 33, 'i' => 34,
+ 'j' => 35, 'k' => 36, 'l' => 37, 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41,
+ 'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48,
+ 'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56,
+ 5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63,
+ ];
+
+ /**
+ * Integer to char map
+ *
+ * @var array
+ */
+ private $intToCharMap = [
+ 0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G',
+ 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N',
+ 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U',
+ 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z', 26 => 'a', 27 => 'b',
+ 28 => 'c', 29 => 'd', 30 => 'e', 31 => 'f', 32 => 'g', 33 => 'h', 34 => 'i',
+ 35 => 'j', 36 => 'k', 37 => 'l', 38 => 'm', 39 => 'n', 40 => 'o', 41 => 'p',
+ 42 => 'q', 43 => 'r', 44 => 's', 45 => 't', 46 => 'u', 47 => 'v', 48 => 'w',
+ 49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3',
+ 56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+',
+ 63 => '/',
+ ];
+
+ /**
+ * Constructor
+ */
+ public function __construct() {
+ // I leave it here for future reference
+ // foreach(str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') as $i => $char)
+ // {
+ // $this->charToIntMap[$char] = $i;
+ // $this->intToCharMap[$i] = $char;
+ // }
+ }
+
+ /**
+ * Convert from a two-complement value to a value where the sign bit is
+ * is placed in the least significant bit. For example, as decimals:
+ * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
+ * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
+ * We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297,
+ * even on a 64 bit machine.
+ * @param int $aValue
+ */
+ public function toVLQSigned( $aValue ) {
+ return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 );
+ }
+
+ /**
+ * Convert to a two-complement value from a value where the sign bit is
+ * is placed in the least significant bit. For example, as decimals:
+ * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
+ * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
+ * We assume that the value was generated with a 32 bit machine in mind.
+ * Hence
+ * 1 becomes -2147483648
+ * even on a 64 bit machine.
+ * @param int $aValue
+ */
+ public function fromVLQSigned( $aValue ) {
+ return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 );
+ }
+
+ /**
+ * Return the base 64 VLQ encoded value.
+ *
+ * @param int $aValue The value to encode
+ * @return string The encoded value
+ */
+ public function encode( $aValue ) {
+ $encoded = '';
+ $vlq = $this->toVLQSigned( $aValue );
+ do
+ {
+ $digit = $vlq & $this->mask;
+ $vlq = $this->zeroFill( $vlq, $this->shift );
+ if ( $vlq > 0 ) {
+ $digit |= $this->continuationBit;
+ }
+ $encoded .= $this->base64Encode( $digit );
+ } while ( $vlq > 0 );
+
+ return $encoded;
+ }
+
+ /**
+ * Return the value decoded from base 64 VLQ.
+ *
+ * @param string $encoded The encoded value to decode
+ * @return int The decoded value
+ */
+ public function decode( $encoded ) {
+ $vlq = 0;
+ $i = 0;
+ do
+ {
+ $digit = $this->base64Decode( $encoded[$i] );
+ $vlq |= ( $digit & $this->mask ) << ( $i * $this->shift );
+ $i++;
+ } while ( $digit & $this->continuationBit );
+
+ return $this->fromVLQSigned( $vlq );
+ }
+
+ /**
+ * Right shift with zero fill.
+ *
+ * @param int $a number to shift
+ * @param int $b number of bits to shift
+ * @return int
+ */
+ public function zeroFill( $a, $b ) {
+ return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) );
+ }
+
+ /**
+ * Encode single 6-bit digit as base64.
+ *
+ * @param int $number
+ * @return string
+ * @throws Exception If the number is invalid
+ */
+ public function base64Encode( $number ) {
+ if ( $number < 0 || $number > 63 ) {
+ throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', (string)$number ) );
+ }
+ return $this->intToCharMap[$number];
+ }
+
+ /**
+ * Decode single 6-bit digit from base64
+ *
+ * @param string $char
+ * @return int
+ * @throws Exception If the number is invalid
+ */
+ public function base64Decode( $char ) {
+ if ( !array_key_exists( $char, $this->charToIntMap ) ) {
+ throw new Exception( sprintf( 'Invalid base 64 digit "%s" given.', $char ) );
+ }
+ return $this->charToIntMap[$char];
+ }
+
+}
diff --git a/vendor/wikimedia/less.php/lib/Less/SourceMap/Generator.php b/vendor/wikimedia/less.php/lib/Less/SourceMap/Generator.php
new file mode 100644
index 0000000..7da9609
--- /dev/null
+++ b/vendor/wikimedia/less.php/lib/Less/SourceMap/Generator.php
@@ -0,0 +1,354 @@
+<?php
+/**
+ * Source map generator
+ *
+ * @private
+ */
+class Less_SourceMap_Generator extends Less_Configurable {
+
+ /**
+ * What version of source map does the generator generate?
+ */
+ private const VERSION = 3;
+
+ /**
+ * Array of default options
+ *
+ * @var array
+ */
+ protected $defaultOptions = [
+ // an optional source root, useful for relocating source files
+ // on a server or removing repeated values in the 'sources' entry.
+ // This value is prepended to the individual entries in the 'source' field.
+ 'sourceRoot' => '',
+
+ // an optional name of the generated code that this source map is associated with.
+ 'sourceMapFilename' => null,
+
+ // url of the map
+ 'sourceMapURL' => null,
+
+ // absolute path to a file to write the map to
+ 'sourceMapWriteTo' => null,
+
+ // output source contents?
+ 'outputSourceFiles' => false,
+
+ // base path for filename normalization
+ 'sourceMapRootpath' => '',
+
+ // base path for filename normalization
+ 'sourceMapBasepath' => ''
+ ];
+
+ /**
+ * The base64 VLQ encoder
+ *
+ * @var Less_SourceMap_Base64VLQ
+ */
+ protected $encoder;
+
+ /**
+ * Array of mappings
+ *
+ * @var array
+ */
+ protected $mappings = [];
+
+ /**
+ * The root node
+ *
+ * @var Less_Tree_Ruleset
+ */
+ protected $root;
+
+ /**
+ * Array of contents map
+ *
+ * @var array
+ */
+ protected $contentsMap = [];
+
+ /**
+ * File to content map
+ *
+ * @var array
+ */
+ protected $sources = [];
+ protected $source_keys = [];
+
+ /**
+ * Constructor
+ *
+ * @param Less_Tree_Ruleset $root The root node
+ * @param array $contentsMap
+ * @param array $options Array of options
+ */
+ public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = [] ) {
+ $this->root = $root;
+ $this->contentsMap = $contentsMap;
+ $this->encoder = new Less_SourceMap_Base64VLQ();
+
+ $this->SetOptions( $options );
+
+ $this->options['sourceMapRootpath'] = $this->fixWindowsPath( $this->options['sourceMapRootpath'], true );
+ $this->options['sourceMapBasepath'] = $this->fixWindowsPath( $this->options['sourceMapBasepath'], true );
+ }
+
+ /**
+ * Generates the CSS
+ *
+ * @return string
+ */
+ public function generateCSS() {
+ $output = new Less_Output_Mapped( $this->contentsMap, $this );
+
+ // catch the output
+ $this->root->genCSS( $output );
+
+ $sourceMapUrl = $this->getOption( 'sourceMapURL' );
+ $sourceMapFilename = $this->getOption( 'sourceMapFilename' );
+ $sourceMapContent = $this->generateJson();
+ $sourceMapWriteTo = $this->getOption( 'sourceMapWriteTo' );
+
+ if ( !$sourceMapUrl && $sourceMapFilename ) {
+ $sourceMapUrl = $this->normalizeFilename( $sourceMapFilename );
+ }
+
+ // write map to a file
+ if ( $sourceMapWriteTo ) {
+ $this->saveMap( $sourceMapWriteTo, $sourceMapContent );
+ }
+
+ // inline the map
+ if ( !$sourceMapUrl ) {
+ $sourceMapUrl = sprintf( 'data:application/json,%s', Less_Functions::encodeURIComponent( $sourceMapContent ) );
+ }
+
+ if ( $sourceMapUrl ) {
+ $output->add( sprintf( '/*# sourceMappingURL=%s */', $sourceMapUrl ) );
+ }
+
+ return $output->toString();
+ }
+
+ /**
+ * Saves the source map to a file
+ *
+ * @param string $file The absolute path to a file
+ * @param string $content The content to write
+ * @throws Exception If the file could not be saved
+ */
+ protected function saveMap( $file, $content ) {
+ $dir = dirname( $file );
+ // directory does not exist
+ if ( !is_dir( $dir ) ) {
+ // FIXME: create the dir automatically?
+ throw new Exception( sprintf( 'The directory "%s" does not exist. Cannot save the source map.', $dir ) );
+ }
+ // FIXME: proper saving, with dir write check!
+ if ( file_put_contents( $file, $content ) === false ) {
+ throw new Exception( sprintf( 'Cannot save the source map to "%s"', $file ) );
+ }
+ return true;
+ }
+
+ /**
+ * Normalizes the filename
+ *
+ * @param string $filename
+ * @return string
+ */
+ protected function normalizeFilename( $filename ) {
+ $filename = $this->fixWindowsPath( $filename );
+
+ $rootpath = $this->getOption( 'sourceMapRootpath' );
+ $basePath = $this->getOption( 'sourceMapBasepath' );
+
+ // "Trim" the 'sourceMapBasepath' from the output filename.
+ if ( is_string( $basePath ) && strpos( $filename, $basePath ) === 0 ) {
+ $filename = substr( $filename, strlen( $basePath ) );
+ }
+
+ // Remove extra leading path separators.
+ if ( strpos( $filename, '\\' ) === 0 || strpos( $filename, '/' ) === 0 ) {
+ $filename = substr( $filename, 1 );
+ }
+
+ return $rootpath . $filename;
+ }
+
+ /**
+ * Adds a mapping
+ *
+ * @param int $generatedLine The line number in generated file
+ * @param int $generatedColumn The column number in generated file
+ * @param int $originalLine The line number in original file
+ * @param int $originalColumn The column number in original file
+ * @param array $fileInfo The original source file
+ */
+ public function addMapping( $generatedLine, $generatedColumn, $originalLine, $originalColumn, $fileInfo ) {
+ $this->mappings[] = [
+ 'generated_line' => $generatedLine,
+ 'generated_column' => $generatedColumn,
+ 'original_line' => $originalLine,
+ 'original_column' => $originalColumn,
+ 'source_file' => $fileInfo['currentUri']
+ ];
+
+ $this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
+ }
+
+ /**
+ * Generates the JSON source map
+ *
+ * @return string
+ * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#
+ */
+ protected function generateJson() {
+ $sourceMap = [];
+ $mappings = $this->generateMappings();
+
+ // File version (always the first entry in the object) and must be a positive integer.
+ $sourceMap['version'] = self::VERSION;
+
+ // An optional name of the generated code that this source map is associated with.
+ $file = $this->getOption( 'sourceMapFilename' );
+ if ( $file ) {
+ $sourceMap['file'] = $file;
+ }
+
+ // An optional source root, useful for relocating source files on a server or removing repeated values in the 'sources' entry. This value is prepended to the individual entries in the 'source' field.
+ $root = $this->getOption( 'sourceRoot' );
+ if ( $root ) {
+ $sourceMap['sourceRoot'] = $root;
+ }
+
+ // A list of original sources used by the 'mappings' entry.
+ $sourceMap['sources'] = [];
+ foreach ( $this->sources as $source_uri => $source_filename ) {
+ $sourceMap['sources'][] = $this->normalizeFilename( $source_filename );
+ }
+
+ // A list of symbol names used by the 'mappings' entry.
+ $sourceMap['names'] = [];
+
+ // A string with the encoded mapping data.
+ $sourceMap['mappings'] = $mappings;
+
+ if ( $this->getOption( 'outputSourceFiles' ) ) {
+ // An optional list of source content, useful when the 'source' can't be hosted.
+ // The contents are listed in the same order as the sources above.
+ // 'null' may be used if some original sources should be retrieved by name.
+ $sourceMap['sourcesContent'] = $this->getSourcesContent();
+ }
+
+ // less.js compat fixes
+ if ( count( $sourceMap['sources'] ) && empty( $sourceMap['sourceRoot'] ) ) {
+ unset( $sourceMap['sourceRoot'] );
+ }
+
+ return json_encode( $sourceMap );
+ }
+
+ /**
+ * Returns the sources contents
+ *
+ * @return array|null
+ */
+ protected function getSourcesContent() {
+ if ( empty( $this->sources ) ) {
+ return;
+ }
+ $content = [];
+ foreach ( $this->sources as $sourceFile ) {
+ $content[] = file_get_contents( $sourceFile );
+ }
+ return $content;
+ }
+
+ /**
+ * Generates the mappings string
+ *
+ * @return string
+ */
+ public function generateMappings() {
+ if ( !count( $this->mappings ) ) {
+ return '';
+ }
+
+ $this->source_keys = array_flip( array_keys( $this->sources ) );
+
+ // group mappings by generated line number.
+ $groupedMap = $groupedMapEncoded = [];
+ foreach ( $this->mappings as $m ) {
+ $groupedMap[$m['generated_line']][] = $m;
+ }
+ ksort( $groupedMap );
+
+ $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0;
+
+ foreach ( $groupedMap as $lineNumber => $line_map ) {
+ while ( ++$lastGeneratedLine < $lineNumber ) {
+ $groupedMapEncoded[] = ';';
+ }
+
+ $lineMapEncoded = [];
+ $lastGeneratedColumn = 0;
+
+ foreach ( $line_map as $m ) {
+ $mapEncoded = $this->encoder->encode( $m['generated_column'] - $lastGeneratedColumn );
+ $lastGeneratedColumn = $m['generated_column'];
+
+ // find the index
+ if ( $m['source_file'] ) {
+ $index = $this->findFileIndex( $m['source_file'] );
+ if ( $index !== false ) {
+ $mapEncoded .= $this->encoder->encode( $index - $lastOriginalIndex );
+ $lastOriginalIndex = $index;
+
+ // lines are stored 0-based in SourceMap spec version 3
+ $mapEncoded .= $this->encoder->encode( $m['original_line'] - 1 - $lastOriginalLine );
+ $lastOriginalLine = $m['original_line'] - 1;
+
+ $mapEncoded .= $this->encoder->encode( $m['original_column'] - $lastOriginalColumn );
+ $lastOriginalColumn = $m['original_column'];
+ }
+ }
+
+ $lineMapEncoded[] = $mapEncoded;
+ }
+
+ $groupedMapEncoded[] = implode( ',', $lineMapEncoded ) . ';';
+ }
+
+ return rtrim( implode( $groupedMapEncoded ), ';' );
+ }
+
+ /**
+ * Finds the index for the filename
+ *
+ * @param string $filename
+ * @return int|false
+ */
+ protected function findFileIndex( $filename ) {
+ return $this->source_keys[$filename];
+ }
+
+ /**
+ * fix windows paths
+ * @param string $path
+ * @param bool $addEndSlash
+ * @return string
+ */
+ public function fixWindowsPath( $path, $addEndSlash = false ) {
+ $slash = ( $addEndSlash ) ? '/' : '';
+ if ( !empty( $path ) ) {
+ $path = str_replace( '\\', '/', $path );
+ $path = rtrim( $path, '/' ) . $slash;
+ }
+
+ return $path;
+ }
+
+}