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

Add shadow dom support #1805

Open
wants to merge 3 commits into
base: develop
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
29 changes: 22 additions & 7 deletions core/emitter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import EventEmitter from 'eventemitter3';
import instances from './instances';
import logger from './logger';

const debug = logger('quill:events');
const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
const EMITTERS = [];

EVENTS.forEach(eventName => {
document.addEventListener(eventName, (...args) => {
Array.from(document.querySelectorAll('.ql-container')).forEach(node => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
}
EMITTERS.forEach(em => {
em.handleDOM(...args);
});
});
});
Expand All @@ -20,6 +17,7 @@ class Emitter extends EventEmitter {
constructor() {
super();
this.listeners = {};
EMITTERS.push(this);
Copy link
Author

Choose a reason for hiding this comment

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

@jhchen we could possible iterate through EMITTERS here, looking for any with a container which is no longer in the DOM tree. this isn't straight forward, though, because document.body.contains(node) is false for nodes in shadow trees. we would have to go up the tree (getRootNode) until we reach a node which is in document , if ever.

Copy link
Author

@43081j 43081j Dec 26, 2017

Choose a reason for hiding this comment

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

ah this won't work, we don't have any idea what container is at this point

this.on('error', debug.error);
}

Expand All @@ -29,8 +27,25 @@ class Emitter extends EventEmitter {
}

handleDOM(event, ...args) {
const target = event.composedPath()[0];
const containsNode = (node, child) => {
if (child.getRootNode() === document) {
return node.contains(child);
}

while (!node.contains(child)) {
const root = child.getRootNode();
if (!root || !root.host) {
return false;
}
child = root.host;
}

return true;
};

(this.listeners[event.type] || []).forEach(({ node, handler }) => {
if (event.target === node || node.contains(event.target)) {
if (target === node || containsNode(node, target)) {
handler(event, ...args);
}
});
Expand Down
13 changes: 8 additions & 5 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class Selection {
this.composing = false;
this.mouseDown = false;
this.root = this.scroll.domNode;
this.rootDocument = this.root.getRootNode
? this.root.getRootNode()
: document;
this.cursor = this.scroll.create('cursor', this);
// savedRange is last non-null range
this.savedRange = new Range(0, 0);
this.lastRange = this.savedRange;
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', document, () => {
this.emitter.listenDOM('selectionchange', this.rootDocument, () => {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
Expand Down Expand Up @@ -175,7 +178,7 @@ class Selection {
}

getNativeRange() {
const selection = document.getSelection();
const selection = this.rootDocument.getSelection();
if (selection == null || selection.rangeCount <= 0) return null;
const nativeRange = selection.getRangeAt(0);
if (nativeRange == null) return null;
Expand All @@ -193,8 +196,8 @@ class Selection {

hasFocus() {
return (
document.activeElement === this.root ||
contains(this.root, document.activeElement)
this.rootDocument.activeElement === this.root ||
contains(this.root, this.rootDocument.activeElement)
);
}

Expand Down Expand Up @@ -316,7 +319,7 @@ class Selection {
) {
return;
}
const selection = document.getSelection();
const selection = this.rootDocument.getSelection();
if (selection == null) return;
if (startNode != null) {
if (!this.hasFocus()) this.root.focus();
Expand Down
3 changes: 2 additions & 1 deletion modules/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Toolbar extends Module {
quill.container.parentNode.insertBefore(container, quill.container);
this.container = container;
} else if (typeof this.options.container === 'string') {
this.container = document.querySelector(this.options.container);
const rootDocument = quill.container.getRootNode();
this.container = rootDocument.querySelector(this.options.container);
} else {
this.container = this.options.container;
}
Expand Down
41 changes: 41 additions & 0 deletions test/unit/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@ describe('Selection', function() {
});
});

describe('shadow root', function() {
Copy link
Author

Choose a reason for hiding this comment

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

@jhchen am i ok to just disable these where attachShadow isn't supported? firefox tests will fail until i do as they're unusually far behind in implementing the shadow dom spec.

// Some browsers don't support shadow DOM
if (!document.head.attachShadow) {
return;
}

let container;
let root;

beforeEach(function() {
root = document.createElement('div');
root.attachShadow({ mode: 'open' });
root.shadowRoot.innerHTML = '<div></div>';

document.body.appendChild(root);

container = root.shadowRoot.firstChild;
});

afterEach(function() {
document.body.removeChild(root);
});

it('getRange()', function() {
const selection = this.initialize(Selection, '<p>0123</p>', container);
selection.setNativeRange(container.firstChild.firstChild, 1);
const [range] = selection.getRange();
expect(range.index).toEqual(1);
expect(range.length).toEqual(0);
});

it('setRange()', function() {
const selection = this.initialize(Selection, '', container);
const expected = new Range(0);
selection.setRange(expected);
const [range] = selection.getRange();
expect(range).toEqual(expected);
expect(selection.hasFocus()).toBe(true);
});
});

describe('getRange()', function() {
it('empty document', function() {
const selection = this.initialize(Selection, '');
Expand Down
32 changes: 32 additions & 0 deletions test/unit/modules/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,38 @@ describe('Toolbar', function() {
});
});

describe('shadow dom', function() {
// Some browsers don't support shadow DOM
if (!document.head.attachShadow) {
return;
}

let container;
let editor;

beforeEach(function() {
container = document.createElement('div');
container.attachShadow({ mode: 'open' });
container.shadowRoot.innerHTML = `
<div class="editor"></div>
<div class="toolbar"></div>`;

editor = new Quill(container.shadowRoot.querySelector('.editor'), {
modules: {
toolbar: '.toolbar',
},
});
});

it('should initialise', function() {
const editorDiv = container.shadowRoot.querySelector('.editor');
const toolbarDiv = container.shadowRoot.querySelector('.toolbar');
expect(editorDiv.className).toBe('editor ql-container');
expect(toolbarDiv.className).toBe('toolbar ql-toolbar');
expect(editor.container).toBe(editorDiv);
});
});

describe('active', function() {
beforeEach(function() {
const container = this.initialize(
Expand Down