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/compare document position fails to work correctly in shadow dom #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"build:umd": "npm-run-all --sequential build:umd:bundle build:umd:min build:umd:clean",
"build:umd:bundle": "browserify dist/src/ally.js --debug --standalone ally --transform rollupify --transform [ babelify --presets [ es2015 ] ] | exorcist dist/ally.js.map > dist/ally.js",
"build:umd:clean": "rm dist/ally.js dist/ally.js.map",
"build:umd:min": "uglifyjs dist/ally.js --in-source-map dist/ally.js.map --source-map dist/ally.min.js.map --source-map-url ally.min.js.map --preamble \"/*! ${npm_package_name} - v${npm_package_version} - ${npm_package_homepage} - ${npm_package_license} License */\" --mangle --compress --output dist/ally.min.js",
"build:umd:min": "uglifyjs dist/ally.js --in-source-map dist/ally.js.map --source-map --source-map-url --preamble \"/*! ${npm_package_name} - v${npm_package_version} - ${npm_package_homepage} - ${npm_package_license} License */\" --mangle --compress --output dist/ally.min.js",
"build:common": "babel --no-babelrc --presets es2015 --plugins add-module-exports --source-maps --out-dir dist/common dist/src",
"build:amd": "babel --no-babelrc --presets es2015 --plugins add-module-exports,transform-es2015-modules-umd --source-maps --out-dir dist/amd dist/src",
"build:esm": "babel --source-maps --out-dir dist/esm dist/src",
Expand Down
14 changes: 12 additions & 2 deletions src/util/compare-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@
listOfElements.some(isChildOf)
*/

import {getShadowParent} from './get-shadow-parent';

export function getParentComparator({parent, element, includeSelf} = {}) {
if (parent) {
return function isChildOf(node) {
const shadowParent = getShadowParent(node);
return Boolean(
includeSelf && node === parent
(includeSelf && node === parent
|| shadowParent === parent)
|| parent.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY
|| (shadowParent
&& parent.compareDocumentPosition(shadowParent) & Node.DOCUMENT_POSITION_CONTAINED_BY)
);
};
} else if (element) {
return function isParentOf(node) {
const shadowParent = getShadowParent(element);
return Boolean(
includeSelf && element === node
(includeSelf && element === node
|| shadowParent === node)
|| node.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_CONTAINED_BY
|| (shadowParent
&& node.compareDocumentPosition(shadowParent) & Node.DOCUMENT_POSITION_CONTAINED_BY)
);
};
}
Expand Down
13 changes: 13 additions & 0 deletions src/util/get-shadow-parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Get the shadow root host element if the node passed in is in the shadow root
USAGE:
var shadowParent = getShadowParent(someNode)
*/
export function getShadowParent(node) {
for (; node; node = node.parentNode) {
if (node.toString() === '[object ShadowRoot]') {
return node.host;
}
}
return null;
}
7 changes: 7 additions & 0 deletions test/unit/maintain.disabled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ define(function(require) {
expect(fixture.input.after.disabled).to.equal(false, 'not disabled within filter');
expect(fixture.input.first.disabled).to.equal(false, 'not disabled within Shadow Root');
});

bdd.it('should not disable filtered shadowed elements', function() {
handle = maintainDisabled({
filter: '#first-shadow-host',
});
expect(fixture.input.first.disabled).to.equal(false, 'not disabled within Shadow Root');
});
});

bdd.describe('for DOM Mutation', function() {
Expand Down