Skip to content

Commit

Permalink
fix(slatetohtml): do not add an empty style attribute (thompsonsj#20)
Browse files Browse the repository at this point in the history
* fix(slatetohtml): do not add an empty style attribute

* style(utilities): format
  • Loading branch information
thompsonsj authored Dec 6, 2022
1 parent a35597f commit a303806
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
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

0 comments on commit a303806

Please sign in to comment.