Skip to content

Commit

Permalink
getSemanticHTML() exports white-space style when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 20, 2024
1 parent 2f59bea commit 355ba75
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
17 changes: 16 additions & 1 deletion packages/quill/src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ function convertListHTML(
return `</li></${endTag}>${convertListHTML(items, lastIndent - 1, types)}`;
}

const SPACE_EXCLUDE_NBSP = '[^\\S\\u00a0]';
const COLLAPSIBLE_SPACES_REGEX = new RegExp(
`^${SPACE_EXCLUDE_NBSP}|${SPACE_EXCLUDE_NBSP}{2,}|${SPACE_EXCLUDE_NBSP}$`,
);
function convertHTML(
blot: Blot,
index: number,
Expand All @@ -370,7 +374,18 @@ function convertHTML(
return blot.html(index, length);
}
if (blot instanceof TextBlot) {
return escapeText(blot.value().slice(index, index + length));
const escapedText = escapeText(blot.value().slice(index, index + length));

if (!COLLAPSIBLE_SPACES_REGEX.test(escapedText)) {
return escapedText;
}

const { parentElement } = blot.domNode;
const style =
parentElement?.ownerDocument.defaultView?.getComputedStyle(parentElement);
return style?.whiteSpace
? `<span style="white-space:${style.whiteSpace}">${escapedText}</span>`
: escapedText;
}
if (blot instanceof ParentBlot) {
// TODO fix API
Expand Down
65 changes: 63 additions & 2 deletions packages/quill/test/unit/core/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import { ColorClass } from '../../../src/formats/color.js';
import Quill from '../../../src/core.js';
import { normalizeHTML } from '../__helpers__/utils.js';

const createEditor = (html: string) => {
const createEditor = (htmlOrContents: string | Delta) => {
const container = document.createElement('div');
container.innerHTML = normalizeHTML(html);
if (typeof htmlOrContents === 'string') {
container.innerHTML = normalizeHTML(htmlOrContents);
}
document.body.appendChild(container);
const quill = new Quill(container, {
registry: createRegistry([
Expand All @@ -54,9 +56,17 @@ const createEditor = (html: string) => {
SizeClass,
]),
});
if (typeof htmlOrContents !== 'string') {
quill.setContents(htmlOrContents);
}
return quill.editor;
};

const applyWhiteSpace = (editor: Editor, whiteSpace: string) => {
editor.scroll.domNode.style.whiteSpace = whiteSpace;
return editor;
};

describe('Editor', () => {
describe('insert', () => {
test('text', () => {
Expand Down Expand Up @@ -1246,6 +1256,57 @@ describe('Editor', () => {
);
});

test('collapsible spaces', () => {
expect(
applyWhiteSpace(
createEditor('<p><strong>123 </strong>123<em> 123</em></p>'),
'pre-wrap',
).getHTML(0, 11),
).toEqual(
'<strong><span style="white-space:pre-wrap">123 </span></strong>123<em><span style="white-space:pre-wrap"> 123</span></em>',
);

expect(
applyWhiteSpace(
createEditor(new Delta().insert('1 2\n')),
'pre-wrap',
).getHTML(0, 5),
).toEqual('<span style="white-space:pre-wrap">1 2</span>');

expect(
applyWhiteSpace(
createEditor(
new Delta().insert(' 123', { bold: true }).insert('\n'),
),
'pre-wrap',
).getHTML(0, 5),
).toEqual(
'<strong><span style="white-space:pre-wrap"> 123</span></strong>',
);
});

test('&nbsp;', () => {
expect(
applyWhiteSpace(
createEditor(
new Delta().insert('\u00a0 123', { bold: true }).insert('\n'),
),
'pre-wrap',
).getHTML(0, 5),
).toEqual('<strong>\u00a0 123</strong>');

expect(
applyWhiteSpace(
createEditor(
new Delta().insert('\u00a0 123', { bold: true }).insert('\n'),
),
'pre-wrap',
).getHTML(0, 6),
).toEqual(
'<strong><span style="white-space:pre-wrap">\u00a0 123</span></strong>',
);
});

test('mixed list', () => {
const editor = createEditor(
`
Expand Down

0 comments on commit 355ba75

Please sign in to comment.