Skip to content

Commit

Permalink
Replaced the polyfill implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Dec 21, 2023
1 parent 96af1a6 commit 3ab14a0
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Tests

on:
push:
branches: [ master, Workarounds ]
branches: [ master ]
pull_request:
branches: [ master ]

Expand Down
2 changes: 2 additions & 0 deletions src/ForwardCompatibleNodes/DocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace s9e\SweetDOM\ForwardCompatibleNodes;

use s9e\SweetDOM\DocumentFragment as ParentClass;
use s9e\SweetDOM\NodeTraits\ParentNodePolyfill;

class DocumentFragment extends ParentClass
{
use ParentNodePolyfill;
}
2 changes: 2 additions & 0 deletions src/ForwardCompatibleNodes/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use s9e\SweetDOM\Element as ParentClass;
use s9e\SweetDOM\NodeTraits\ChildNodeForwardCompatibility;
use s9e\SweetDOM\NodeTraits\ParentNodePolyfill;

class Element extends ParentClass
{
use ChildNodeForwardCompatibility;
use ParentNodePolyfill;
}
20 changes: 14 additions & 6 deletions src/NodeTraits/DeprecatedMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
*/
namespace s9e\SweetDOM\NodeTraits;

use const ENT_COMPAT, ENT_XML1, E_USER_DEPRECATED;
use DOMException;
use const DOM_SYNTAX_ERR, ENT_COMPAT, ENT_XML1, E_USER_DEPRECATED;
use function array_flip, htmlspecialchars, preg_match, preg_match_all, preg_replace_callback, strtolower, trigger_error;

/**
* @method mixed polyfillMethodsCall(string $name, array $arguments)
* @method mixed magicMethodsCall(string $name, array $arguments)
*/
trait DeprecatedMethods
{
use PolyfillMethods
use MagicMethods
{
PolyfillMethods::__call as polyfillMethodsCall;
MagicMethods::__call as magicMethodsCall;
}

public function __call(string $name, array $arguments)
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __call(string $name, array $arguments)

}

return $this->polyfillMethodsCall($name, $arguments);
return $this->magicMethodsCall($name, $arguments);
}

/**
Expand All @@ -58,7 +59,14 @@ public function insertAdjacentXML(string $where, string $xml): void
$fragment = $this->ownerDocument->createDocumentFragment();
$fragment->appendXML($this->addMissingNamespaceDeclarations($xml));

$this->insertAdjacentNode($where, $fragment);
match (strtolower($where))
{
'afterbegin' => $this->prepend($fragment),
'afterend' => $this->after($fragment),
'beforebegin' => $this->before($fragment),
'beforeend' => $this->appendChild($fragment),
default => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR)
};
}

/**
Expand Down
58 changes: 58 additions & 0 deletions src/NodeTraits/ParentNodePolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);

/**
* @package s9e\SweetDOM
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\SweetDOM\NodeTraits;

use DOMElement;
use DOMException;
use DOMNode;
use const DOM_SYNTAX_ERR;
use function strtolower;

trait ParentNodePolyfill
{
public function insertAdjacentElement(string $where, DOMElement $element): ?DOMElement
{
$this->insertAdjacentNode($where, $element);

return $element;
}

public function insertAdjacentText(string $where, string $data): void
{
$node = $this->ownerDocument->createTextNode($data);
$this->insertAdjacentNode($where, $node);
}

public function replaceChildren(...$nodes): void
{
while (isset($this->lastChild))
{
$this->lastChild->remove();
}
$this->append(...$nodes);
}

/**
* Insert given node relative to this element's position
*
* @param string $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
* @param DOMNode $node
* @return void
*/
private function insertAdjacentNode(string $where, DOMNode $node): void
{
match (strtolower($where))
{
'afterbegin' => $this->prepend($node),
'afterend' => $this->after($node),
'beforebegin' => $this->before($node),
'beforeend' => $this->appendChild($node),
default => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR)
};
}
}
78 changes: 0 additions & 78 deletions src/NodeTraits/PolyfillMethods.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/PatchedNodes/DocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
namespace s9e\SweetDOM\PatchedNodes;

use s9e\SweetDOM\DocumentFragment as ParentClass;
use s9e\SweetDOM\NodeTraits\ParentNodePolyfill;
use s9e\SweetDOM\NodeTraits\ParentNodeWorkarounds;

class DocumentFragment extends ParentClass
{
use ParentNodePolyfill;
use ParentNodeWorkarounds;
}
2 changes: 2 additions & 0 deletions src/PatchedNodes/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

use s9e\SweetDOM\Element as ParentClass;
use s9e\SweetDOM\NodeTraits\ChildNodeWorkarounds;
use s9e\SweetDOM\NodeTraits\ParentNodePolyfill;
use s9e\SweetDOM\NodeTraits\ParentNodeWorkarounds;

class Element extends ParentClass
{
use ChildNodeWorkarounds;
use ParentNodePolyfill;
use ParentNodeWorkarounds;
}
26 changes: 15 additions & 11 deletions tests/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,17 +633,9 @@ protected function runPolyfillTest(string $expected, string $methodName, callabl
$dom = new Document;
$dom->loadXML('<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><span><br/></span></p>');

$dom->firstOf('//span')->__call($methodName, $argumentsCallback($dom));
$dom->firstOf('//span')->$methodName(...$argumentsCallback($dom));
$this->assertXmlStringEqualsXmlString($expected, $dom->saveXML($dom->documentElement));

if (method_exists('DOMElement', $methodName))
{
$dom = new Document;
$dom->loadXML('<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><span><br/></span></p>');

$dom->firstOf('//span')->$methodName(...$argumentsCallback($dom));
$this->assertXmlStringEqualsXmlString($expected, $dom->saveXML($dom->documentElement), 'Reference does not match');
}
}

#[DataProvider('getInsertAdjacentElementTests')]
Expand Down Expand Up @@ -710,35 +702,47 @@ public static function getInsertAdjacentElementTests()

#[DataProvider('getInsertAdjacentTextTests')]
#[Group('polyfill')]
public function testInsertAdjacentText($position, $expected)
public function testInsertAdjacentText($position, $data, $expected)
{
$this->runPolyfillTest($expected, 'insertAdjacentText', fn() => [$position, $position]);
$this->runPolyfillTest($expected, 'insertAdjacentText', fn() => [$position, $data]);
}

public static function getInsertAdjacentTextTests()
{
return [
[
'afterbegin',
'afterbegin',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<span>afterbegin<br/></span>
</p>'
],
[
'afterbegin',
'after&<>\'"begin',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<span>after&amp;&lt;&gt;\'"begin<br/></span>
</p>'
],
[
'afterend',
'afterend',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><span><br/></span>afterend</p>'
],
[
'beforebegin',
'beforebegin',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform">beforebegin<span><br/></span></p>'
],
[
'beforeend',
'beforeend',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<span><br/>beforeend</span>
</p>'
],
[
'BeforeEnd',
'BeforeEnd',
'<p xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<span><br/>BeforeEnd</span>
Expand Down

0 comments on commit 3ab14a0

Please sign in to comment.