Browse Source

Don't emit errors if positions are not tracked

ns
J. King 3 years ago
parent
commit
ef2cb5a835
  1. 73
      lib/Data.php
  2. 17
      lib/ParseErrorDummy.php
  3. 6
      lib/Parser.php

73
lib/Data.php

@ -45,6 +45,7 @@ class Data {
$this->errorHandler = $errorHandler ?? new ParseError;
$this->filePath = $filePath;
$encodingOrContentType = (string) $encodingOrContentType;
// don't track the current line/column position if erroro reporting has been suppressed
if (!(error_reporting() & \E_USER_WARNING)) {
$this->track = false;
}
@ -223,45 +224,49 @@ class Data {
/** Returns an indexed array with the line and column positions of the requested offset from the current position */
public function whereIs(int $relativePos): array {
if ($this->eof) {
$relativePos++;
if ($this->astrals[$this->data->posChar()] ?? false) {
if ($this->track) {
if ($this->eof) {
$relativePos++;
if ($this->astrals[$this->data->posChar()] ?? false) {
$relativePos++;
}
}
}
if ($relativePos === 0) {
if (!$this->_column && $this->_line > 1) {
return [$this->_line - 1, $this->newlines[$this->data->posChar()] + 1];
} else {
return [$this->_line, $this->_column];
}
} elseif ($relativePos < 0) {
$pos = $this->data->posChar();
$line = $this->_line;
$col = $this->_column;
do {
// If the current position is the start of a line,
// get the column position of the end of the previous line
if (isset($this->newlines[$pos])) {
$line--;
$col = $this->newlines[$pos];
// If the newline was a normalized CR+LF pair,
// go back one extra character
if (isset($this->normalized[$pos])) {
$pos--;
}
if ($relativePos === 0) {
if (!$this->_column && $this->_line > 1) {
return [$this->_line - 1, $this->newlines[$this->data->posChar()] + 1];
} else {
$col--;
// supplementary plane characters count as two
if ($this->astrals[$pos] ?? false) {
$this->_column--;
}
return [$this->_line, $this->_column];
}
$pos--;
} while (++$relativePos < 0);
return [$line, $col];
} elseif ($relativePos < 0) {
$pos = $this->data->posChar();
$line = $this->_line;
$col = $this->_column;
do {
// If the current position is the start of a line,
// get the column position of the end of the previous line
if (isset($this->newlines[$pos])) {
$line--;
$col = $this->newlines[$pos];
// If the newline was a normalized CR+LF pair,
// go back one extra character
if (isset($this->normalized[$pos])) {
$pos--;
}
} else {
$col--;
// supplementary plane characters count as two
if ($this->astrals[$pos] ?? false) {
$this->_column--;
}
}
$pos--;
} while (++$relativePos < 0);
return [$line, $col];
} else {
return [$this->_line, $this->_column + $relativePos];
}
} else {
return [$this->_line, $this->_column + $relativePos];
return [0, 0];
}
}

17
lib/ParseErrorDummy.php

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace dW\HTML5;
class ParseErrorDummy extends ParseError {
public function setHandler() {
// Do nothing
}
public function clearHandler() {
// Do nothing
}
public function emit(string $file, int $line, int $column, int $code, ...$arg): bool {
return false;
}
}

6
lib/Parser.php

@ -25,7 +25,11 @@ class Parser {
public static function parse(string $data, ?Document $document = null, ?string $encodingOrContentType = null, ?\DOMElement $fragmentContext = null, ?String $file = null): Document {
// Initialize the various classes needed for parsing
$document = $document ?? new Document;
$errorHandler = new ParseError;
if ((error_reporting() & \E_USER_WARNING)) {
$errorHandler = new ParseError;
} else {
$errorHandler = new ParseErrorDummy;
}
$decoder = new Data($data, $file ?? "STDIN", $errorHandler, $encodingOrContentType);
$document->documentEncoding = $decoder->encoding;
$stack = new OpenElementsStack($fragmentContext);

Loading…
Cancel
Save