Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/js/models/metadata/eml211/EML211.js
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ define([
emlString += this.formatXML(rootEMLNode);
});

emlString = this.revertSourcedToSource(emlString);

return emlString;
},

Expand Down Expand Up @@ -2187,14 +2189,29 @@ define([

/**
* Replace elements named "source" with "sourced" due to limitations with
* using $.parseHTML() rather than $.parseXML()
* using $.parseHTML() rather than $.parseXML(). In HTML <source> is
* treated as a void element a closing tag so any <source>…</source> will
* have the closing tag removed upon parsing. To avoid this problem, we
* replace <source> with <sourced> before parsing, and then revert it back
* to <source> before saving to the server.
* @param {string} xmlString The XML string to make the replacement in
* @returns {string} The cleaned up XML string
*/
cleanUpXML(xmlString) {
xmlString.replace("<source>", "<sourced>");
xmlString.replace("</source>", "</sourced>");
xmlString = xmlString.replace(/<source>/g, "<sourced>");
xmlString = xmlString.replace(/<\/source>/g, "</sourced>");
return xmlString;
},

/**
* Revert elements named "sourced" back to "source" before saving to the
* server. See cleanUpXML() for more info.
* @param {string} xmlString The XML string in which to make the replacement
* @returns {string} The cleaned up XML string
*/
revertSourcedToSource(xmlString) {
xmlString = xmlString.replace(/<sourced>/g, "<source>");
xmlString = xmlString.replace(/<\/sourced>/g, "</source>");
return xmlString;
},

Expand Down
21 changes: 21 additions & 0 deletions test/js/specs/unit/models/metadata/eml211/EML211.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,26 @@ define([
.should.be.instanceof(EMLTaxonCoverage);
});
});

describe("XML clean up helpers", function () {
const sourceXML =
"<dataset><source>One</source><source>Two</source></dataset>";
const sourcedXML =
"<dataset><sourced>One</sourced><sourced>Two</sourced></dataset>";

it("cleanUpXML converts source elements to sourced", function () {
state.eml.cleanUpXML(sourceXML).should.equal(sourcedXML);
});

it("revertSourcedToSource converts sourced elements back to source", function () {
state.eml.revertSourcedToSource(sourcedXML).should.equal(sourceXML);
});

it("round trips source elements through cleanUpXML and revertSourcedToSource", function () {
state.eml
.revertSourcedToSource(state.eml.cleanUpXML(sourceXML))
.should.equal(sourceXML);
});
});
});
});