From 30f1a9ef1dad8b05231a7c29ccfe6cf35618b011 Mon Sep 17 00:00:00 2001 From: Conor Cussell Date: Wed, 19 Jul 2017 03:44:10 +0200 Subject: [PATCH] Allow HTMLSerializer to deserialize an empty doc (#915) * Allow HTML serializer to deserialize an empty document This specially handles the case where the HTML is empty. Test by running: ``` const html = new HTML() html.deserialize('') ``` Closes #539 * Use defaultBlockType in HTML serializer * Add test case for deserialising empty string * Use fixtures --- src/serializers/html.js | 18 +++++++++++++----- .../html/deserialize/empty-string/index.js | 1 + .../html/deserialize/empty-string/input.html | 0 .../html/deserialize/empty-string/output.yaml | 7 +++++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 test/serializers/fixtures/html/deserialize/empty-string/index.js create mode 100644 test/serializers/fixtures/html/deserialize/empty-string/input.html create mode 100644 test/serializers/fixtures/html/deserialize/empty-string/output.yaml diff --git a/src/serializers/html.js b/src/serializers/html.js index c67769c2f..f6646bbe4 100644 --- a/src/serializers/html.js +++ b/src/serializers/html.js @@ -98,6 +98,11 @@ class Html { const children = $.children().toArray() let nodes = this.deserializeElements(children) + const { defaultBlockType } = this + const defaults = typeof defaultBlockType == 'string' + ? { type: defaultBlockType } + : defaultBlockType + // HACK: ensure for now that all top-level inline are wrapped into a block. nodes = nodes.reduce((memo, node, i, original) => { if (node.kind == 'block') { @@ -111,11 +116,6 @@ class Html { return memo } - const { defaultBlockType } = this - const defaults = typeof defaultBlockType == 'string' - ? { type: defaultBlockType } - : defaultBlockType - const block = { kind: 'block', nodes: [node], @@ -126,6 +126,14 @@ class Html { return memo }, []) + if (nodes.length === 0) { + nodes = [{ + kind: 'block', + nodes: [], + ...defaults + }] + } + const raw = { kind: 'state', document: { diff --git a/test/serializers/fixtures/html/deserialize/empty-string/index.js b/test/serializers/fixtures/html/deserialize/empty-string/index.js new file mode 100644 index 000000000..56004c9f9 --- /dev/null +++ b/test/serializers/fixtures/html/deserialize/empty-string/index.js @@ -0,0 +1 @@ +export default {} \ No newline at end of file diff --git a/test/serializers/fixtures/html/deserialize/empty-string/input.html b/test/serializers/fixtures/html/deserialize/empty-string/input.html new file mode 100644 index 000000000..e69de29bb diff --git a/test/serializers/fixtures/html/deserialize/empty-string/output.yaml b/test/serializers/fixtures/html/deserialize/empty-string/output.yaml new file mode 100644 index 000000000..b90a670ca --- /dev/null +++ b/test/serializers/fixtures/html/deserialize/empty-string/output.yaml @@ -0,0 +1,7 @@ +data: {} +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - characters: [] \ No newline at end of file