Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   2  
   3  namespace Sabberworm\CSS\Value;
   4  
   5  use Sabberworm\CSS\OutputFormat;
   6  use Sabberworm\CSS\Parsing\ParserState;
   7  use Sabberworm\CSS\Parsing\SourceException;
   8  use Sabberworm\CSS\Parsing\UnexpectedEOFException;
   9  use Sabberworm\CSS\Parsing\UnexpectedTokenException;
  10  
  11  class CSSString extends PrimitiveValue
  12  {
  13      /**
  14       * @var string
  15       */
  16      private $sString;
  17  
  18      /**
  19       * @param string $sString
  20       * @param int $iLineNo
  21       */
  22      public function __construct($sString, $iLineNo = 0)
  23      {
  24          $this->sString = $sString;
  25          parent::__construct($iLineNo);
  26      }
  27  
  28      /**
  29       * @return CSSString
  30       *
  31       * @throws SourceException
  32       * @throws UnexpectedEOFException
  33       * @throws UnexpectedTokenException
  34       */
  35      public static function parse(ParserState $oParserState)
  36      {
  37          $sBegin = $oParserState->peek();
  38          $sQuote = null;
  39          if ($sBegin === "'") {
  40              $sQuote = "'";
  41          } elseif ($sBegin === '"') {
  42              $sQuote = '"';
  43          }
  44          if ($sQuote !== null) {
  45              $oParserState->consume($sQuote);
  46          }
  47          $sResult = "";
  48          $sContent = null;
  49          if ($sQuote === null) {
  50              // Unquoted strings end in whitespace or with braces, brackets, parentheses
  51              while (!preg_match('/[\\s{}()<>\\[\\]]/isu', $oParserState->peek())) {
  52                  $sResult .= $oParserState->parseCharacter(false);
  53              }
  54          } else {
  55              while (!$oParserState->comes($sQuote)) {
  56                  $sContent = $oParserState->parseCharacter(false);
  57                  if ($sContent === null) {
  58                      throw new SourceException(
  59                          "Non-well-formed quoted string {$oParserState->peek(3)}",
  60                          $oParserState->currentLine()
  61                      );
  62                  }
  63                  $sResult .= $sContent;
  64              }
  65              $oParserState->consume($sQuote);
  66          }
  67          return new CSSString($sResult, $oParserState->currentLine());
  68      }
  69  
  70      /**
  71       * @param string $sString
  72       *
  73       * @return void
  74       */
  75      public function setString($sString)
  76      {
  77          $this->sString = $sString;
  78      }
  79  
  80      /**
  81       * @return string
  82       */
  83      public function getString()
  84      {
  85          return $this->sString;
  86      }
  87  
  88      /**
  89       * @return string
  90       */
  91      public function __toString()
  92      {
  93          return $this->render(new OutputFormat());
  94      }
  95  
  96      /**
  97       * @return string
  98       */
  99      public function render(OutputFormat $oOutputFormat)
 100      {
 101          $sString = addslashes($this->sString);
 102          $sString = str_replace("\n", '\A', $sString);
 103          return $oOutputFormat->getStringQuotingType() . $sString . $oOutputFormat->getStringQuotingType();
 104      }
 105  }