-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(domhandler): remove limit on mark transforms (#183)
* fix(domhandler): remove limit on mark transforms * style(convertslate): keep change request focussed * test(stylesmixedin): add test for conversion back to slate * test(stylesmixedin): rename tests * fix(packagejson): restore
- Loading branch information
1 parent
44accea
commit 3084324
Showing
4 changed files
with
91 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface Ifixture { | ||
name: string | ||
html: string | ||
slate: object[] | ||
} | ||
|
||
export const fixtures: Ifixture[] = [ | ||
{ | ||
name: 'p tag element transform', | ||
html: '<p><span style="background-color:#38761D;"><span style="color:#FE9900;"><span style="font-family:arial narrow;"><span style="font-size:20px;"><strong><u><i>some title!</i></u></strong></span></span></span></span></p>', | ||
slate: [ | ||
{ | ||
type: 'p', | ||
children: [ | ||
{ | ||
backgroundColor: '#38761D', | ||
color: '#FE9900', | ||
fontFamily: "arial narrow", | ||
fontSize: "20px", | ||
text: "some title!", | ||
bold: true, | ||
italic: true, | ||
underline: true, | ||
}, | ||
] | ||
} | ||
] | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { slateToHtml, htmlToSlateConfig, HtmlToSlateConfig, htmlToSlate } from '@slate-serializers/html' | ||
import { Element } from 'domhandler' | ||
import { SlateToDomConfig, slateToDomConfig } from '@slate-serializers/dom' | ||
import { transformStyleStringToObject } from '@slate-serializers/utilities' | ||
import { stylesMixedInFixtures as fixtures } from '../tests' | ||
|
||
export const slateToDomConfigStyleObject: SlateToDomConfig = { | ||
...slateToDomConfig, | ||
markTransforms: { | ||
...slateToDomConfig.markTransforms, | ||
backgroundColor: ({ node }) => { | ||
return new Element('span', { | ||
style: `background-color:${node.backgroundColor};` | ||
}) | ||
}, | ||
color: ({ node }) => { | ||
return new Element('span', { | ||
style: `color:${node.color};` | ||
}) | ||
}, | ||
fontFamily: ({ node }) => { | ||
return new Element('span', { | ||
style: `font-family:${node.fontFamily};` | ||
}) | ||
}, | ||
fontSize: ({ node }) => { | ||
return new Element('span', { | ||
style: `font-size:${node.fontSize};` | ||
}) | ||
} | ||
}, | ||
defaultTag: 'p', | ||
} | ||
|
||
export const htmlToSlateConfigStyleObject: HtmlToSlateConfig = { | ||
...htmlToSlateConfig, | ||
textTags: { | ||
...htmlToSlateConfig.textTags, | ||
span: (el) => { | ||
const style = el?.attribs && transformStyleStringToObject(el.attribs['style'] || ``) | ||
return { | ||
...(style && style) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
describe('styles as attributes on a leaf', () => { | ||
for (const fixture of fixtures) { | ||
it(`${fixture.name}`, () => { | ||
expect(slateToHtml(fixture.slate, slateToDomConfigStyleObject )).toEqual(fixture.html) | ||
expect(htmlToSlate(fixture.html, htmlToSlateConfigStyleObject)).toEqual(fixture.slate) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters