Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if HTML snippet is valid with JavaScript

I need a reliable JavaScript library / function to check if an HTML snippet is valid that I can call from my code. For example, it should check that opened tags and quotation marks are closed, nesting is correct, etc.

I don't want the validation to fail because something is not 100% standard (but would work anyway).

like image 774
User Avatar asked Apr 05 '12 10:04

User


People also ask

How do you check is HTML valid or not?

Take checkHTML("<p>Test<P>test") for instance. That is perfectly valid HTML, but the browser will normalize it when it pulls it back out of innerHTML . There will be no text outside an element in the DOM generated from that valid HTML.

How do you check if a string is HTML or not in JavaScript?

test with "<p>fizz buzz</p>" to check if "<p>fizz buzz</p>" is a JavaScript string. We use <\/?[a-z][\s\S]*> to check for any tags in the string to check if it has any HTML markup.

Which node js is the valid HTML tag?

The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<). It should be followed by a double quotes string or single quotes string. It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.


1 Answers

Update: this answer is limited - please see the edit below.

Expanding on @kolink's answer, I use:

var checkHTML = function(html) {   var doc = document.createElement('div');   doc.innerHTML = html;   return ( doc.innerHTML === html ); } 

I.e., we create a temporary div with the HTML. In order to do this, the browser will create a DOM tree based on the HTML string, which may involve closing tags etc.

Comparing the div's HTML contents with the original HTML will tell us if the browser needed to change anything.

checkHTML('<a>hell<b>o</b>') 

Returns false.

checkHTML('<a>hell<b>o</b></a>') 

Returns true.

Edit: As @Quentin notes below, this is excessively strict for a variety of reasons: browsers will often fix omitted closing tags, even if closing tags are optional for that tag. Eg:

<p>one para <p>second para 

...is considered valid (since Ps are allowed to omit closing tags) but checkHTML will return false. Browsers will also normalise tag cases, and alter white space. You should be aware of these limits when deciding to use this approach.

like image 79
mikemaccana Avatar answered Oct 16 '22 16:10

mikemaccana