-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from veewee/cdata
Cdata functions
- Loading branch information
Showing
13 changed files
with
267 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Assert; | ||
|
||
use DOMCdataSection; | ||
use Psl\Type\Exception\AssertException; | ||
use function Psl\Type\instance_of; | ||
|
||
/** | ||
* @psalm-assert DOMCdataSection $node | ||
* @throws AssertException | ||
*/ | ||
function assert_cdata(mixed $node): DOMCdataSection | ||
{ | ||
return instance_of(DOMCdataSection::class)->assert($node); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Builder; | ||
|
||
use Closure; | ||
use DOMCdataSection; | ||
use DOMDocument; | ||
use DOMNode; | ||
use Webmozart\Assert\Assert; | ||
use function VeeWee\Xml\Dom\Assert\assert_cdata; | ||
use function VeeWee\Xml\Dom\Predicate\is_document; | ||
use function VeeWee\Xml\Internal\configure; | ||
|
||
/** | ||
* @param list<callable(DOMCdataSection): DOMCdataSection> $configurators | ||
* | ||
* @return \Closure(DOMNode): DOMCdataSection | ||
*/ | ||
function cdata(string $data, ...$configurators): Closure | ||
{ | ||
return static function (DOMNode $node) use ($data, $configurators): DOMCdataSection { | ||
$document = is_document($node) ? $node : $node->ownerDocument; | ||
Assert::isInstanceOf($document, DOMDocument::class, 'Can not create cdata without a DOM document.'); | ||
|
||
return assert_cdata( | ||
configure(...$configurators)($document->createCDATASection($data)) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Predicate; | ||
|
||
use DOMCdataSection; | ||
use DOMNode; | ||
|
||
/** | ||
* @psalm-assert-if-true DOMCdataSection $node | ||
*/ | ||
function is_cdata(DOMNode $node): bool | ||
{ | ||
return $node instanceof DOMCdataSection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Assert; | ||
|
||
use DOMNode; | ||
use PHPUnit\Framework\TestCase; | ||
use Psl\Type\Exception\AssertException; | ||
use VeeWee\Xml\Dom\Document; | ||
use function VeeWee\Xml\Dom\Assert\assert_cdata; | ||
|
||
final class AssertCDataTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider provideTestCases | ||
*/ | ||
public function test_it_knows_cdata(?DOMNode $node, bool $expected): void | ||
{ | ||
if (!$expected) { | ||
$this->expectException(AssertException::class); | ||
} | ||
|
||
$actual = assert_cdata($node); | ||
static::assertSame($node, $actual); | ||
} | ||
|
||
public function provideTestCases() | ||
{ | ||
$doc = Document::fromXmlString( | ||
<<<EOXML | ||
<doc><![CDATA[<html>HELLO</html]]></doc> | ||
EOXML | ||
)->toUnsafeDocument(); | ||
|
||
yield [$doc, false]; | ||
yield [$doc->documentElement, false]; | ||
yield [$doc->documentElement->firstChild, true]; | ||
yield [null, false]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Builder; | ||
|
||
use DOMCdataSection; | ||
use DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use function Psl\Fun\identity; | ||
use function VeeWee\Xml\Dom\Builder\cdata; | ||
use function VeeWee\Xml\Dom\Mapper\xml_string; | ||
|
||
final class CdataTest extends TestCase | ||
{ | ||
public function test_it_can_build_cdata(): void | ||
{ | ||
$doc = new DOMDocument(); | ||
$node = cdata($data = '<html>hello</html>')($doc); | ||
|
||
static::assertInstanceOf(DOMCdataSection::class, $node); | ||
static::assertSame($data, $node->textContent); | ||
static::assertSame(xml_string()($node), '<![CDATA['.$data.']]>'); | ||
} | ||
|
||
public function test_it_can_build_cdata_with_configurators(): void | ||
{ | ||
$doc = new DOMDocument(); | ||
$node = cdata($data = '<html>hello</html>', identity())($doc); | ||
|
||
static::assertInstanceOf(DOMCdataSection::class, $node); | ||
static::assertSame($data, $node->textContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Predicate; | ||
|
||
use DOMNode; | ||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Xml\Dom\Document; | ||
use function VeeWee\Xml\Dom\Predicate\is_cdata; | ||
|
||
final class IsCDataTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider provideTestCases | ||
*/ | ||
public function test_it_knows_cdata(?DOMNode $node, bool $expected): void | ||
{ | ||
$actual = is_cdata($node); | ||
static::assertSame($expected, $actual); | ||
} | ||
|
||
public function provideTestCases() | ||
{ | ||
$doc = Document::fromXmlString( | ||
<<<EOXML | ||
<doc><![CDATA[<html>HELLO</html]]></doc> | ||
EOXML | ||
)->toUnsafeDocument(); | ||
|
||
yield [$doc, false]; | ||
yield [$doc->documentElement, false]; | ||
yield [$doc->documentElement->firstChild, true]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters