Fix parsing & serializing <source> elements in EML model #2776
+41
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The editor uses
$.parseHTML()rather than$.parseXML()(or the native XML DOMParser) to parse EML. This is for historical reasons and to maintain compatibility with the rest of the codebase.In HTML,
<source>is a void element, meaning it can't contain text content and must not have a closing tag. Consequently the<source>...</source>elements in measurement scale get mis-parsed by the EML211 model. The closing tag is ignored and the contained text ends up outside the element node, leading malformed EML.This issue doesn't come up often in practice because
<source>elements can't be added via the UI, but it can happen when users create and upload EML via other tools, then try to edit it in the Editor.The EML model already had an existing
cleanUpXML()method that was meant to replace<source>elements with<sourced>and the rest of the code base expected this behaviour (e.g. looked for the text content of<sourced>elements to get thesourcetext for the model). However, the replacement wasn't assigned back toxmlString, so it had no effect. It was also not set to replace all occurrences, only the first. Additionally, we did not revert the changes when saving back to the server, so the saved EML would contain<sourced>elements instead of<source>.This PR fixes the
cleanUpXML()method to properly replace all occurrences of<source>and</source>with<sourced>and</sourced>, and adds arevertSourcedToSource()method to revert these changes before saving the EML back to the server. I also added a unit test to verify this behaviour, with multiple<source>elements.