?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend/Controllers/



?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend

?? Viewing: DomHtmlDocument.php

<?php

namespace KS_PAC_DCFH\Frontend\Controllers;

use DOMDocument;
use DOMXPath;

if (!defined('ABSPATH')) {
    exit;
}

class DomHtmlDocument
{
    public DOMDocument $dom;

    public DOMXPath $xpath;

    public string $html;

    public function __construct(string $html)
    {
        $this->dom = new DOMDocument('1.0', 'UTF-8');
        libxml_use_internal_errors(true);
        $html = mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, ~0], 'UTF-8');
        $this->dom->loadHTML($html, LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        libxml_clear_errors();
        $this->xpath = new DOMXPath($this->dom);
        $this->html = $html;
    }

    public function find_node(string $xpath_query, $context_node = null)
    {
        $node = $this->xpath->query($xpath_query, $context_node);

        return ($node && $node->length > 0) ? $node->item(0) : null;
    }

    public function find_nodes(string $xpath_query, $context_node = null)
    {
        return $this->xpath->query($xpath_query, $context_node);
    }

    public function get_attribute($node, string $attribute_name, $default_value = null)
    {
        return $node->hasAttribute($attribute_name)
            ? $node->getAttribute($attribute_name)
            : $default_value;
    }

    public function get_all_attributes($node): array
    {
        $attributes = [];
        foreach ($node->attributes as $attr) {
            $attributes[$attr->name] = $attr->value;
        }

        return $attributes;
    }

    public function set_attributes($node, array $attributes): void
    {
        foreach ($attributes as $name => $value) {
            $node->setAttribute($name, (string)$value);
        }
    }

    public function add_element(string $tag_name, array $attributes = [], ?string $text = null)
    {
        $element = $this->dom->createElement($tag_name);
        $this->set_attributes($element, $attributes);
        if ($text !== null) {
            $contentNode = str_contains($text, '<') ? $this->dom->createCDATASection($text) : $this->dom->createTextNode($text);
            $element->appendChild($contentNode);
        }

        return $element;
    }

    public function create_document_fragment(string $html)
    {
        $fragment = $this->dom->createDocumentFragment();
        $fragment->appendXML($html);

        return $fragment;
    }

    public function append_html_to_node($parent_node, string $html): void
    {
        $fragment = $this->create_document_fragment($html);
        $parent_node->appendChild($fragment);
    }

    public function create_cdata_section(string $text)
    {
        return $this->dom->createCDATASection($text);
    }

    public function append_child($parent_node, $child_node): void
    {
        $parent_node->appendChild($child_node);
    }

    public function insert_after($new_node, $existing_node): void
    {
        if ($existing_node->nextSibling) {
            $existing_node->parentNode->insertBefore($new_node, $existing_node->nextSibling);
        } else {
            $existing_node->parentNode->appendChild($new_node);
        }
    }

    public function get_dom(): DOMDocument
    {
        return $this->dom;
    }

    public function get_html(): string
    {
        return $this->html;
    }

    public function save_html(): string
    {
        return $this->dom->saveHTML();
    }
}


??

??