diff --git a/README.md b/README.md index 73b7ca3..658e0c5 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,11 @@ partial class Document extends Node implements \ArrayAccess { ?Node $node = null, array $config = [] ): string; + + public function serializeInner( + ?Node $node = null, + array $config = [] + ): string; } ``` @@ -226,6 +231,65 @@ Converts a node to a string. ``` +#### MensBeam\HTML\DOM\Document::serializeInner #### + +Converts a node to a string but only serializes the node's contents. + +* `node`: A node within the document to serialize, defaults to the document itself. +* `config`: A configuration array with the possible keys and value types being: + - `booleanAttributeValues` (`bool|null`): Whether to include the values of boolean attributes on HTML elements during serialization. Per the standard this is `true` by default + - `foreignVoidEndTags` (`bool|null`): Whether to print the end tags of foreign void elements rather than self-closing their start tags. Per the standard this is `true` by default + - `groupElements` (`bool|null`): Group like "block" elements and insert extra newlines between groups + - `indentStep` (`int|null`): The number of spaces or tabs (depending on setting of indentStep) to indent at each step. This is `1` by default and has no effect unless `reformatWhitespace` is `true` + - `indentWithSpaces` (`bool|null`): Whether to use spaces or tabs to indent. This is `true` by default and has no effect unless `reformatWhitespace` is `true` + - `reformatWhitespace` (`bool|null`): Whether to reformat whitespace (pretty-print) or not. This is `false` by default + +##### Examples ##### + +- Serializing a document (functionally identical to `Document::serialize`): + + ```php + namespace MensBeam\HTML\DOM; + + $d = new Document(''); + echo $d->serializeInner(); + ``` + + or: + + ```php + namespace MensBeam\HTML\DOM; + + $d = new Document(''); + echo $d; + ``` + + Output: + + ```html + + ``` + +ook + +- Serializing an element's contents: + + ```php + namespace MensBeam\HTML\DOM; + + $d = new Document('

Ook!

Ook, eek? Ooooook. Ook.'); + $body = $d->body; + echo $body->serialize($body, [ 'reformatWhitespace' => true ]); + ``` + + Output: + + ```html +

Ook!

+ +

Ook, eek? Ooooook. Ook.

+ ``` + ### MensBeam\HTML\DOM\Node ### Common namespace constants are provided in `MensBeam\HTML\DOM\Node` to make using namespaces with this library not so onerous. In addition, constants are provided here to be used with `MensBeam\HTML\DOM\ParentNode::walk`. `MensBeam\HTML\DOM\Node` also implements `\Stringable` which means that any node can be simply converted to a string to serialize it. diff --git a/lib/Document.php b/lib/Document.php index ba4d458..3126208 100644 --- a/lib/Document.php +++ b/lib/Document.php @@ -816,6 +816,15 @@ class Document extends Node implements \ArrayAccess { return Serializer::serialize($node->innerNode, $config); } + public function serializeInner(?Node $node = null, array $config = []): string { + $node = $node ?? $this; + if ($node !== $this && $node->ownerDocument !== $this) { + throw new DOMException(DOMException::WRONG_DOCUMENT); + } + + return Serializer::serializeInner($node->innerNode, $config); + } + public function __toString(): string { return $this->serialize();