Browse Source

Improve coverage

split-manual
J. King 3 years ago
parent
commit
d319be477e
  1. 1
      lib/ActiveFormattingElementsList.php
  2. 5
      lib/Data.php
  3. 14
      lib/OpenElementsStack.php
  4. 2
      lib/Stack.php
  5. 3
      lib/TemplateInsertionModesStack.php
  6. 11
      lib/Token.php
  7. 16
      lib/TreeBuilder.php
  8. 3
      tests/cases/TestTokenizer.php
  9. 1
      tests/cases/TestTreeConstructor.php

1
lib/ActiveFormattingElementsList.php

@ -158,6 +158,7 @@ class ActiveFormattingElementsList extends Stack {
}
}
/** @codeCoverageIgnore */
public function __toString(): string {
$out = [];
foreach ($this as $entry) {

5
lib/Data.php

@ -46,9 +46,7 @@ class Data {
$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;
}
$this->track = (bool) (error_reporting() & \E_USER_WARNING);
if ($encoding = Charset::fromBOM($data)) {
// encoding determined from Unicode byte order mark
@ -67,7 +65,6 @@ class Data {
$this->data = Encoding::createDecoder($encoding, $data, false, true);
}
public function consume(int $length = 1, $advancePointer = true): string {
assert($length > 0, new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length));

14
lib/OpenElementsStack.php

@ -135,7 +135,7 @@ class OpenElementsStack extends Stack {
public function insert(Element $element, ?int $at = null): void {
assert($at === null || ($at >= 0 && $at <= count($this->_storage)), new Exception(Exception::STACK_INVALID_INDEX, $at));
if ($at === null) {
$this[] = $element;
$this[] = $element; // @codeCoverageIgnore
} else {
array_splice($this->_storage, $at, 0, [$element]);
}
@ -329,8 +329,8 @@ class OpenElementsStack extends Stack {
# since the loop will always terminate in the previous step
# if the top of the stack — an html element — is reached.)
}
assert(false, new Exception(Exception::STACK_INVALID_STATE, (string)$this));
}
assert(false, new Exception(Exception::STACK_INVALID_STATE, (string)$this)); // @codeCoverageIgnore
} // @codeCoverageIgnore
protected function computeProperties(): void {
$this->count = count($this->_storage);
@ -348,15 +348,15 @@ class OpenElementsStack extends Stack {
$this->currentNodeName = $this->currentNode->nodeName;
$this->currentNodeNamespace = $this->currentNode->namespaceURI;
} else {
$this->currentNodeName = null;
$this->currentNodeNamespace = null;
$this->currentNodeName = null; // @codeCoverageIgnore
$this->currentNodeNamespace = null; // @codeCoverageIgnore
}
if ($this->adjustedCurrentNode) {
$this->adjustedCurrentNodeName = $this->adjustedCurrentNode->nodeName;
$this->adjustedCurrentNodeNamespace = $this->adjustedCurrentNode->namespaceURI;
} else {
$this->adjustedCurrentNodeName = null;
$this->adjustedCurrentNodeNamespace = null;
$this->adjustedCurrentNodeName = null; // @codeCoverageIgnore
$this->adjustedCurrentNodeNamespace = null; // @codeCoverageIgnore
}
}

2
lib/Stack.php

@ -12,7 +12,7 @@ abstract class Stack implements \ArrayAccess, \Countable, \IteratorAggregate {
if (is_null($offset)) {
$this->_storage[] = $value;
} else {
$this->_storage[$offset] = $value;
$this->_storage[$offset] = $value; // @codeCoverageIgnore
}
$this->count = count($this->_storage);
}

3
lib/TemplateInsertionModesStack.php

@ -4,11 +4,12 @@ namespace dW\HTML5;
class TemplateInsertionModesStack extends Stack {
public function __get($property) {
assert($property === "currentMode", new \Exception("Property $property is invalid"));
switch ($property) {
case 'currentMode':
return $this->isEmpty() ? null : $this->top();
default:
return null;
return null; // @codeCoverageIgnore
}
}
}

11
lib/Token.php

@ -79,17 +79,6 @@ abstract class TagToken extends Token {
return (isset($this->attributes[$key])) ? $this->attributes[$key] : null;
}
public function setAttribute(string $name, string $value) {
$key = $this->_getAttributeKey($name);
if (is_null($key)) {
$this->attributes[] = new TokenAttr($name, $value);
} else {
$attribute = &$this->attributes[$key];
$attribute->name = $name;
$attribute->value = $value;
}
}
private function _getAttributeKey(string $name) {
foreach ($this->attributes as $key => $a) {
if ($a->name === $name) {

16
lib/TreeBuilder.php

@ -261,7 +261,7 @@ class TreeBuilder {
$this->stack[] = $dom->documentElement;
# If the context element is a template element, push "in template" onto the stack of
# template insertion modes so that it is the new current template insertion mode.
if ($fragmentContext->nodeName === "template" && !$fragmentContext->namespaceURI) {
if ($fragmentContext->nodeName === "template" && $fragmentContext->namespaceURI === null) {
$this->templateInsertionModes[] = self::IN_TEMPLATE_MODE;
}
# Create a start tag token whose name is the local name of context and whose attributes are the attributes of context.
@ -275,7 +275,7 @@ class TreeBuilder {
# the form element pointer keeps its initial value, null.)
$node = $fragmentContext;
do {
if ($node->nodeName === "form" && !$fragmentContext->namespaceURI) {
if ($node->nodeName === "form" && $fragmentContext->namespaceURI === null) {
$this->formElement = $node;
break;
}
@ -1094,8 +1094,8 @@ class TreeBuilder {
# not, add the attribute and its corresponding value to that element.
$top = $this->stack[0];
foreach ($token->attributes as $a) {
if (!$top->hasAttribute($a->name)) {
$top->setAttribute($a->name, $a->value);
if (!$top->hasAttributeNS(null, $a->name)) {
$top->setAttributeNS(null, $a->name, $a->value);
}
}
}
@ -1122,8 +1122,8 @@ class TreeBuilder {
$this->framesetOk = false;
$body = $this->stack[1];
foreach ($token->attributes as $a) {
if (!$body->hasAttribute($a->name)) {
$body->setAttribute($a->name, $a->value);
if (!$body->hasAttributeNS(null, $a->name)) {
$body->setAttributeNS(null, $a->name, $a->value);
}
}
}
@ -4269,12 +4269,10 @@ class TreeBuilder {
protected function createElementForToken(TagToken $token, ?string $namespace = null, ?\DOMNode $intendedParent = null): Element {
// DEVIATION: Steps related to scripting have been elided entirely
# Let document be intended parent's node document.
$document = $this->DOM;
# Let local name be the tag name of the token.
$localName = $token->name;
# Let element be the result of creating an element given document,
# localName, given namespace, null, and is.
$element = $document->createElementNS($namespace, $localName);
$element = $this->DOM->createElementNS($namespace, $token->name);
# Append each attribute in the given token to element.
foreach ($token->attributes as $attr) {
# If element has an xmlns attribute in the XMLNS namespace whose value

3
tests/cases/TestTokenizer.php

@ -13,6 +13,7 @@ use dW\HTML5\DOCTYPEToken;
use dW\HTML5\EndTagToken;
use dW\HTML5\NullCharacterToken;
use dW\HTML5\StartTagToken;
use dW\HTML5\TokenAttr;
use dW\HTML5\WhitespaceToken;
/**
@ -161,7 +162,7 @@ class TestTokenizer extends \PHPUnit\Framework\TestCase {
case "StartTag":
$t = new StartTagToken($token[1], $token[3] ?? false);
foreach ($token[2] ?? [] as $name => $value) {
$t->setAttribute((string) $name, $value);
$t->attributes[] = new TokenAttr((string) $name, $value);
}
$tokens[] = $t;
break;

1
tests/cases/TestTreeConstructor.php

@ -23,6 +23,7 @@ use dW\HTML5\TreeBuilder;
* @covers \dW\HTML5\TemplateInsertionModesStack
* @covers \dW\HTML5\OpenElementsStack
* @covers \dW\HTML5\Stack
* @covers \dW\HTML5\TagToken
*/
class TestTreeConstructor extends \PHPUnit\Framework\TestCase {
use \dW\HTML5\EscapeString;

Loading…
Cancel
Save