From e84a70012fb837f0ef46f1416b44ea63b43cb5b9 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Tue, 17 Jun 2025 10:15:31 +0000 Subject: [PATCH] Bug 40156: Advanced editor should not create empty fields and subfields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, advanced bibliographic records editor creates empty subfields (or even empty fields) when no content is placed after ‡ (or, in case of control fields, after ). Test plan: ========== 1. Enable EnableAdvancedCatalogingEditor (have StripWhitespaceChars set to "Don't strip" - the default setting). 2. Open a record in the advanced editor, create empty subfield and empty fields, e.g.: 007 500 _ _ ‡a 246 3 1 ‡aAdditional title :‡b 3. Save the record. Go to bibliographic record Normal view and in modal "MARC preview" notice that the fields / subfields you entered as empty exist in the record with no data. This is wrong. 4. Apply the patch ; restart_all. 5. Repeat p. 2 and 3. Note that empty fields/subfields have not been created. Sponsored-by: Ignatianum University in Cracow Signed-off-by: Wojciech Baran --- .../lib/koha/cateditor/text-marc.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/text-marc.js b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/text-marc.js index b7623de805..7f3950c11d 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/text-marc.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/text-marc.js @@ -58,9 +58,11 @@ define( [ 'marc-record' ], function( MARC ) { tagNumber = tagNumber[1]; if ( tagNumber < '010' ) { - var field = new MARC.Field( tagNumber, ' ', ' ', [ [ '@', line.substring( 4 ) ] ] ); - field.sourceLine = i; - record.addField( field ); + if (line.length > 4) { + var field = new MARC.Field(tagNumber, ' ', ' ', [['@', line.substring(4)]]); + field.sourceLine = i; + record.addField(field); + } } else { var indicators = line.match( /^... ([0-9A-Za-z_ ]) ([0-9A-Za-z_ ])/ ); if ( !indicators ) { @@ -86,12 +88,17 @@ define( [ 'marc-record' ], function( MARC ) { $.each( subfields, function( i, subfield ) { var next = subfields[ i + 1 ]; - - field.addSubfield( [ subfield.code, line.substring( subfield.ch + 2, next ? next.ch : line.length ) ] ); + var subfield_start = subfield.ch + 2; + var subfield_end = (next ? next.ch : line.length); + if (subfield_end > subfield_start) { + field.addSubfield( [ subfield.code, line.substring( subfield_start, subfield_end ) ] ); + } } ); field.sourceLine = i; - record.addField( field ); + if (field.subfields().length) { + record.addField( field ); + } } } ); -- 2.39.5