Skip to content

Commit

Permalink
feat: Extracting stripWhitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcs committed Oct 29, 2023
1 parent df225c2 commit 5300b51
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe('when stripWhitespace is true', () => {
text: 'one two three',
},
{
text: 'hello one two three four',
text: 'hello one two\nthree\nfour\n',
},
{
text: '\nhello one two\nthree\nfour\n',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PlateEditor } from '../../../types/PlateEditor';
import { normalizeDescendantsToDocumentFragment } from '../../../utils/normalizeDescendantsToDocumentFragment';
import { deserializeHtmlElement } from './deserializeHtmlElement';
import { htmlStringToDOMNode } from './htmlStringToDOMNode';
import { setStripWhitespace } from './stripWhitespaceConfig';

/**
* Deserialize HTML element to a valid document fragment.
Expand All @@ -19,13 +18,12 @@ export const deserializeHtml = <V extends Value>(
stripWhitespace?: boolean;
}
): EDescendant<V>[] => {
setStripWhitespace(stripWhitespace);
// for serializer
if (typeof element === 'string') {
element = htmlStringToDOMNode(element, stripWhitespace);
element = htmlStringToDOMNode(element);
}

const fragment = deserializeHtmlElement(editor, element) as EDescendant<V>[];
const fragment = deserializeHtmlElement(editor, element, stripWhitespace) as EDescendant<V>[];

return normalizeDescendantsToDocumentFragment(editor, {
descendants: fragment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { deserializeHtmlNode } from './deserializeHtmlNode';
*/
export const deserializeHtmlElement = <V extends Value>(
editor: PlateEditor<V>,
element: HTMLElement
element: HTMLElement,
stripWhitespace = true
): DeserializeHtmlNodeReturnType<EDescendant<V>> => {
return deserializeHtmlNode(editor)(element);
return deserializeHtmlNode(editor, stripWhitespace)(element);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import { isHtmlElement } from './isHtmlElement';
* Deserialize HTML element or child node.
*/
export const deserializeHtmlNode =
<V extends Value>(editor: PlateEditor<V>) =>
<V extends Value>(
editor: PlateEditor<V>,
stripWhitespace = true,
) =>
(
node: HTMLElement | ChildNode
): DeserializeHtmlNodeReturnType<EDescendant<V>> => {
const textNode = htmlTextNodeToString(node);
const textNode = htmlTextNodeToString(node, stripWhitespace);
if (textNode) return textNode;

if (!isHtmlElement(node)) return null;
Expand All @@ -27,13 +30,13 @@ export const deserializeHtmlNode =
if (breakLine) return breakLine;

// body
const fragment = htmlBodyToFragment(editor, node as HTMLElement);
const fragment = htmlBodyToFragment(editor, node as HTMLElement, stripWhitespace);
if (fragment) return fragment;

// element
const element = htmlElementToElement(editor, node as HTMLElement);
const element = htmlElementToElement(editor, node as HTMLElement, stripWhitespace);
if (element) return element;

// leaf
return htmlElementToLeaf(editor, node as HTMLElement);
return htmlElementToLeaf(editor, node as HTMLElement, stripWhitespace);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { deserializeHtmlNode } from './deserializeHtmlNode';

export const deserializeHtmlNodeChildren = <V extends Value>(
editor: PlateEditor<V>,
node: HTMLElement | ChildNode
node: HTMLElement | ChildNode,
stripWhitespace: boolean
) =>
Array.from(node.childNodes).flatMap(
deserializeHtmlNode(editor)
deserializeHtmlNode(editor, stripWhitespace)
) as DeserializeHtmlChildren<EDescendant<V>>[];
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ jsx;
*/
export const htmlBodyToFragment = <V extends Value>(
editor: PlateEditor<V>,
element: HTMLElement
element: HTMLElement,
stripWhitespace = true
): EDescendant<V>[] | undefined => {
if (element.nodeName === 'BODY') {
return jsx(
'fragment',
{},
deserializeHtmlNodeChildren(editor, element)
deserializeHtmlNodeChildren(editor, element, stripWhitespace)
) as EDescendant<V>[];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { pipeDeserializeHtmlElement } from './pipeDeserializeHtmlElement';
*/
export const htmlElementToElement = <V extends Value>(
editor: PlateEditor<V>,
element: HTMLElement
element: HTMLElement,
stripWhitespace = true
) => {
const deserialized = pipeDeserializeHtmlElement(editor, element);

Expand All @@ -19,7 +20,7 @@ export const htmlElementToElement = <V extends Value>(

let descendants =
node.children ??
(deserializeHtmlNodeChildren(editor, element) as TDescendant[]);
(deserializeHtmlNodeChildren(editor, element, stripWhitespace) as TDescendant[]);
if (descendants.length === 0 || withoutChildren) {
descendants = [{ text: '' }];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import { pipeDeserializeHtmlLeaf } from './pipeDeserializeHtmlLeaf';
*/
export const htmlElementToLeaf = <V extends Value>(
editor: PlateEditor<V>,
element: HTMLElement
element: HTMLElement,
stripWhitespace = true
) => {
const node = pipeDeserializeHtmlLeaf(editor, element);

return deserializeHtmlNodeChildren(editor, element).reduce(
return deserializeHtmlNodeChildren(editor, element, stripWhitespace).reduce(
(arr: TDescendant[], child) => {
if (!child) return arr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
*/
export const htmlStringToDOMNode = (
rawHtml: string,
stripWhitespace = true
) => {
const node = document.createElement('body');
node.innerHTML = rawHtml;

if (stripWhitespace) {
node.innerHTML = node.innerHTML.replaceAll(/(\r\n|[\t\n\r])/g, '');
}

return node;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { htmlTextNodeToString } from './htmlTextNodeToString';
import { setStripWhitespace } from './stripWhitespaceConfig';

describe('htmlTextNodeToString', () => {
beforeEach(() => {
setStripWhitespace(true);
});

describe('when empty div element', () => {
it('should be undefined', () => {
Expand Down Expand Up @@ -38,9 +34,7 @@ describe('htmlTextNodeToString', () => {
const input = document.createTextNode('\n\n\ntest\n\ntest\n\n');
const output = 'test\n\ntest';

setStripWhitespace(false);

expect(htmlTextNodeToString(input)).toEqual(output);
expect(htmlTextNodeToString(input, false)).toEqual(output);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function findParentElementWhiteSpace(node: HTMLElement | ChildNode) {
// Both `node.style` and `getComputedStyle` always return an empty value.
while (parentNode != null) {

// The <pre> default style is "white-space: pre;"
if (parentNode.nodeName === 'PRE') return 'pre';

if (parentNode.nodeType === Node.ELEMENT_NODE) {
const styleStr = getStyleFromNode(parentNode as typeof node);
const styles = styleStr ? styleToObject(styleStr) : {};
Expand Down Expand Up @@ -91,9 +94,9 @@ const mergeWhitespace = (node: HTMLElement | ChildNode) => {

export const htmlTextNodeToString = (
node: HTMLElement | ChildNode,
stripWhitespace = true
) => {
if (isHtmlText(node)) {
const stripWhitespace = getStripWhitespace();

if (stripWhitespace) {
mergeWhitespace(node);
Expand Down

This file was deleted.

0 comments on commit 5300b51

Please sign in to comment.