Created
September 27, 2023 20:22
-
-
Save claygriffiths/9f9e144f6375c9df099350d2c021878e to your computer and use it in GitHub Desktop.
GWiz: Validate HTML Fields in Form Editor
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
// Paste this into your browser DevTools console while editing a form in Gravity Forms | |
// Load fast-xml-parser | |
jQuery.getScript( | |
"https://cdnjs.cloudflare.com/ajax/libs/fast-xml-parser/4.0.8/fxparser.js", | |
function () { | |
// Loop through HTML fields in window.form.fields | |
window.form.fields.forEach(function (field) { | |
if (field.type !== "html") { | |
return; | |
} | |
const prefix = "<html><body>"; | |
const suffix = "</body></html>"; | |
const data = prefix + field.content + suffix; | |
const result = fxparser.XMLValidator.validate(data, { | |
ignoreAttributes: false, | |
unpairedTags: ["hr", "br", "link", "meta"], | |
stopNodes: ["*.pre", "*.script"], | |
processEntities: true, | |
htmlEntities: true, | |
}); | |
if (result !== true && result.err) { | |
jQuery("#field_" + field.id).css("background-color", "#ffd5d5"); | |
jQuery("#field_" + field.id) | |
.find(".gfield_label") | |
.after( | |
'<div class="html_error" style="color: #ff0000;">HTML error found: ' + | |
result.err.msg + | |
"</div>" | |
); | |
} else { | |
jQuery("#field_" + field.id).css("background-color", undefined); | |
jQuery("#field_" + field.id) | |
.find(".html_error") | |
.remove(); | |
} | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment