Browse Source

Added tests for Element::innerText

master
Dustin Wilson 2 years ago
parent
commit
5e3c3d2ddb
  1. 30
      composer.lock
  2. 22
      lib/Element.php
  3. 51
      tests/cases/TestElement.php
  4. 12
      vendor-bin/phpunit/composer.lock

30
composer.lock

@ -568,16 +568,16 @@
},
{
"name": "guzzlehttp/guzzle",
"version": "7.4.0",
"version": "7.4.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "868b3571a039f0ebc11ac8f344f4080babe2cb94"
"reference": "ee0a041b1760e6a53d2a39c8c34115adc2af2c79"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/868b3571a039f0ebc11ac8f344f4080babe2cb94",
"reference": "868b3571a039f0ebc11ac8f344f4080babe2cb94",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/ee0a041b1760e6a53d2a39c8c34115adc2af2c79",
"reference": "ee0a041b1760e6a53d2a39c8c34115adc2af2c79",
"shasum": ""
},
"require": {
@ -586,7 +586,7 @@
"guzzlehttp/psr7": "^1.8.3 || ^2.1",
"php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0",
"symfony/deprecation-contracts": "^2.2"
"symfony/deprecation-contracts": "^2.2 || ^3.0"
},
"provide": {
"psr/http-client-implementation": "1.0"
@ -672,7 +672,7 @@
],
"support": {
"issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.4.0"
"source": "https://github.com/guzzle/guzzle/tree/7.4.1"
},
"funding": [
{
@ -688,7 +688,7 @@
"type": "tidelift"
}
],
"time": "2021-10-18T09:52:00+00:00"
"time": "2021-12-06T18:43:05+00:00"
},
{
"name": "guzzlehttp/promises",
@ -1544,25 +1544,25 @@
},
{
"name": "symfony/deprecation-contracts",
"version": "v2.5.0",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "6f981ee24cf69ee7ce9736146d1c57c2780598a8"
"reference": "c726b64c1ccfe2896cb7df2e1331c357ad1c8ced"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/6f981ee24cf69ee7ce9736146d1c57c2780598a8",
"reference": "6f981ee24cf69ee7ce9736146d1c57c2780598a8",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/c726b64c1ccfe2896cb7df2e1331c357ad1c8ced",
"reference": "c726b64c1ccfe2896cb7df2e1331c357ad1c8ced",
"shasum": ""
},
"require": {
"php": ">=7.1"
"php": ">=8.0.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "2.5-dev"
"dev-main": "3.0-dev"
},
"thanks": {
"name": "symfony/contracts",
@ -1591,7 +1591,7 @@
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.0"
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.0.0"
},
"funding": [
{
@ -1607,7 +1607,7 @@
"type": "tidelift"
}
],
"time": "2021-07-12T14:48:14+00:00"
"time": "2021-11-01T23:48:49+00:00"
},
{
"name": "symfony/http-foundation",

22
lib/Element.php

@ -121,9 +121,9 @@ class Element extends Node {
# 2. Replace all with fragment within this.
$innerNode = $this->innerNode;
$children = $innerNode->childNodes();
$children = $innerNode->childNodes;
while ($innerNode->hasChildNodes()) {
$innerNode->removeChild($this->innerNode->firstChild);
$innerNode->removeChild($innerNode->firstChild);
}
// Check for child nodes before appending to prevent a stupid warning.
@ -697,8 +697,8 @@ class Element extends Node {
protected function getRenderedTextFragment(string $input): \DOMDocumentFragment {
# The rendered text fragment for a string input given a Document document is the
# result of running the following steps:
$fragment = $this->innerNode->createDocumentFragment();
$innerNode = $this->innerNode;
$fragment = $innerNode->ownerDocument->createDocumentFragment();
# 1. Let position be a position variable for input, initially pointing at the
# start of input.
@ -712,21 +712,19 @@ class Element extends Node {
while ($position < $strlen) {
# 1. Collect a sequence of code points that are not U+000A LF or U+000D CR from input
# given position, and set text to the result.
$chr = substr($input, $position, 1);
$p = $position;
while (!in_array($input[$p], [ "\n", "\r" ])) {
$text .= $input[$p];
$p++;
while (isset($input[$position]) && !in_array($input[$position], [ "\n", "\r" ])) {
$text .= $input[$position];
$position++;
}
# 2. If text is not the empty string, then append a new Text node whose data is
# text and node document is document to fragment.
if ($text !== '') {
$fragment->appendChild($this->innerNode->ownerDocument->createTextNode($text));
$fragment->appendChild($innerNode->ownerDocument->createTextNode($text));
}
# 3. While position is not past the end of input, and the code point at position
# is either U+000A LF or U+000D CR:
# is either U+000A LF or U+000D CR:
while ($position < $strlen && in_array($input[$position], [ "\n", "\r" ])) {
# 1. If the code point at position is U+000D CR and the next code point is
# U+000A LF, then advance position to the next code point in input.
@ -739,7 +737,7 @@ class Element extends Node {
# 3. Append the result of creating an element given document, br, and the HTML
# namespace to fragment.
$fragment->appendChild($this->innerNode->ownerDocument->createElement('br'));
$fragment->appendChild($innerNode->ownerDocument->createElement('br'));
}
}

51
tests/cases/TestElement.php

@ -1372,6 +1372,57 @@ class TestElement extends \PHPUnit\Framework\TestCase {
}
/**
* @covers \MensBeam\HTML\DOM\Element::__get_innerText
* @covers \MensBeam\HTML\DOM\Element::__set_innerText
*
* @covers \MensBeam\HTML\DOM\Document::__construct
* @covers \MensBeam\HTML\DOM\Document::__get_body
* @covers \MensBeam\HTML\DOM\Document::__get_documentElement
* @covers \MensBeam\HTML\DOM\Document::createElement
* @covers \MensBeam\HTML\DOM\Document::createTextNode
* @covers \MensBeam\HTML\DOM\DOMImplementation::__construct
* @covers \MensBeam\HTML\DOM\Element::__construct
* @covers \MensBeam\HTML\DOM\Element::__get_innerHTML
* @covers \MensBeam\HTML\DOM\Element::getRenderedTextFragment
* @covers \MensBeam\HTML\DOM\Node::__construct
* @covers \MensBeam\HTML\DOM\Node::__get_textContent
* @covers \MensBeam\HTML\DOM\Node::appendChild
* @covers \MensBeam\HTML\DOM\Node::getInnerDocument
* @covers \MensBeam\HTML\DOM\Node::getInnerNode
* @covers \MensBeam\HTML\DOM\Node::getRootNode
* @covers \MensBeam\HTML\DOM\Node::postInsertionBugFixes
* @covers \MensBeam\HTML\DOM\Node::preInsertionBugFixes
* @covers \MensBeam\HTML\DOM\Node::preInsertionValidity
* @covers \MensBeam\HTML\DOM\Text::__construct
* @covers \MensBeam\HTML\DOM\Inner\Document::__construct
* @covers \MensBeam\HTML\DOM\Inner\Document::__get_wrapperNode
* @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::get
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::has
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::key
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::set
* @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor
* @covers \MensBeam\HTML\DOM\Inner\Reflection::getProtectedProperty
*/
public function testProperty_innerText() {
$d = new Document();
$d->appendChild($d->createElement('html'));
$d->documentElement->appendChild($d->createElement('body'));
$s = $d->body->appendChild($d->createElement('span'));
$s->appendChild($d->createTextNode('ook'));
$this->assertSame('<span>ook</span>', $d->body->innerHTML);
$d->body->innerText = <<<TEXT
ook
eek ook
TEXT;
$this->assertSame('ookook eek ook', $d->body->innerText);
$this->assertSame('ook<br><br>ook eek ook', $d->body->innerHTML);
}
/**
* @covers \MensBeam\HTML\DOM\Element::__get_outerHTML
* @covers \MensBeam\HTML\DOM\Element::__set_outerHTML

12
vendor-bin/phpunit/composer.lock

@ -462,16 +462,16 @@
},
{
"name": "phpspec/prophecy",
"version": "1.14.0",
"version": "v1.15.0",
"source": {
"type": "git",
"url": "https://github.com/phpspec/prophecy.git",
"reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e"
"reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpspec/prophecy/zipball/d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e",
"reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e",
"url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13",
"reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13",
"shasum": ""
},
"require": {
@ -523,9 +523,9 @@
],
"support": {
"issues": "https://github.com/phpspec/prophecy/issues",
"source": "https://github.com/phpspec/prophecy/tree/1.14.0"
"source": "https://github.com/phpspec/prophecy/tree/v1.15.0"
},
"time": "2021-09-10T09:02:12+00:00"
"time": "2021-12-08T12:19:24+00:00"
},
{
"name": "phpunit/php-code-coverage",

Loading…
Cancel
Save