Url.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Url
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Url extends Less_Tree {
  9. public $attrs;
  10. public $value;
  11. public $currentFileInfo;
  12. public $isEvald;
  13. public $type = 'Url';
  14. public function __construct( $value, $currentFileInfo = null, $isEvald = null ) {
  15. $this->value = $value;
  16. $this->currentFileInfo = $currentFileInfo;
  17. $this->isEvald = $isEvald;
  18. }
  19. public function accept( $visitor ) {
  20. $this->value = $visitor->visitObj( $this->value );
  21. }
  22. /**
  23. * @see Less_Tree::genCSS
  24. */
  25. public function genCSS( $output ) {
  26. $output->add( 'url(' );
  27. $this->value->genCSS( $output );
  28. $output->add( ')' );
  29. }
  30. /**
  31. * @param Less_Functions $ctx
  32. */
  33. public function compile( $ctx ) {
  34. $val = $this->value->compile( $ctx );
  35. if ( !$this->isEvald ) {
  36. // Add the base path if the URL is relative
  37. if ( Less_Parser::$options['relativeUrls']
  38. && $this->currentFileInfo
  39. && is_string( $val->value )
  40. && Less_Environment::isPathRelative( $val->value )
  41. ) {
  42. $rootpath = $this->currentFileInfo['uri_root'];
  43. if ( !$val->quote ) {
  44. $rootpath = preg_replace( '/[\(\)\'"\s]/', '\\$1', $rootpath );
  45. }
  46. $val->value = $rootpath . $val->value;
  47. }
  48. $val->value = Less_Environment::normalizePath( $val->value );
  49. }
  50. // Add cache buster if enabled
  51. if ( Less_Parser::$options['urlArgs'] ) {
  52. if ( !preg_match( '/^\s*data:/', $val->value ) ) {
  53. $delimiter = strpos( $val->value, '?' ) === false ? '?' : '&';
  54. $urlArgs = $delimiter . Less_Parser::$options['urlArgs'];
  55. $hash_pos = strpos( $val->value, '#' );
  56. if ( $hash_pos !== false ) {
  57. $val->value = substr_replace( $val->value, $urlArgs, $hash_pos, 0 );
  58. } else {
  59. $val->value .= $urlArgs;
  60. }
  61. }
  62. }
  63. return new Less_Tree_URL( $val, $this->currentFileInfo, true );
  64. }
  65. }