Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+33
-26
@@ -11,16 +11,23 @@
|
||||
namespace IPS\Text;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DOMDocument;
|
||||
use DOMNode;
|
||||
use DOMNodeList;
|
||||
use function defined;
|
||||
use function substr;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM Parser
|
||||
*/
|
||||
class _DOMParser
|
||||
class DOMParser
|
||||
{
|
||||
/**
|
||||
* @brief Callback to parse a DOMElement object
|
||||
@@ -35,12 +42,12 @@ class _DOMParser
|
||||
/**
|
||||
* Parse
|
||||
*
|
||||
* @param string $value Contents to parse
|
||||
* @param callback $elementParser Callback to parse a DOMElement object. Is expected to call `$parent->appendChild( $element )` after doing any parsing and call `$parser->_parseDomNodeList()`. `function ( \DOMElement $element, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @param callback|NULL $textParser Callback to parse a DOMText object. Is expected to call `$parent->appendChild( $element )` after doing any parsing. `function ( \DOMText $textNode, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @param string $value Contents to parse
|
||||
* @param callback $elementParser Callback to parse a DOMElement object. Is expected to call `$parent->appendChild( $element )` after doing any parsing and call `$parser->_parseDomNodeList()`. `function ( \DOMElement $element, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @param callback|null $textParser Callback to parse a DOMText object. Is expected to call `$parent->appendChild( $element )` after doing any parsing. `function ( \DOMText $textNode, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @return string
|
||||
*/
|
||||
public static function parse( $value, $elementParser, $textParser = NULL )
|
||||
public static function parse( string $value, callable $elementParser, callable $textParser = NULL ): string
|
||||
{
|
||||
$content = static::getDocumentBodyContents( ( new static( $elementParser, $textParser ) )->parseValueIntoDocument( $value ) );
|
||||
|
||||
@@ -54,11 +61,11 @@ class _DOMParser
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param callback $elementParser Callback to parse a DOMElement object. Is expected to call `$parent->appendChild( $element )` after doing any parsing and call `$parser->_parseDomNodeList()`. `function ( \DOMElement $element, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @param callback|NULL $textParser Callback to parse a DOMText object. Is expected to call `$parent->appendChild( $element )` after doing any parsing. `function ( \DOMText $textNode, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @return string
|
||||
* @param callback $elementParser Callback to parse a DOMElement object. Is expected to call `$parent->appendChild( $element )` after doing any parsing and call `$parser->_parseDomNodeList()`. `function ( \DOMElement $element, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @param callback|null $textParser Callback to parse a DOMText object. Is expected to call `$parent->appendChild( $element )` after doing any parsing. `function ( \DOMText $textNode, \DOMNode $parent, \IPS\Text\DOMParser $parser )`
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $elementParser, $textParser = NULL )
|
||||
public function __construct( callable $elementParser, callable $textParser = NULL )
|
||||
{
|
||||
$this->elementParser = $elementParser;
|
||||
$this->textParser = $textParser;
|
||||
@@ -67,10 +74,10 @@ class _DOMParser
|
||||
/**
|
||||
* Parse Value into DOMDocument
|
||||
*
|
||||
* @param string $value Contents to parse
|
||||
* @return \DOMDocument
|
||||
* @param string $value Contents to parse
|
||||
* @return \IPS\Xml\DOMDocument
|
||||
*/
|
||||
public function parseValueIntoDocument( $value )
|
||||
public function parseValueIntoDocument( string $value ): \IPS\Xml\DOMDocument
|
||||
{
|
||||
/* Load the value into a DOMDocument */
|
||||
$source = new \IPS\Xml\DOMDocument( '1.0', 'UTF-8' );
|
||||
@@ -89,11 +96,11 @@ class _DOMParser
|
||||
/**
|
||||
* Parse DOMNode
|
||||
*
|
||||
* @param \DOMNode $node The node from the source document to parse
|
||||
* @param \DOMNode $parent The node from the new document which will be this node's parent
|
||||
* @param DOMNode $node The node from the source document to parse
|
||||
* @param DOMNode $parent The node from the new document which will be this node's parent
|
||||
* @return void
|
||||
*/
|
||||
public function _parseDomNode( \DOMNode $node, \DOMNode &$parent )
|
||||
public function _parseDomNode( DOMNode $node, DOMNode &$parent ) : void
|
||||
{
|
||||
switch ( $node->nodeType )
|
||||
{
|
||||
@@ -150,11 +157,11 @@ class _DOMParser
|
||||
/**
|
||||
* Loop child nodes of a node and parse them
|
||||
*
|
||||
* @param \DOMNodeList $children The child nodes from the source document
|
||||
* @param \DOMNode $parent The node from the new document which will be the parent of all these nodes
|
||||
* @param DOMNodeList $children The child nodes from the source document
|
||||
* @param DOMNode $parent The node from the new document which will be the parent of all these nodes
|
||||
* @return void
|
||||
*/
|
||||
public function _parseDomNodeList( \DOMNodeList $children, \DOMNode $parent )
|
||||
public function _parseDomNodeList( DOMNodeList $children, DOMNode $parent ) : void
|
||||
{
|
||||
foreach ( $children as $child )
|
||||
{
|
||||
@@ -165,10 +172,10 @@ class _DOMParser
|
||||
/**
|
||||
* Get body contents from document
|
||||
*
|
||||
* @param \DOMDocument $document The document
|
||||
* @return string
|
||||
* @param DOMDocument $document The document
|
||||
* @return DOMNode|null
|
||||
*/
|
||||
public static function getDocumentBody( \DOMDocument $document )
|
||||
public static function getDocumentBody( DOMDocument $document ): ?DOMNode
|
||||
{
|
||||
return $document->getElementsByTagName('body')->item(0);
|
||||
}
|
||||
@@ -176,14 +183,14 @@ class _DOMParser
|
||||
/**
|
||||
* Get body contents from document
|
||||
*
|
||||
* @param \DOMDocument $document The document
|
||||
* @param DOMDocument $document The document
|
||||
* @return string
|
||||
*/
|
||||
public static function getDocumentBodyContents( \DOMDocument $document )
|
||||
public static function getDocumentBodyContents( DOMDocument $document ): string
|
||||
{
|
||||
if ( $body = static::getDocumentBody( $document ) )
|
||||
{
|
||||
return \substr( $document->saveHTML( $body ), 6, -7 );
|
||||
return substr( $document->saveHTML( $body ), 6, -7 );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user