Skip to content

Commit

Permalink
Fix checkWhitespace() error when no previous node (#935)
Browse files Browse the repository at this point in the history
Fixes error with checkWhitespace() when an emoticon is matched to be
removed but there is no previous node.

Fixes #933
  • Loading branch information
samclarke authored Aug 25, 2023
1 parent 0ee1033 commit 0540c93
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lib/emoticons.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function checkWhitespace(node, rangeHelper) {
var range = rangeHelper.cloneSelected();
var rangeStart = -1;
var rangeStartContainer = range.startContainer;
var previousText = prev.nodeValue || '';
var previousText = (prev && prev.nodeValue) || '';

previousText += dom.data(emoticon, 'sceditor-emoticon');

Expand Down Expand Up @@ -62,8 +62,10 @@ export function checkWhitespace(node, rangeHelper) {
}

next.insertData(0, previousText);
dom.remove(prev);
dom.remove(emoticon);
if (prev) {
dom.remove(prev);
}

// Need to update the range starting position if it's been modified
if (rangeStart > -1) {
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/lib/emoticons.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,34 @@ QUnit.test('checkWhitespace() - Remove cursor placed after', function (assert) {
assert.ok(mockRangeHelper.selectRangeCalled);
});

QUnit.test('checkWhitespace() - Remove with no previous node', function (assert) {
var root = utils.htmlToDiv('<img data-sceditor-emoticon=":)" />test');

var mockRange = {
startContainer: root,
startOffset: 1,
setStart: function (container, offset) {
this.startContainer = container;
this.startOffset = offset;
},
collapse: function () {}
};

var mockRangeHelper = {
selectedRange: mockRange,
cloneSelected: function () {
return mockRange;
},
selectRange: function (range) {
this.selectedRange = range;
}
};

emoticons.checkWhitespace(root, mockRangeHelper);

assert.nodesEqual(root, utils.htmlToDiv(':)test'));
});

QUnit.test('checkWhitespace() - no emoticons', function (assert) {
assert.expect(0);

Expand Down

0 comments on commit 0540c93

Please sign in to comment.