Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slatetohtml): do not add an empty style attribute #20

Merged
merged 2 commits into from
Dec 6, 2022
Merged
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
4 changes: 2 additions & 2 deletions __tests__/serializers/slateToDom/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { slateToDom } from '../../../src'
import { selectAll } from 'css-select'
import { parseDocument } from 'htmlparser2'
import { replaceElement, textContent } from 'domutils'
import { Document, Element, isTag, Node } from 'domhandler'
import { replaceElement } from 'domutils'
import { Element, Node } from 'domhandler'
import serializer from 'dom-serializer'

describe('slateToDom expected behaviour', () => {
Expand Down
17 changes: 16 additions & 1 deletion __tests__/serializers/slateToHtml/slateDemo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { slateToHtml, slateDemoSlateToDomConfig } from "../../../src"

describe('slateToHtml expected behaviour', () => {
it('encodes HTML entities', () => {
it('adds the `text-align:right;` css property/value to style', () => {
const html = '<p style="text-align:right;">This is a right aligned paragraph.</p>'
const slate = [
{
Expand All @@ -16,4 +16,19 @@ describe('slateToHtml expected behaviour', () => {
]
expect(slateToHtml(slate, slateDemoSlateToDomConfig)).toEqual(html)
})

it('does not add empty style attributes', () => {
const html = '<p>This is a right aligned paragraph.</p>'
const slate = [
{
children: [
{
text: "This is a right aligned paragraph.",
},
],
type: "paragraph",
},
]
expect(slateToHtml(slate, slateDemoSlateToDomConfig)).toEqual(html)
})
})
11 changes: 7 additions & 4 deletions src/serializers/slatetoHtml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { nestedMarkElements } from '../../utilities/domhandler'
import serializer from 'dom-serializer'
import { config as defaultConfig } from '../../config/slateToDom/default'
import { SlateToDomConfig } from '../..'
import { getNested, styleToString } from '../../utilities'
import { getNested, isEmptyObject, styleToString } from '../../utilities'

type SlateToHtml = (node: any[], config?: SlateToDomConfig) => string
type SlateToDom = (node: any[], config?: SlateToDomConfig) => AnyNode | ArrayLike<AnyNode>
Expand Down Expand Up @@ -46,9 +46,12 @@ const slateNodeToHtml = (node: any, config = defaultConfig) => {
styleAttrs[cssProperty] = cssValue
}
})
attribs = {
...attribs,
style: styleToString(styleAttrs),

if (!isEmptyObject(styleAttrs)) {
attribs = {
...attribs,
style: styleToString(styleAttrs),
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export const styleToString = (style: { [key: string]: string }) => {
)
}

export const isEmptyObject = (obj: any) =>
obj && Object.keys(obj).length === 0 && Object.getPrototypeOf(obj) === Object.prototype

export const removeEmpty = (obj: {}): {} => {
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null))
}
Expand Down