diff options
author | Julius Härtl <jus@bitgrid.net> | 2019-07-02 21:22:20 +0300 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-07-02 21:22:20 +0300 |
commit | 7a5063d10564bd2bb0142c532a845b963ca533fd (patch) | |
tree | 2f0af27e84e7510dd8c0f208b8ef9d9acef16b1d | |
parent | e6a951fe693cee8655324fd0347a5cc1ef0c8806 (diff) |
Use text node to serialize plain text
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | src/EditorFactory.js | 16 | ||||
-rw-r--r-- | src/tests/markdown.spec.js | 3 | ||||
-rw-r--r-- | src/tests/plaintext.spec.js | 18 |
3 files changed, 32 insertions, 5 deletions
diff --git a/src/EditorFactory.js b/src/EditorFactory.js index 3a991f01b..ec9a8835e 100644 --- a/src/EditorFactory.js +++ b/src/EditorFactory.js @@ -101,6 +101,9 @@ const createEditor = ({ content, onUpdate, extensions, enableRichEditing, langua const markdownit = MarkdownIt('commonmark', { html: false, breaks: false }) .enable('strikethrough') +const SerializeException = (message) => { + this.message = message +} const createMarkdownSerializer = (_nodes, _marks) => { const nodes = Object .entries(_nodes) @@ -124,9 +127,16 @@ const createMarkdownSerializer = (_nodes, _marks) => { } const serializePlainText = (tiptap) => { - const tmp = document.createElement('div') - tmp.innerHTML = tiptap.getHTML() - return tmp.textContent || tmp.innerText || '' + const doc = tiptap.getJSON() + + if (doc.content.length !== 1 || doc.content[0].content.length !== 1) { + throw new SerializeException('Failed to serialize document to plain text') + } + const codeBlock = doc.content[0].content[0] + if (codeBlock.type !== 'text') { + throw new SerializeException('Failed to serialize document to plain text') + } + return codeBlock.text } export default createEditor diff --git a/src/tests/markdown.spec.js b/src/tests/markdown.spec.js index a191d15c1..cf011929b 100644 --- a/src/tests/markdown.spec.js +++ b/src/tests/markdown.spec.js @@ -75,6 +75,9 @@ describe('Markdown though editor', () => { test('images', () => { expect(markdownThroughEditor('![test](foo)')).toBe('![test](foo)') }) + test('special characters', () => { + expect(markdownThroughEditor('"\';&.-#><')).toBe('"\';&.-#><') + }) }) describe('Markdown serializer from html', () => { diff --git a/src/tests/plaintext.spec.js b/src/tests/plaintext.spec.js index 19f87d40f..a139a4a86 100644 --- a/src/tests/plaintext.spec.js +++ b/src/tests/plaintext.spec.js @@ -1,5 +1,6 @@ import { markdownit, createEditor, createMarkdownSerializer, serializePlainText } from './../EditorFactory'; import spec from "./fixtures/spec" +import xssFuzzVectors from './fixtures/xssFuzzVectors'; const escapeHTML = (s) => { return s.toString() @@ -19,7 +20,7 @@ const plaintextThroughEditor = (markdown) => { return serializePlainText(tiptap) || 'failed' } -describe('Markdown though editor', () => { +describe('commonmark as plaintext', () => { // FIXME: Those two tests currently fail as trailing whitespace seems to be stripped, // if it occurs in the first line which is empty otherwise. const skippedMarkdownTests = [ @@ -34,7 +35,8 @@ describe('Markdown though editor', () => { expect(plaintextThroughEditor(entry.markdown)).toBe(entry.markdown) }) }) - +}) +describe('markdown as plaintext', () => { test('headlines', () => { expect(plaintextThroughEditor('# Test')).toBe('# Test') expect(plaintextThroughEditor('## Test')).toBe('## Test') @@ -66,3 +68,15 @@ describe('Markdown though editor', () => { expect(plaintextThroughEditor('![test](foo)')).toBe('![test](foo)') }) }) + +describe('html as plain text', () => { + test('link', () => { + expect(plaintextThroughEditor('<a>sdf</a>')).toBe('<a>sdf</a>') + expect(plaintextThroughEditor('<a href="foobar">sdf</a>')).toBe('<a href="foobar">sdf</a>') + + }) + test('special characters', () => { + expect(plaintextThroughEditor('"\';&.-#><')).toBe('"\';&.-#><') + expect(plaintextThroughEditor(xssFuzzVectors)).toBe(xssFuzzVectors) + }) +}) |