Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save claygriffiths/9f9e144f6375c9df099350d2c021878e to your computer and use it in GitHub Desktop.
Save claygriffiths/9f9e144f6375c9df099350d2c021878e to your computer and use it in GitHub Desktop.
GWiz: Validate HTML Fields in Form Editor
// 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