From 61591f6a2ed5bc1659dfe913daf787b859e103df Mon Sep 17 00:00:00 2001 From: JFH Date: Sun, 28 Jun 2020 21:52:27 +0200 Subject: [PATCH] fix issue with undo because this.elem was overwritten with node.remove() --- cypress/integration/ui/issues/issue-359.js | 26 ++++++++++++++++++++++ editor/extensions/ext-connector.js | 7 +++--- editor/history.js | 4 ++-- editor/svgcanvas.js | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 cypress/integration/ui/issues/issue-359.js diff --git a/cypress/integration/ui/issues/issue-359.js b/cypress/integration/ui/issues/issue-359.js new file mode 100644 index 00000000..a07749ca --- /dev/null +++ b/cypress/integration/ui/issues/issue-359.js @@ -0,0 +1,26 @@ +import { + visitAndApproveStorage +} from '../../../support/ui-test-helper.js'; + +// See https://github.com/SVG-Edit/svgedit/issues/359 +describe('Fix issue 359', function () { + beforeEach(() => { + visitAndApproveStorage(); + }); + + it('can undo without throwing', function () { + cy.get('#tool_source').click(); + cy.get('#svg_source_textarea') + .type('{selectall}') + .type(` + + Layer 1 + + + `, {parseSpecialCharSequences: false}); + cy.get('#tool_source_save').click(); + cy.get('#tool_undo').click(); + cy.get('#tool_redo').click(); // test also redo to make the test more comprehensive + // if the undo throws an error to the console, the test will fail + }); +}); diff --git a/editor/extensions/ext-connector.js b/editor/extensions/ext-connector.js index 48284e6f..7cd1f70a 100644 --- a/editor/extensions/ext-connector.js +++ b/editor/extensions/ext-connector.js @@ -573,18 +573,19 @@ export default { }, elementChanged (opts) { let elem = opts.elems[0]; - if (elem && elem.tagName === 'svg' && elem.id === 'svgcontent') { + if (!elem) return; + if (elem.tagName === 'svg' && elem.id === 'svgcontent') { // Update svgcontent (can change on import) svgcontent = elem; init(); } // Has marker, so change offset - if (elem && ( + if ( elem.getAttribute('marker-start') || elem.getAttribute('marker-mid') || elem.getAttribute('marker-end') - )) { + ) { const start = elem.getAttribute('marker-start'); const mid = elem.getAttribute('marker-mid'); const end = elem.getAttribute('marker-end'); diff --git a/editor/history.js b/editor/history.js index 40f0064a..00acda69 100644 --- a/editor/history.js +++ b/editor/history.js @@ -223,7 +223,7 @@ export class InsertElementCommand extends Command { } this.parent = this.elem.parentNode; - this.elem = this.elem.remove(); + this.elem.remove(); if (handler) { handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this); @@ -280,7 +280,7 @@ export class RemoveElementCommand extends Command { removeElementFromListMap(this.elem); this.parent = this.elem.parentNode; - this.elem = this.elem.remove(); + this.elem.remove(); if (handler) { handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this); diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 4c9d7804..8ac7b4af 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -516,7 +516,7 @@ const undoMgr = canvas.undoMgr = new UndoManager({ } else if (!isApply) { restoreRefElems(cmd.elem); } - if (cmd.elem.tagName === 'use') { + if (cmd.elem && cmd.elem.tagName === 'use') { setUseData(cmd.elem); } } else if (cmdType === ChangeElementCommand.type()) {