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(*): lint and format #58

Merged
merged 1 commit into from
Apr 24, 2023
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
13 changes: 6 additions & 7 deletions __tests__/serializers/slateToHtml/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('slateToHtml expected behaviour', () => {
{
children: [
{
text: "2 > 1 but is < 3 & it can break HTML",
text: '2 > 1 but is < 3 & it can break HTML',
},
],
type: 'p',
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('slateToHtml expected behaviour', () => {
{
children: [
{
text: "2 > 1 but is < 3 & it can break HTML",
text: '2 > 1 but is < 3 & it can break HTML',
},
],
type: 'p',
Expand All @@ -70,14 +70,13 @@ describe('slateToHtml expected behaviour', () => {
type: 'p',
},
]
expect(slateToHtml(
slate,
{
expect(
slateToHtml(slate, {
...slateToDomConfig,
encodeEntities: false,
alwaysEncodeBreakingEntities: true,
}
)).toEqual(html)
}),
).toEqual(html)
})

it('does not encode HTML entities with the appropriate option', () => {
Expand Down
7 changes: 2 additions & 5 deletions src/serializers/htmlToSlate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ const deserialize = ({
const childrenContext = getContext(nodeName) || context

const isLastChild = index === childrenLength - 1
const isWithinTextNodes = (
currentEl.prev?.type === ElementType.Text
&& currentEl.next?.type === ElementType.Text
)
const isWithinTextNodes = currentEl.prev?.type === ElementType.Text && currentEl.next?.type === ElementType.Text

if (nodeName === 'br' && config.convertBrToLineBreak && context !== 'preserve') {
return [jsx('text', { text: context ? '\n' : '' }, [])]
Expand Down Expand Up @@ -124,7 +121,7 @@ const gatherTextMarkAttributes = ({ el, config = defaultConfig }: IgatherTextMar
let allAttrs = {}
const children = getChildren(el)
if (children.length) {
[el, ...children.flat()].forEach((child) => {
;[el, ...children.flat()].forEach((child) => {
const name = getName(child as Element)
const attrs = config.textTags[name] ? config.textTags[name](child as Element) : {}
allAttrs = {
Expand Down
5 changes: 4 additions & 1 deletion src/serializers/slateToHtml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const slateToDom: SlateToDom = (node: any[], config = defaultConfig) => {

const slateNodeToHtml = (node: any, config = defaultConfig, isLastNodeInDocument = false) => {
if (SlateText.isText(node)) {
const str = (config.alwaysEncodeBreakingEntities && config.encodeEntities === false) ? encodeBreakingEntities(node.text) : node.text
const str =
config.alwaysEncodeBreakingEntities && config.encodeEntities === false
? encodeBreakingEntities(node.text)
: node.text

// convert line breaks to br tags
const strLines = config.convertLineBreakToBr ? str.split('\n') : [str]
Expand Down
21 changes: 11 additions & 10 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ export const getNested = (obj: any, ...args: string[]) => {
}

export const encodeBreakingEntities = (str: string) => {
const swapChar = (charToSwap: string) => { // that swaps characters to HTML entities
switch(charToSwap){
case "&":
return "&amp;"
case "<":
return "&lt;"
case ">":
return "&gt;"
const swapChar = (charToSwap: string) => {
// that swaps characters to HTML entities
switch (charToSwap) {
case '&':
return '&amp;'
case '<':
return '&lt;'
case '>':
return '&gt;'
default:
return charToSwap
}
}
str = str.replace(/[&<>]/g, function(match){
str = str.replace(/[&<>]/g, (match) => {
return swapChar(match)
})

return str
}
}