Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/draft-js-export-html/src/__tests__/stateToHTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ describe('stateToHTML', () => {
);
});

it('blockStyleFn should support setting custom element', () => {
let options = {
blockStyleFn: () => ({
element: 'li',
}),
};

let contentState1 = convertFromRaw(
// <h1>Hello <em>world</em>.</h1>
{
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(
'<p>Hello <em>world</em>.</p>',
);

expect(stateToHTML(contentState1, options)).toBe(
'<li>Hello <em>world</em>.</li>',
);
});

it('should support custom entity styles', () => {
let options = {
entityStyleFn: (entity) => {
Expand Down
13 changes: 12 additions & 1 deletion packages/draft-js-export-html/src/stateToHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -295,6 +295,9 @@ class MarkupGenerator {
? {style: styleAttr}
: {...attributes, style: styleAttr};
}
if (element != null) {
tags = [element];
}
attrString = stringifyAttrs(attributes);
} else {
attrString = '';
Expand All @@ -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(`</${tags[0]}>\n`);
} else {
Expand Down