Skip to content

Commit 1a6a34e

Browse files
palmer-clclarencepalmer
andauthored
fix: preserve empty spaces in XML parsing and handle temporary wrappers in text node importer (#775)
Co-authored-by: clarencepalmer <cole@rollprogramcole.com>
1 parent 2ee08c4 commit 1a6a34e

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

packages/super-editor/src/core/super-converter/SuperConverter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ class SuperConverter {
163163
}
164164

165165
parseXmlToJson(xml) {
166-
return JSON.parse(xmljs.xml2json(xml, null, 2));
166+
// We need to preserve nodes with xml:space="preserve" and only have empty spaces
167+
const newXml = xml.replace(/(<w:t xml:space="preserve">)(\s+)(<\/w:t>)/g, '$1[[sdspace]]$2[[sdspace]]$3');
168+
return JSON.parse(xmljs.xml2json(newXml, null, 2));
167169
}
168170

169171
static getStoredSuperdocVersion(docx) {

packages/super-editor/src/core/super-converter/v2/importer/textNodeImporter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export const handleTextNode = (params) => {
1616

1717
// Text nodes have no children. Only text, and there should only be one child
1818
let text;
19-
if (elements.length === 1) text = elements[0].text;
19+
if (elements.length === 1) {
20+
text = elements[0].text;
21+
// Handle the removal of a temporary wrapper that we added to preserve empty spaces
22+
text = text.replace(/\[\[sdspace\]\]/g, '');
23+
}
2024
// Word sometimes will have an empty text node with a space attribute, in that case it should be a space
2125
else if (!elements.length && 'attributes' in node && node.attributes['xml:space'] === 'preserve') {
2226
text = ' ';

0 commit comments

Comments
 (0)