I build a script that combines all css on a page together to use it in my cms. It worked fine for a long time now i i get this error:
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag header invalid in Entity, line: 10 in css.php on line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag nav invalid in Entity, line: 10 in css.php on line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag section invalid in Entity, line: 22 in css.php on line 26
This is the php script
This is my code:
<?php
header('Content-type: text/css');
include ('../global.php');
if ($usetpl == '1') {
$client = New client();
$tplname = $client->template();
$location = "../templates/$tplname/header.php";
$page = file_get_contents($location);
} else {
$page = file_get_contents('../index.php');
}
class StyleSheets extends DOMDocument implements IteratorAggregate
{
public function __construct ($source)
{
parent::__construct();
$this->loadHTML($source);
}
public function getIterator ()
{
static $array;
if (NULL === $array) {
$xp = new DOMXPath($this);
$expression = '//head/link[@rel="stylesheet"]/@href';
$array = array();
foreach ($xp->query($expression) as $node)
$array[] = $node->nodeValue;
}
return new ArrayIterator($array);
}
}
foreach (new StyleSheets($page) as $index => $file) {
$css = file_get_contents($file);
echo $css;
}
Header, Nav and Section are elements from HTML5. Because HTML5 developers felt it is too difficult to remember Public and System Identifiers, the DocType declaration is just:
<!DOCTYPE html>
In other words, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD and that doesnt contain those elements, hence the Warnings.
To surpress the Warnings, put
libxml_use_internal_errors(true);
before the call toloadHTML
and
libxml_use_internal_errors(false);
after it.
An alternative would be to use https://github.com/html5lib/html5lib-php.
With a DOMDocument object, you should be able to place an @ before the load method in order to SUPPRESS all WARNINGS.
$dom = new DOMDocument;
@$dom->loadHTML($source);
And carry on.
HTML5 elements are still not supported, but you can silence libxml errors completely with the$options
parameter.
Just set
$doc = new DOMDocument();
$doc->loadHTMLFile("html5.html", LIBXML_NOERROR);
This option is preferred over@
which silences PHP errors.
But be careful, libxml is very forgiving and it will parse a broken HTML document. If you silence libxml errors you might not even be aware that the HTML is malformed.
Most people do not realize the difference between HTML and XML as languages and HTML and XML in regards to parsers. A parser takes code and the HTML and XML parsers are completely different. While there are some minor things XML parsers will tolerate in browsers (e.g. duplicateid
values) they don't mess around with junk that looks like code.
PHP's XML parser is even stricter and doesn't allow duplicateid
values. Additionally since anything can be an element (e.g.footer
,header
,section
) PHP's XML parser will not complain about unknown HTML5+ elements.
$dom->loadXML($xml);
For anyone developing on client side I highly recommend using the XML parser to handle your HTML5 code and since I started developing in the 2000s in to 2020 Gecko browsers (e.g. Waterfox, Firefox) have the best XML parser as the entire page will break and you'll get an explicit error message. Stricter code yields better results if you can comprehend quality eventually yields quantity though the opposite is not true.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.