Skip to content

fix(dia.HighlighterView): prevent highlighting nodes outside cell view #2900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/joint-core/src/dia/HighlighterView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const HighlighterView = mvc.View.extend({
}
} else if (nodeSelector) {
el = V.toNode(nodeSelector);
if (!(el instanceof SVGElement)) el = null;
if (!(el instanceof SVGElement) || !cellView.el.contains(el)) el = null;
}
return el ? el : null;
},
Expand Down
86 changes: 78 additions & 8 deletions packages/joint-core/test/jointjs/dia/HighlighterView.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ QUnit.module('HighlighterView', function(hooks) {

var highlightSpy = sinon.spy(joint.dia.HighlighterView.prototype, 'highlight');
var unhighlightSpy = sinon.spy(joint.dia.HighlighterView.prototype, 'unhighlight');
var invalidSpy = sinon.spy();

paper.on('cell:highlight:invalid', invalidSpy);

var id = 'highlighter-id';
var node = elementView.el.querySelector('[joint-selector="body"]');
Expand All @@ -412,31 +415,71 @@ QUnit.module('HighlighterView', function(hooks) {
assert.ok(highlightSpy.calledOnceWithExactly(elementView, node));
assert.ok(highlightSpy.calledOn(highlighter));
assert.ok(unhighlightSpy.notCalled);
assert.ok(invalidSpy.notCalled);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

// Update (Default will unhighlight and highlight)
// Re-render (will not highlight the node, because
// it's not in the DOM anymore)
element.attr(['body', 'fill'], 'red', { dirty: true });
var node2 = elementView.el.querySelector('[joint-selector="body"]');
assert.ok(!node.isConnected);
assert.notEqual(node, node2);
assert.ok(highlightSpy.calledOnce);
assert.ok(highlightSpy.calledOnceWithExactly(elementView, node));
assert.ok(highlightSpy.calledOn(highlighter));
assert.notOk(highlightSpy.called);
assert.ok(unhighlightSpy.calledOnce);
assert.ok(unhighlightSpy.calledOnceWithExactly(elementView, node));
assert.ok(unhighlightSpy.calledOn(highlighter));
assert.ok(invalidSpy.calledOnce);
assert.ok(invalidSpy.calledOnceWithExactly(elementView, id, highlighter));
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

// Unhighlight
joint.dia.HighlighterView.remove(elementView, id);
assert.equal(joint.dia.HighlighterView.get(elementView, id), null);
assert.notOk(unhighlightSpy.called);
assert.ok(highlightSpy.notCalled);
assert.ok(invalidSpy.notCalled);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

// Highlight
var id2 = 'highlighter-id-2';
var node3 = elementView.el.querySelector('[joint-selector="label"]');
var highlighter2 = joint.dia.HighlighterView.add(elementView, node3, id2);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

// Update (Default will unhighlight and highlight)
element.attr(['body', 'fill'], 'blue', { dirty: false });
var node4 = elementView.el.querySelector('[joint-selector="label"]');
assert.equal(node3, node4);
assert.ok(highlightSpy.calledOnce);
assert.ok(highlightSpy.calledOnceWithExactly(elementView, node3));
assert.ok(highlightSpy.calledOn(highlighter2));
assert.ok(unhighlightSpy.calledOnce);
assert.ok(unhighlightSpy.calledOnceWithExactly(elementView, node));
assert.ok(unhighlightSpy.calledOn(highlighter));
assert.ok(unhighlightSpy.calledOnceWithExactly(elementView, node3));
assert.ok(unhighlightSpy.calledOn(highlighter2));
assert.ok(invalidSpy.notCalled);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

// Unhighlight
joint.dia.HighlighterView.remove(elementView, id2);
assert.equal(joint.dia.HighlighterView.get(elementView, id2), null);
assert.ok(unhighlightSpy.calledOnce);
assert.ok(unhighlightSpy.calledOnceWithExactly(elementView, node3));
assert.ok(unhighlightSpy.calledOn(highlighter2));
assert.ok(highlightSpy.notCalled);
assert.ok(invalidSpy.notCalled);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();
invalidSpy.resetHistory();

highlightSpy.restore();
unhighlightSpy.restore();
Expand Down Expand Up @@ -607,9 +650,9 @@ QUnit.module('HighlighterView', function(hooks) {
unhighlightSpy.resetHistory();

// Update (Default will unhighlight and highlight)
link.attr(['line', 'stroke'], 'red', { dirty: true });
link.attr(['line', 'stroke'], 'red', { dirty: false });
var node2 = linkView.el.querySelector('[joint-selector="line"]');
assert.notEqual(node, node2);
assert.equal(node, node2);
assert.ok(highlightSpy.calledOnce);
assert.ok(highlightSpy.calledOnceWithExactly(linkView, node));
assert.ok(highlightSpy.calledOn(highlighter));
Expand All @@ -629,6 +672,33 @@ QUnit.module('HighlighterView', function(hooks) {
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();

var id2 = 'highlighter-id-2';
var node3 = linkView.el.querySelector('[joint-selector="wrapper"]');
var highlighter2 = joint.dia.HighlighterView.add(linkView, node3, id2);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();

// Re-render (will not highlight the node, because
// it's not in the DOM anymore)
link.attr(['line', 'stroke'], 'blue', { dirty: true });
var node4 = linkView.el.querySelector('[joint-selector="wrapper"]');
assert.ok(!node3.isConnected);
assert.notEqual(node3, node4);
assert.notOk(highlightSpy.calledOnce);
assert.ok(unhighlightSpy.calledOnce);
assert.ok(unhighlightSpy.calledOnceWithExactly(linkView, node3));
assert.ok(unhighlightSpy.calledOn(highlighter2));
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();

// Unhighlight
joint.dia.HighlighterView.remove(linkView, id2);
assert.equal(joint.dia.HighlighterView.get(linkView, id2), null);
assert.notOk(unhighlightSpy.called);
assert.ok(highlightSpy.notCalled);
highlightSpy.resetHistory();
unhighlightSpy.resetHistory();

highlightSpy.restore();
unhighlightSpy.restore();
});
Expand Down
Loading