Browse Source

Added Document::serializeInner

master
Dustin Wilson 2 years ago
parent
commit
f23b802b03
  1. 64
      README.md
  2. 9
      lib/Document.php

64
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.
</html>
```
#### 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('<!DOCTYPE html><html></html>');
echo $d->serializeInner();
```
or:
```php
namespace MensBeam\HTML\DOM;
$d = new Document('<!DOCTYPE html><html></html>');
echo $d;
```
Output:
```html
<!DOCTYPE html><html><head></head><body></body></html>
```
ook
- Serializing an element's contents:
```php
namespace MensBeam\HTML\DOM;
$d = new Document('<!DOCTYPE html><html><body><h1>Ook!</h1><p>Ook, eek? Ooooook. Ook.</body></html>');
$body = $d->body;
echo $body->serialize($body, [ 'reformatWhitespace' => true ]);
```
Output:
```html
<h1>Ook!</h1>
<p>Ook, eek? Ooooook. Ook.</p>
```
### 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.

9
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();

Loading…
Cancel
Save