0
votes

I have the following HTML code which I convert to a document object then a string again using serializeToString():

    let html = `<!DOCTYPE html>
    <html>
    	<head>
    		<title>Title</title>
    	</head>
    	<body>Hello
    	</body>
    </html>`;
    
    let newHTMLDocument = new DOMParser().parseFromString( html, 'text/html' );
    
    let head = new XMLSerializer().serializeToString(newHTMLDocument.head);
    
    console.log(head);

Why does head contain:

<head xmlns="http://www.w3.org/1999/xhtml">
        <title>Title</title>
    </head>

As you can see, the xmlns="http://www.w3.org/1999/xhtml" is not in the original string, so why is serializeToString() adding this to my head tag and how can I stop the function from doing that, so the head variable contains this instead:

<head>
        <title>Title</title>
    </head>
1
The DOM parser I'm assuming. It's just correcting the HTML on parse. I can almost guarantee you if you change the HTML type to HTML5 <!DOCTYPE HTML> it won't do that. - user4616966
@TyQ. It still does it! I tried it. - GTS Joe

1 Answers

1
votes

From the XHTML spec, section 3.1.1:

The root element of the document must contain an xmlns declaration for the XHTML namespace [XMLNS]. The namespace for XHTML is defined to be http://www.w3.org/1999/xhtml. An example root element might look like: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

So, it's just complying with standards.

To convert to text without the serializer maybe something like:

let html = `<!DOCTYPE html>
<html>
	<head>
		<title>Title</title>
	</head>
	<body>Hello
	</body>
</html>`;

let newHTMLDocument = new DOMParser().parseFromString( html, 'text/html' );

console.log(newHTMLDocument.head.outerHTML);