Last active
December 21, 2015 04:39
-
-
Save orlando/6251395 to your computer and use it in GitHub Desktop.
poor's man javascript html validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Poor's man html Validator. | |
// https://gist.github.com/orlando/6251395 | |
function validateHtml (htmlString) { | |
var htmlString, divElm, docFrag, docFragHtml; | |
htmlString = htmlString.replace(/ +/g, " ").toLowerCase(); | |
htmlString = htmlString.replace(/\s*\/\s*>/g, ">"); | |
divElm = document.createElement('div'); | |
docFrag = document.createDocumentFragment(); | |
docFrag.appendChild(divElm); | |
docFrag.querySelector('div').innerHTML = htmlString; | |
docFragHtml = docFrag.querySelector('div').innerHTML; | |
docFragHtml = docFragHtml.replace(/'/g, '"'); | |
htmlString = htmlString.replace(/'/g, '"'); | |
if (docFragHtml === htmlString) { | |
console.log("valid") | |
} else { | |
console.log("invalid") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment