Skip to content

Commit

Permalink
Fix white spaces not being preserved when pasted into editor
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 17, 2024
1 parent 5d752e3 commit 6e43f67
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
47 changes: 40 additions & 7 deletions packages/quill/src/modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,47 @@ function isBetweenInlineElements(node: HTMLElement, scroll: ScrollBlot) {
}

const preNodes = new WeakMap();
function isPre(node: Node | null) {

enum WhiteSpaceStyle {
Collapsed,
Preserved,
Inherited,
}
const getWhiteSpaceStyle = (node: Node) => {
if (node instanceof HTMLElement) {
const { whiteSpace } = node.style;
if (
whiteSpace === 'pre' ||
whiteSpace === 'pre-wrap' ||
whiteSpace === 'break-spaces'
) {
return WhiteSpaceStyle.Preserved;
}
if (whiteSpace && whiteSpace !== 'inherit') {
return WhiteSpaceStyle.Collapsed;
}
if (node.tagName === 'PRE') {
return WhiteSpaceStyle.Preserved;
}
}
return WhiteSpaceStyle.Inherited;
};

function shouldPreserveWhiteSpaces(node: Node | null) {
if (node == null) return false;

if (!preNodes.has(node)) {
// @ts-expect-error
if (node.tagName === 'PRE') {
preNodes.set(node, true);
} else {
preNodes.set(node, isPre(node.parentNode));
const type = getWhiteSpaceStyle(node);
switch (type) {
case WhiteSpaceStyle.Preserved:
preNodes.set(node, true);
break;
case WhiteSpaceStyle.Collapsed:
preNodes.set(node, false);
break;
case WhiteSpaceStyle.Inherited:
preNodes.set(node, shouldPreserveWhiteSpaces(node.parentNode));
break;
}
}
return preNodes.get(node);
Expand Down Expand Up @@ -631,7 +664,7 @@ function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) {
if (node.parentElement?.tagName === 'O:P') {
return delta.insert(text.trim());
}
if (!isPre(node)) {
if (!shouldPreserveWhiteSpaces(node)) {
if (
text.trim().length === 0 &&
text.includes('\n') &&
Expand Down
16 changes: 16 additions & 0 deletions packages/quill/test/unit/modules/clipboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ describe('Clipboard', () => {
);
});

test('white-space', () => {
const html = '<span style="white-space:pre">0 1</span>';
expect(createClipboard().convert({ html })).toEqual(
new Delta().insert('0 1'),
);
});

test('white-space with nested levels', () => {
const html = `
<div style="white-space:pre"><span>0 1</span><span style="white-space:normal">0 1</span><span style="white-space:inherit">0 1</span></div>
`;
expect(createClipboard().convert({ html })).toEqual(
new Delta().insert('0 10 10 1'),
);
});

test('nested list', () => {
const delta = createClipboard().convert({
html: '<ol><li>One</li><li class="ql-indent-1">Alpha</li></ol>',
Expand Down

0 comments on commit 6e43f67

Please sign in to comment.