function consume_quoted_link_title()

in src/_Private/consume_link_title.php [27:76]


function consume_quoted_link_title(string $input): ?(string, int) {
  $quote = $input[0];
  invariant(
    $quote === "'" || $quote === '"',
    'Should not be called without a quote',
  );
  $len = Str\length($input);

  $title = '';
  $idx = null;
  for ($idx = 1; $idx < $len; ++$idx) {
    $chr = $input[$idx];
    if ($chr === "\n" && ($input[$idx + 1] ?? null) === "\n") {
      return null;
    }
    if ($chr === $quote) {
      break;
    }

    if ($chr === "\\") {
      if ($idx + 1 < $len) {
        $next = $input[$idx + 1];
        if (C\contains_key(ASCII_PUNCTUATION, $next)) {
          $title .= $next;
          ++$idx;
          continue;
        }
      }
    }

    if ($chr === '&') {
      $rest = Str\slice($input, $idx);
      $result = decode_html_entity($rest);
      if ($result !== null) {
        list($match, $entity, $_rest) = $result;
        $title .= $entity;
        $idx += Str\length($match) - 1;
        continue;
      }
    }

    $title .= $chr;
  }

  if ($idx === $len) {
    return null;
  }

  return tuple($title, $idx + 1);
}