Browse Source

Don't emit errors if positions are not tracked

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

5
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,6 +224,7 @@ 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->track) {
if ($this->eof) {
$relativePos++;
if ($this->astrals[$this->data->posChar()] ?? false) {
@ -263,6 +265,9 @@ class Data {
} else {
return [$this->_line, $this->_column + $relativePos];
}
} else {
return [0, 0];
}
}
public function __get($property) {

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;
}
}

4
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;
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