-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add v2 version for renderToJSON method (#65)
* chore: add v2 version for renderToJSON method * chore: bump version
- Loading branch information
1 parent
7894e20
commit 338135b
Showing
6 changed files
with
155 additions
and
69 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
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,75 @@ | ||
import React from 'react'; | ||
import ReactReconciler from 'react-reconciler'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
|
||
import { noop, escapeTextForBrowser, trimContent } from './render-utils'; | ||
|
||
const reconciler = ReactReconciler({ | ||
supportsMutation: true, | ||
createTextInstance(text) { | ||
return escapeTextForBrowser(text); | ||
}, | ||
createInstance(type, props) { | ||
if (!type.startsWith('mj')) { | ||
return { isReact: true, type, props }; | ||
} | ||
|
||
const { children, dangerouslySetInnerHTML, ...rest } = props; | ||
|
||
const res = { | ||
tagName: type, | ||
attributes: rest, | ||
}; | ||
|
||
Object.keys(res.attributes).forEach((key) => { | ||
const attrKey = res.attributes[key]; | ||
if (attrKey === undefined) { | ||
delete res.attributes[key]; | ||
} | ||
if (typeof attrKey === 'string') { | ||
res.attributes[key] = escapeTextForBrowser(attrKey); | ||
} | ||
}); | ||
|
||
if (props.dangerouslySetInnerHTML && props.dangerouslySetInnerHTML.__html) { | ||
// using replace to prevent issue with $ sign in MJML | ||
// https://github.com/mjmlio/mjml2json#L145 | ||
res.content = props.dangerouslySetInnerHTML.__html.replace('$', '$'); | ||
} | ||
|
||
return res; | ||
}, | ||
appendChildToContainer(container, child) { | ||
trimContent(child); | ||
container.result = child; | ||
}, | ||
appendInitialChild(parent, child) { | ||
if (typeof parent === 'string' || parent.isReact) { | ||
return; | ||
} | ||
if (typeof child === 'string') { | ||
parent.content = (parent.content || '') + child; | ||
} else if (child.isReact) { | ||
const content = ReactDOMServer.renderToStaticMarkup( | ||
React.createElement(child.type, child.props), | ||
); | ||
parent.content = (parent.content || '') + content; | ||
} else { | ||
parent.children = (parent.children || []).concat(child); | ||
} | ||
}, | ||
prepareForCommit: noop, | ||
resetAfterCommit: noop, | ||
clearContainer: noop, | ||
appendChild: noop, | ||
finalizeInitialChildren: noop, | ||
getChildHostContext: noop, | ||
getRootHostContext: noop, | ||
shouldSetTextContent: noop, | ||
}); | ||
|
||
export function renderToJSON2(whatToRender) { | ||
const container = reconciler.createContainer({}, false, false); | ||
reconciler.updateContainer(whatToRender, container, null, null); | ||
return container.containerInfo.result; | ||
} |
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,66 @@ | ||
const matchHtmlRegExp = /["'&<>$]/; | ||
|
||
export function escapeHtml(string) { | ||
const str = '' + string; | ||
const match = matchHtmlRegExp.exec(str); | ||
|
||
if (!match) { | ||
return str; | ||
} | ||
|
||
let escape; | ||
let html = ''; | ||
let index; | ||
let lastIndex = 0; | ||
|
||
for (index = match.index; index < str.length; index++) { | ||
switch (str.charCodeAt(index)) { | ||
case 34: // " | ||
escape = '"'; | ||
break; | ||
case 36: // $ | ||
escape = '$'; | ||
break; | ||
case 38: // & | ||
escape = '&'; | ||
break; | ||
case 39: // ' | ||
escape = '''; // modified from escape-html; used to be ''' | ||
break; | ||
case 60: // < | ||
escape = '<'; | ||
break; | ||
case 62: // > | ||
escape = '>'; | ||
break; | ||
default: | ||
continue; | ||
} | ||
|
||
if (lastIndex !== index) { | ||
html += str.substring(lastIndex, index); | ||
} | ||
|
||
lastIndex = index + 1; | ||
html += escape; | ||
} | ||
|
||
return lastIndex !== index ? html + str.substring(lastIndex, index) : html; | ||
} | ||
|
||
export function escapeTextForBrowser(text) { | ||
if (typeof text === 'boolean' || typeof text === 'number') { | ||
return '' + text; | ||
} | ||
return escapeHtml(text); | ||
} | ||
|
||
export function noop() {} | ||
|
||
export function trimContent(child) { | ||
if (child.content) { | ||
child.content = child.content.trim(); | ||
} else if (child.children) { | ||
child.children.forEach(trimContent); | ||
} | ||
} |
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