-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocument.php
More file actions
103 lines (86 loc) · 2.99 KB
/
Copy pathDocument.php
File metadata and controls
103 lines (86 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* This file is part of the PHPMarkup package.
*
* (c) 2017-2021 Ouxsoft <contact@ouxsoft.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ouxsoft\PHPMarkup;
use DomDocument;
use Ouxsoft\PHPMarkup\Contract\DocumentInterface;
/**
* Class Document
* Hyperlink DomDocument that is loaded from a well formatted LHTML document and returns a HTML5
*
* @package Ouxsoft\PHPMarkup
*/
class Document extends DomDocument implements DocumentInterface
{
public const DEFAULT_LANG = "en";
/**
* Document constructor.
*/
public function __construct()
{
parent::__construct();
// suppress xml parse errors unless debugging
libxml_use_internal_errors(true);
// DomDocument format output option
$this->formatOutput = true;
// DomDocument object setting to preserve white space
$this->preserveWhiteSpace = true;
// DomDocument strict error checking setting
$this->strictErrorChecking = false;
// validate DOM on Parse
$this->validateOnParse = false;
// DomDocument encoding
$this->encoding = 'UTF-8';
}
/**
* Loads source, which is in LHTML format, as DomDocument
*
* A custom load page wrapper is required for server-side HTML5 entity support.
* Using $this->loadHTMLFile will removes HTML5 entities, such as ©
* due to the libxml not supporting HTML5
*
* @param string $source must be well formatted and feature a root element, e.g. <html>
* @return bool
*/
public function loadSource(string $source): bool
{
// add DOCTYPE declaration
$doctype = '<!DOCTYPE html [' . Entities::HTML5 . ']>' . PHP_EOL;
// replace DOCTYPE if present
$count = 1;
str_ireplace('<!doctype html>', $doctype, $source, $count);
if ($count == 0) {
// add doctype if not present
$source = $doctype . $source;
// load as XML
$this->loadXML($source);
} else {
// load as HTML
$this->loadHTML($source);
}
// adds HTML root element if one is not present
if (!is_object($this->documentElement)) {
return false;
// TODO throw new Exception('Invalid LHTML document provided');
}
// add html root element if missing
$root_tag = $this->documentElement->tagName;
if (strcasecmp($root_tag, 'html') !== 0) {
// create a new DomDocument, add source to it, and append to root document with html root
$source_dom = new DOMDocument();
$source_dom->loadXML($source);
$this->loadSource('<html lang="' . self::DEFAULT_LANG . '"></html>');
$this->documentElement->appendChild(
$this->importNode($source_dom->documentElement, true)
);
}
return true;
}
}