Skip to content
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

fix(core): stop mutating Context's input #3076

Merged
merged 12 commits into from
Jul 15, 2021
7 changes: 4 additions & 3 deletions lib/core/base/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
select,
isNodeInContext,
nodeSorter,
respondable
respondable,
clone
} from '../utils';

/**
Expand Down Expand Up @@ -91,9 +92,9 @@ function normalizeContext(context) {
return {
include:
context.include && +context.include.length
? context.include
? Array.from(context.include)
: [document],
exclude: context.exclude || []
exclude: context.exclude ? clone(context.exclude) : []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't going to work. Exclude can contain DOM nodes. We can't clone DOM elements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like DOM nodes aren't being cloned.

};
}

Expand Down
3 changes: 3 additions & 0 deletions lib/core/utils/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function clone(obj) {
var index,
length,
out = obj;
if (obj instanceof window.Node) {
return obj;
}

if (obj !== null && typeof obj === 'object') {
if (Array.isArray(obj)) {
Expand Down
16 changes: 16 additions & 0 deletions test/core/base/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ describe('Context', function() {
fixture.innerHTML = '';
});

it('should not mutate exclude in input', function() {
fixture.innerHTML = '<div id="foo"></div>';
var context = { exclude: [['iframe', '#foo']] };
// eslint-disable-next-line no-new
new Context(context);
assert.deepEqual(context, { exclude: [['iframe', '#foo']] });
});

it('should not mutate its include input', function() {
fixture.innerHTML = '<div id="foo"></div>';
var context = { include: [['#foo']] };
// eslint-disable-next-line no-new
new Context(context);
assert.deepEqual(context, { include: [['#foo']] });
});

describe('include', function() {
it('should accept a single selector', function() {
fixture.innerHTML = '<div id="foo"></div>';
Expand Down