@@ -625,6 +625,68 @@ function buildSVGText(containerNode, str) {
625
625
return hasLink ;
626
626
}
627
627
628
+ /*
629
+ * sanitizeHTML: port of buildSVGText aimed at providing a clean subset of HTML
630
+ * @param {string } str: the html string to clean
631
+ * @returns {string }: a cleaned and normalized version of the input,
632
+ * supporting only a small subset of html
633
+ */
634
+ exports . sanitizeHTML = function sanitizeHTML ( str ) {
635
+ str = str . replace ( NEWLINES , ' ' ) ;
636
+
637
+ var rootNode = document . createElement ( 'p' ) ;
638
+ var currentNode = rootNode ;
639
+ var nodeStack = [ ] ;
640
+
641
+ var parts = str . split ( SPLIT_TAGS ) ;
642
+ for ( var i = 0 ; i < parts . length ; i ++ ) {
643
+ var parti = parts [ i ] ;
644
+ var match = parti . match ( ONE_TAG ) ;
645
+ var tagType = match && match [ 2 ] . toLowerCase ( ) ;
646
+
647
+ if ( tagType in TAG_STYLES ) {
648
+ if ( match [ 1 ] ) {
649
+ if ( nodeStack . length ) {
650
+ currentNode = nodeStack . pop ( ) ;
651
+ }
652
+ } else {
653
+ var extra = match [ 4 ] ;
654
+
655
+ var css = getQuotedMatch ( extra , STYLEMATCH ) ;
656
+ var nodeAttrs = css ? { style : css } : { } ;
657
+
658
+ if ( tagType === 'a' ) {
659
+ var href = getQuotedMatch ( extra , HREFMATCH ) ;
660
+
661
+ if ( href ) {
662
+ var dummyAnchor = document . createElement ( 'a' ) ;
663
+ dummyAnchor . href = href ;
664
+ if ( PROTOCOLS . indexOf ( dummyAnchor . protocol ) !== - 1 ) {
665
+ nodeAttrs . href = encodeURI ( decodeURI ( href ) ) ;
666
+ var target = getQuotedMatch ( extra , TARGETMATCH ) ;
667
+ if ( target ) {
668
+ nodeAttrs . target = target ;
669
+ }
670
+ }
671
+ }
672
+ }
673
+
674
+ var newNode = document . createElement ( tagType ) ;
675
+ currentNode . appendChild ( newNode ) ;
676
+ d3 . select ( newNode ) . attr ( nodeAttrs ) ;
677
+
678
+ currentNode = newNode ;
679
+ nodeStack . push ( newNode ) ;
680
+ }
681
+ } else {
682
+ currentNode . appendChild (
683
+ document . createTextNode ( convertEntities ( parti ) )
684
+ ) ;
685
+ }
686
+ }
687
+ return rootNode . innerHTML ;
688
+ } ;
689
+
628
690
exports . lineCount = function lineCount ( s ) {
629
691
return s . selectAll ( 'tspan.line' ) . size ( ) || 1 ;
630
692
} ;
0 commit comments