You are here

protected function Minifier::saveString in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 8.4 advagg_js_minify/jshrink.inc \JShrink\Minifier::saveString()
  2. 8.2 advagg_js_minify/jshrink.inc \JShrink\Minifier::saveString()
  3. 8.3 advagg_js_minify/jshrink.inc \JShrink\Minifier::saveString()

When a javascript string is detected this function crawls for the end of it and saves the whole string.

Throws

\RuntimeException Unclosed strings will throw an error

1 call to Minifier::saveString()
Minifier::loop in advagg_js_compress/jshrink.inc
The primary action occurs here. This function loops through the input string, outputting anything that's relevant and discarding anything that is not.

File

advagg_js_compress/jshrink.inc, line 484

Class

Minifier
Minifier

Namespace

JShrink

Code

protected function saveString() {
  $startpos = $this->index;

  // saveString is always called after a gets cleared, so we push b into
  // that spot.
  $this->a = $this->b;

  // If this isn't a string we don't need to do anything.
  if (!in_array($this->a, $this->stringDelimiters)) {
    return;
  }

  // String type is the quote used, " or '
  $stringType = $this->a;

  // Echo out that starting quote
  echo $this->a;

  // Loop until the string is done
  while (true) {

    // Grab the very next character and load it into a
    $this->a = $this
      ->getChar();
    switch ($this->a) {

      // If the string opener (single or double quote) is used
      // output it and break out of the while loop-
      // The string is finished!
      case $stringType:
        break 2;

      // New lines in strings without line delimiters are bad- actual
      // new lines will be represented by the string \n and not the actual
      // character, so those will be treated just fine using the switch
      // block below.
      case "\n":
        if ($stringType === '`') {
          echo $this->a;
        }
        else {
          throw new \RuntimeException('Unclosed string at position: ' . $startpos);
        }
        break;

      // Escaped characters get picked up here. If it's an escaped new line it's not really needed
      case '\\':

        // a is a slash. We want to keep it, and the next character,
        // unless it's a new line. New lines as actual strings will be
        // preserved, but escaped new lines should be reduced.
        $this->b = $this
          ->getChar();

        // If b is a new line we discard a and b and restart the loop.
        if ($this->b === "\n") {
          break;
        }

        // echo out the escaped character and restart the loop.
        echo $this->a . $this->b;
        break;

      // Since we're not dealing with any special cases we simply
      // output the character and continue our loop.
      default:
        echo $this->a;
    }
  }
}