dangerzone
v1.0.1
Published
Seamlessly use raw HTML in React without dangerouslySetInnerHTML.
Downloads
34
Readme
dangerzone
Seamlessly use raw HTML in React without
dangerouslySetInnerHTML
.
Install
npm install --save dangerzone
Examples
function WithHtmlLiteral () {
return <div>{dz`<em>Yeah!</em>`}</div>;
}
function WithHtmlVariable (props) {
return <div>{dz(props.html)}</div>;
}
Notes
- Dangerzone removes all
on*
attributes. - Dangerzone replaces
checked
attribute withdefaultChecked
. - Dangerzone replaces
value
attribute withdefaultValue
. - Dangerzone removes
<script>
tags.
API
dangerzone
Converts HTML to ReactNode
s array.
Arguments (if called as template string tag):
html : TemplateStringsArray
— template strings array....rest : any[]
— template values array.
Arguments (if called as regular function):
html : string
— input HTML.
Returns: ReactNode[]
— resulting ReactNode
s array.
Alias: dz
.
htmlToDom
Converts HTML to htmlparser2
's DOM.
Arguments:
html : string
— input HTML.
Returns: Dom
— resulting DOM.
Throws:
htmlparser2
's exception, if there is error while parsing HTML.'Unknown HTML parsing error: Neither DOM nor Error returned'
if neither DOM nor error returned by parser.
domToReact
Converts htmlparser2
's DOM to ReactNode
s array.
Arguments:
dom : Dom
—htmlparser2
's DOM.counter : IDzCounter = { val : 0 }
— counter object needed for addingkey
props to child nodes.
Returns: ReactNode[]
— resulting ReactNode
s array.
attribsToReact
Converts htmlparser2
's attributes to React's DOMAttributes
.
Arguments:
node : Tag | Style
—htmlparser2
'sTag
orStyle
node.counter : IDzCounter = { val : 0 }
— counter object needed for addingkey
props to child nodes.
Returns: DOMAttributes
— resulting React's DOMAttributes
object.
cssToCssom
Converts CSS to postcss
' nodes array.
Arguments:
css : string
— input CSS.
Returns: Declaration[]
— resulting Declaration
s array.
cssomToReact
Converts postcss
' declarations array to React's CSSProperties
.
Arguments:
nodes : Declaration[]
—postcss
' declarations array.
Returns: CSSProperties
— resulting React's style object.
IDzCounter
Special object needed for adding key
props to child React nodes.
val : number
— counter's value.
License
MIT