@@ -570,6 +570,49 @@ const localized_isodate = (date) => {
570
570
return `${ year } -${ month } -${ day } ` ;
571
571
} ;
572
572
573
+ /**
574
+ * Replace HTML reserved characters with html entities to add HTML for user
575
+ * editing to e.g. a textarea or a contenteditable.
576
+ *
577
+ * See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
578
+ *
579
+ * @param {string } html - The HTML string to encode.
580
+ *
581
+ * @returns {string } - Returns the escaped html string:
582
+ * ``&`` will be replaced with ``&``.
583
+ * ``<`` will be repalced with ``<``,
584
+ * ``>`` will be replaced with ``>``,
585
+ * ``"`` will be replaced with ``"``.
586
+ */
587
+ const escape_html = ( html ) => {
588
+ return ( html || "" )
589
+ . replace ( / & / g, "&" ) // needs to be first!
590
+ . replace ( / < / g, "<" )
591
+ . replace ( / > / g, ">" )
592
+ . replace ( / " / g, """ ) ;
593
+ } ;
594
+
595
+ /**
596
+ * Return unescaped, raw HTML from an escaped HTML string.
597
+ *
598
+ * See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
599
+ *
600
+ * @param {string } escaped_html - The HTML string to decode.
601
+ *
602
+ * @returns {string } - Returns the escaped html string:
603
+ * ``&`` will be replaced with ``&``,
604
+ * ``<`` will be repalced with ``<``,
605
+ * ``>`` will be replaced with ``>``,
606
+ * ``"`` will be replaced with ``"``.
607
+ */
608
+ const unescape_html = ( escaped_html ) => {
609
+ return ( escaped_html || "" )
610
+ . replace ( / & a m p ; / g, "&" )
611
+ . replace ( / & l t ; / g, "<" )
612
+ . replace ( / & g t ; / g, ">" )
613
+ . replace ( / & q u o t ; / g, '"' ) ;
614
+ } ;
615
+
573
616
var utils = {
574
617
// pattern pimping - own module?
575
618
jqueryPlugin : jqueryPlugin ,
@@ -599,6 +642,8 @@ var utils = {
599
642
jqToNode : jqToNode ,
600
643
ensureArray : ensureArray ,
601
644
localized_isodate : localized_isodate ,
645
+ escape_html : escape_html ,
646
+ unescape_html : unescape_html ,
602
647
} ;
603
648
604
649
export default utils ;
0 commit comments