|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74241 DomDocument::loadHTML ignores LIBXML_NOCDATA
Submitted: 2017-03-13 10:42 UTC Modified: 2017-03-13 10:57 UTC
From: tom at r dot je Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 7.1.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: tom at r dot je
New email:
PHP Version: OS:


 [2017-03-13 10:42 UTC] tom at r dot je
Using PHP 7.1 with Libxml version 2.9.4 DomDocument::loadHTML ignores LIBXML_NOCDATA.

Test script:
$doc = new \DomDocument();

$doc->loadHTML('<!doctype html>

$().ready(function() {});

echo $doc->saveXML(null, LIBXML_NOCDATA);




<?xml version="1.0" standalone="yes"?>
<!DOCTYPE html>

$().ready(function() {});


Changing `loadHTML` to `loadXML` works as expected, no <![CDATA[ appears in the output.

saveHTML also omits the CDATA tags but as I'm working with document fragments, this isn't helpful as it wraps everything with html and body tags. 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-13 10:57 UTC]
-Status: Open +Status: Not a bug
 [2017-03-13 10:57 UTC]
LIBXML_NOCDATA, which matters at load time and not save time, is not respected because CDATA tags make no sense to have in HTML.

Loading as HTML but saving as XML, or vice versa, is incorrect: either the content is HTML or it is XML. If you want to deal with HTML fragments without the DTD and implied <html> elements then use the LIBXML_HTML_NODEFDTD and LIBXML_HTML_NOIMPLIED options with loadHTML().
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Sep 29 00:01:23 2020 UTC