Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:37:35+00:00 2026-05-30T01:37:35+00:00

On Google Chrome (Canary), it seems no string can make the DOM parser fail.

  • 0

On Google Chrome (Canary), it seems no string can make the DOM parser fail. I’m trying to parse some HTML, but if the HTML isn’t completely, 100%, valid, I want it to display an error. I’ve tried the obvious:

var newElement = document.createElement('div');
newElement.innerHTML = someMarkup; // Might fail on IE, never on Chrome.

I’ve also tried the method in this question. Doesn’t fail for invalid markup, even the most invalid markup I can produce.

So, is there some way to parse HTML “strictly” in Google Chrome at least? I don’t want to resort to tokenizing it myself or using an external validation utility. If there’s no other alternative, a strict XML parser is fine, but certain elements don’t require closing tags in HTML, and preferably those shouldn’t fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:37:36+00:00Added an answer on May 30, 2026 at 1:37 am

    Use the DOMParser to check a document in two steps:

    1. Validate whether the document is XML-conforming, by parsing it as XML.
    2. Parse the string as HTML. This requires a modification on the DOMParser.

      Loop through each element, and check whether the DOM element is an instance of HTMLUnknownElement. For this purpose, getElementsByTagName('*') fits well.
      (If you want to strictly parse the document, you have to recursively loop through each element, and remember whether the element is allowed to be placed at that location. Eg. <area> in <map>)

    Demo: http://jsfiddle.net/q66Ep/1/

    /* DOM parser for text/html, see https://stackoverflow.com/a/9251106/938089 */
    ;(function(DOMParser) {"use strict";var DOMParser_proto=DOMParser.prototype,real_parseFromString=DOMParser_proto.parseFromString;try{if((new DOMParser).parseFromString("", "text/html"))return;}catch(e){}DOMParser_proto.parseFromString=function(markup,type){if(/^\s*text\/html\s*(;|$)/i.test(type)){var doc=document.implementation.createHTMLDocument(""),doc_elt=doc.documentElement,first_elt;doc_elt.innerHTML=markup;first_elt=doc_elt.firstElementChild;if (doc_elt.childElementCount===1&&first_elt.localName.toLowerCase()==="html")doc.replaceChild(first_elt,doc_elt);return doc;}else{return real_parseFromString.apply(this, arguments);}};}(DOMParser));
    
    /*
     * @description              Validate a HTML string
     * @param       String html  The HTML string to be validated 
     * @returns            null  If the string is not wellformed XML
     *                    false  If the string contains an unknown element
     *                     true  If the string satisfies both conditions
     */
    function validateHTML(html) {
        var parser = new DOMParser()
          , d = parser.parseFromString('<?xml version="1.0"?>'+html,'text/xml')
          , allnodes;
        if (d.querySelector('parsererror')) {
            console.log('Not welformed HTML (XML)!');
            return null;
        } else {
            /* To use text/html, see https://stackoverflow.com/a/9251106/938089 */
            d = parser.parseFromString(html, 'text/html');
            allnodes = d.getElementsByTagName('*');
            for (var i=allnodes.length-1; i>=0; i--) {
                if (allnodes[i] instanceof HTMLUnknownElement) return false;
            }
        }
        return true; /* The document is syntactically correct, all tags are closed */
    }
    
    console.log(validateHTML('<div>'));  //  null, because of the missing close tag
    console.log(validateHTML('<x></x>'));// false, because it's not a HTML element
    console.log(validateHTML('<a></a>'));//  true, because the tag is closed,
                                         //       and the element is a HTML element
    

    See revision 1 of this answer for an alternative to XML validation without the DOMParser.

    Considerations

    • The current method completely ignores the doctype, for validation.
    • This method returns null for <input type="text">, while it’s valid HTML5 (because the tag is not closed).
    • Conformance is not checked.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Google Chrome, I want to debug some JavaScript code. How can I
Google Chrome extensions are a nice invention, but they can potentially interact with a
Google Chrome is throwing Uncaught TypeError: Cannot set property 'isDown' of undefined but it
Google Chrome seems to be blocking a popup I am creating via jQuery. After
Google Chrome seems to have a bug when overflowing content inside of a fieldset.
In Google Chrome, the following code snippet: <html xmlns = http://www.w3.org/1999/xhtml> <head> <title>Example</title> <script
In Google Chrome, AJAX called within $(function(){....}); seems to keep the page loading. I
I have problem where google chrome is showing: The site uses SSL, but Google
Google Chrome's native client is soon to be released. http://blog.chromium.org/2011/02/native-client-getting-ready-for-takeoff.html Would this allow node.js
I can't get Google Chrome to submit my form. It's getting pretty ridiculous. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.