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

Greyepoxy/issue 2633 #2953

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ Quill.imports = {
'core/theme': Theme,
};

function expandConfig(container, userConfig) {
function expandConfig(container, userConfig, containerDocument) {
containerDocument = containerDocument || document;
userConfig = extend(
true,
{
Expand Down Expand Up @@ -524,7 +525,7 @@ function expandConfig(container, userConfig) {
);
['bounds', 'container', 'scrollingContainer'].forEach(key => {
if (typeof userConfig[key] === 'string') {
userConfig[key] = document.querySelector(userConfig[key]);
userConfig[key] = containerDocument.querySelector(userConfig[key]);
}
});
userConfig.modules = Object.keys(userConfig.modules).reduce(
Expand Down
16 changes: 8 additions & 8 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Selection {
this.lastRange = this.savedRange;
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', document, () => {
this.emitter.listenDOM('selectionchange', this.root.ownerDocument, () => {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
Expand Down Expand Up @@ -89,10 +89,10 @@ class Selection {
}

handleDragging() {
this.emitter.listenDOM('mousedown', document.body, () => {
this.emitter.listenDOM('mousedown', this.root.ownerDocument.body, () => {
this.mouseDown = true;
});
this.emitter.listenDOM('mouseup', document.body, () => {
this.emitter.listenDOM('mouseup', this.root.ownerDocument.body, () => {
this.mouseDown = false;
this.update(Emitter.sources.USER);
});
Expand Down Expand Up @@ -150,7 +150,7 @@ class Selection {
}
let side = 'left';
let rect;
if (node instanceof Text) {
if (node instanceof Text || node.nodeType === Node.TEXT_NODE) {
if (offset < node.data.length) {
range.setStart(node, offset);
range.setEnd(node, offset + 1);
Expand All @@ -175,7 +175,7 @@ class Selection {
}

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

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

Expand Down Expand Up @@ -316,7 +316,7 @@ class Selection {
) {
return;
}
const selection = document.getSelection();
const selection = this.root.ownerDocument.getSelection();
if (selection == null) return;
if (startNode != null) {
if (!this.hasFocus()) this.root.focus();
Expand Down
2 changes: 1 addition & 1 deletion modules/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Toolbar extends Module {
} else {
this.container = this.options.container;
}
if (!(this.container instanceof HTMLElement)) {
if (this.container.ownerDocument === 'undefined') {
return debug.error('Container required for toolbar', this.options);
}
this.container.classList.add('ql-toolbar');
Expand Down
37 changes: 33 additions & 4 deletions test/helpers/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import Selection from '../../core/selection';
import Scroll from '../../blots/scroll';
import Quill, { globalRegistry } from '../../core/quill';

const div = document.createElement('div');
div.id = 'test-container';
document.body.appendChild(div);
const div = setupTestContainer();

window.onerror = function(msg) {
return msg === 'Script error.';
Expand All @@ -28,6 +26,37 @@ beforeEach(function() {
this.initialize = initialize.bind(this);
});

function setupTestContainer() {
const testFrame = document.createElement('iframe');
testFrame.setAttribute('width', 2000);
testFrame.setAttribute('height', 2000);
testFrame.setAttribute('frameborder', 0);
document.body.appendChild(testFrame);
const headerCssNodesHtml = getCssLinkNodesText(document.head);
const bodyCssNodesHtml = getCssLinkNodesText(document.body);
testFrame.contentWindow.document.open();
testFrame.contentWindow.document.write(
`<!DOCTYPE html>\n<html><head>${headerCssNodesHtml}</head><body>${bodyCssNodesHtml}</body></html>`,
);
testFrame.contentWindow.document.close();
const divTestContainer = document.createElement('div');
divTestContainer.id = 'test-container';
testFrame.contentWindow.document.body.appendChild(divTestContainer);
return divTestContainer;
}

function getCssLinkNodesText(elementToSearchFrom) {
const cssLinkNodes = elementToSearchFrom.querySelectorAll(
'link[type="text/css"]',
);

return Array.from(cssLinkNodes.values())
.map(function(element) {
return element.outerHTML;
})
.join('');
}

function compareApproximately(actual, expected, tolerance) {
const pass = Math.abs(actual - expected) <= tolerance;
return {
Expand All @@ -38,7 +67,7 @@ function compareApproximately(actual, expected, tolerance) {

function compareHTML(actual, expected, ignoreClassId, ignoreUI = true) {
const [div1, div2] = [actual, expected].map(function(html) {
if (html instanceof HTMLElement) {
if (typeof html !== 'string') {
html = html.innerHTML;
}
const container = document.createElement('div');
Expand Down
22 changes: 15 additions & 7 deletions test/unit/core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,19 @@ describe('Quill', function() {
});

it('convert css selectors', function() {
const config = expandConfig('#test-container', {
bounds: '#test-container',
});
expect(config.bounds).toEqual(document.querySelector('#test-container'));
const containerDocument = this.container.ownerDocument;
const config = expandConfig(
'#test-container',
{
bounds: '#test-container',
},
containerDocument,
);
expect(config.bounds).toEqual(
containerDocument.querySelector('#test-container'),
);
expect(config.container).toEqual(
document.querySelector('#test-container'),
containerDocument.querySelector('#test-container'),
);
});

Expand Down Expand Up @@ -610,13 +617,14 @@ describe('Quill', function() {
});

it('toolbar container shorthand', function() {
const containerDocument = this.container.ownerDocument;
const config = expandConfig('#test-container', {
modules: {
toolbar: document.querySelector('#test-container'),
toolbar: containerDocument.querySelector('#test-container'),
},
});
expect(config.modules.toolbar).toEqual({
container: document.querySelector('#test-container'),
container: containerDocument.querySelector('#test-container'),
handlers: Toolbar.DEFAULTS.handlers,
});
});
Expand Down