Skip to content

MixedContent Support #112

@staabm

Description

@staabm

In some document one needs to support mixed-content like

<p>This is some text <extref href="..">and a inline tag</extref>and even more text</p>

in this case content is not only between tags, but tags are also mixed together. This form of content is not possible with the current deserializers.

for this case we build this small deserializer function:

    $mixedContent = function(Sabre\Xml\Reader $reader) {

        // If there's no children, we don't do anything.
        if ($reader->isEmptyElement) {
            $reader->next();
            return [];
        }

        $previousDepth = $reader->depth;

        $content = array();
        $reader->read();
        while (true) {
            if ($reader->nodeType == XMLReader::ELEMENT) {
                $content[] = $reader->parseCurrentElement();
            } else if ($reader->depth >= $previousDepth && in_array($reader->nodeType, [XMLReader::TEXT, XMLReader::CDATA, XMLReader::WHITESPACE])) {
                $content[] = $reader->value;
                $reader->read();
            } else if ($reader->nodeType == XMLReader::END_ELEMENT) {
                // Ensuring we are moving the cursor after the end element.
                $reader->read();
                break;
            } else {
                $reader->read();
            }
        }
        return $content;
    };

when time allows I will open a PR and provide some tests for it. If anyone beats me todo it, feel free to pick the code up and provide some tests.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions