From dc2b78a94872e00bf9e12f4059eef60ac1312063 Mon Sep 17 00:00:00 2001 From: Filip Engberg Date: Tue, 31 Oct 2017 12:02:16 +0100 Subject: [PATCH 1/2] Add support for overriding block element using blockStyleFn - Add test --- .../src/__tests__/stateToHTML-test.js | 33 +++++++++++++++++++ .../draft-js-export-html/src/stateToHTML.js | 13 +++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js b/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js index 36fb0ac3..52d30232 100644 --- a/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js +++ b/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js @@ -206,6 +206,39 @@ describe('stateToHTML', () => { ); }); + it('blockStyleFn should support setting custom element', () => { + let options = { + blockStyleFn: (block) => ({ + element: 'li', + }), + }; + + let contentState1 = convertFromRaw( + //

Hello world.

+ { + entityMap: {}, + blocks: [ + { + key: 'dn025', + text: 'Hello world.', + type: 'unstyled', + depth: 0, + inlineStyleRanges: [{offset: 6, length: 5, style: 'ITALIC'}], + entityRanges: [], + }, + ], + }, // eslint-disable-line + ); + + expect(stateToHTML(contentState1)).toBe( + '

Hello world.

', + ); + + expect(stateToHTML(contentState1, options)).toBe( + '
  • Hello world.
  • ', + ); + }) + it('should support custom entity styles', () => { let options = { entityStyleFn: (entity) => { diff --git a/packages/draft-js-export-html/src/stateToHTML.js b/packages/draft-js-export-html/src/stateToHTML.js index d90b7fac..89991247 100644 --- a/packages/draft-js-export-html/src/stateToHTML.js +++ b/packages/draft-js-export-html/src/stateToHTML.js @@ -285,7 +285,7 @@ class MarkupGenerator { let attrString; if (this.options.blockStyleFn) { - let {attributes, style} = this.options.blockStyleFn(block) || {}; + let {attributes, style, element} = this.options.blockStyleFn(block) || {}; // Normalize `className` -> `class`, etc. attributes = normalizeAttributes(attributes); if (style != null) { @@ -295,6 +295,9 @@ class MarkupGenerator { ? {style: styleAttr} : {...attributes, style: styleAttr}; } + if (element != null) { + tags = [element]; + } attrString = stringifyAttrs(attributes); } else { attrString = ''; @@ -307,6 +310,14 @@ class MarkupGenerator { writeEndTag(block, defaultBlockTag) { let tags = getTags(block.getType(), defaultBlockTag); + + if (this.options.blockStyleFn) { + let {element} = this.options.blockStyleFn(block) || {}; + if (element != null) { + tags = [element]; + } + } + if (tags.length === 1) { this.output.push(`\n`); } else { From dfd8f250db561aad57e46a2bd215bed077652b4e Mon Sep 17 00:00:00 2001 From: Filip Engberg Date: Tue, 31 Oct 2017 14:46:13 +0100 Subject: [PATCH 2/2] Fix travis errors --- .../draft-js-export-html/src/__tests__/stateToHTML-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js b/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js index 52d30232..b971109b 100644 --- a/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js +++ b/packages/draft-js-export-html/src/__tests__/stateToHTML-test.js @@ -208,7 +208,7 @@ describe('stateToHTML', () => { it('blockStyleFn should support setting custom element', () => { let options = { - blockStyleFn: (block) => ({ + blockStyleFn: () => ({ element: 'li', }), }; @@ -237,7 +237,7 @@ describe('stateToHTML', () => { expect(stateToHTML(contentState1, options)).toBe( '
  • Hello world.
  • ', ); - }) + }); it('should support custom entity styles', () => { let options = {