From 7a5063d10564bd2bb0142c532a845b963ca533fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 2 Jul 2019 20:22:20 +0200 Subject: Use text node to serialize plain text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- src/EditorFactory.js | 16 +++++++++++++--- src/tests/markdown.spec.js | 3 +++ 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('sdf')).toBe('sdf') + expect(plaintextThroughEditor('sdf')).toBe('sdf') + + }) + test('special characters', () => { + expect(plaintextThroughEditor('"\';&.-#><')).toBe('"\';&.-#><') + expect(plaintextThroughEditor(xssFuzzVectors)).toBe(xssFuzzVectors) + }) +}) -- cgit v1.2.3