From dd45ee8e85ceaadb1677e081e6d62ba114732b18 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 23 Oct 2023 02:01:03 +0000 Subject: [PATCH] Bug 35104: Alert when inserting text invalid in XML into bib record This change adds a Javascript based alert when a user tries to save data via the bib record editor that is invalid in XML. Test plan: 0. Apply patch 1. Go to http://localhost:8081/cgi-bin/koha/cataloguing/addbiblio.pl?frameworkcode= 2. Copy the data from "Text file containing control characters" 3. Paste the data into 245$a and 500$a 4. Click "Save" 5. Note the alerts at the top of the web page and that the record does not save 6. Resolve the problems 7. Click "Save" 8. Note that the record saves Signed-off-by: Martin Renvoize --- .../prog/en/modules/cataloguing/addbiblio.tt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt index 73fe2521c2f..57bddf5cf59 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt @@ -726,6 +726,45 @@ $(document).ready(function(){ } } + function containsInvalidXMLChars(input) { + let pattern = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]+/; + return pattern . test(input); + } + + function checkForXML(){ + let alert_text = ""; + let subfields = new Array(); + const regex = /([A-Za-z0-9]+)_subfield_([A-Za-z0-9]+)_/; + $("input[type='text'].input_marceditor, textarea.input_marceditor").each(function(){ + if ( containsInvalidXMLChars(this.value) ){ + let html_id = this.id; + let subfield = { "id": html_id }; + //FIXME: Turns out tabindex isn't the real tab index... + subfield['tab'] = parseInt(this.getAttribute('tabindex')) + 1; + let match = html_id.match(regex); + if (match){ + subfield['tag'] = match[1]; + subfield['subfield_code'] = match[2]; + } + subfields.push(subfield); + } + }); + if (subfields.length > 0){ + alert_text += "
"; + alert_text += "

" + _("The following subfields contain characters invalid in XML:") + "

"; + for (let i = 0; i < subfields.length; i++){ + let subfield = subfields[i]; + let tag = subfield.tag; + let subfield_code = subfield.subfield_code; + let tab = subfield.tab; + let id = subfield.id; + alert_text += "
  • "+_("Tag %s subfield %s in tab %s").format(tag, subfield_code, tab) + ' ' + _("Go to field") + '
  • '; + } + alert_text += "
    "; + } + return alert_text; + } + /** * Run checks for mandatory and important fields * Output errors if necessary, or submit the form @@ -733,6 +772,17 @@ $(document).ready(function(){ function Check(){ var StrAlert = AreFieldsNotOk(); var StrWarning = AreFieldsNotOk( false ); + + [% IF ! advancedMARCEditor %] + let AlertXML = checkForXML(); + if (AlertXML) { + if ( StrAlert === false ) { + StrAlert = ''; + } + StrAlert += AlertXML; + } + [% END %] + if( !StrAlert && StrWarning ){ // Check important fields $("#check_errors").html( StrWarning ); -- 2.41.0