@@ -39,6 +39,7 @@ Snap.plugin(function (Snap, Element, Paper, global) {
3939 // TODO: better than checking children.length and a blacklist would be to refer to
4040 // Graphic Elements vs. Container Elements (see https://www.w3.org/TR/SVG/struct.html#TermContainerElement)
4141 // image is excluded here as <image ...>\n</image> has a child of type #text (== '\n')
42+ // text is excluded here as <tspan> cannot be rendered without a <text> parent
4243 const goRecursive = ! [
4344 "image" ,
4445 "defs" ,
@@ -47,6 +48,7 @@ Snap.plugin(function (Snap, Element, Paper, global) {
4748 "rdf:rdf" ,
4849 "cc:work" ,
4950 "sodipodi:namedview" ,
51+ "text" ,
5052 ] . includes ( elem . type ) ;
5153
5254 if ( children . length > 0 && goRecursive ) {
@@ -57,43 +59,15 @@ Snap.plugin(function (Snap, Element, Paper, global) {
5759 ) ;
5860 }
5961 } else {
60- if ( elem . type === "g" ) return [ ] ; // means empty group
61- if ( elem . type === "defs" ) return [ ] ; // means empty defs
62- if (
63- elem . type === "image" ||
64- elem . type === "text" ||
65- elem . type === "#text"
66- ) {
67- if ( elem . type === "#text" ) {
68- if ( elem . node . nodeValue . trim ( ) !== "" ) {
69- let parent = elem . parent ( ) ;
70- if ( parent . type === "textPath" ) {
71- parent = parent . parent ( ) ;
72- }
73- parent . addClass ( className ) ;
74- selection . push ( parent ) ;
75- }
76- } else if ( elem . type === "text" ) {
77- if ( elem . node . nodeValue !== null ) {
78- elem . addClass ( className ) ;
79- selection . push ( elem ) ;
80- }
81- } else if ( elem . type === "image" ) {
82- elem . addClass ( className ) ;
83- selection . push ( elem ) ;
84- }
62+ let processedElem = processElementByType (
63+ elem ,
64+ className ,
65+ fillPaths
66+ ) ;
67+ if ( Array . isArray ( processedElem ) && processedElem . length === 0 ) {
68+ return [ ] ;
8569 } else {
86- // check for non-dimensional elements and out of working area elements
87- const bb = elem . getBBox ( ) ;
88- if ( bb . w === 0 || bb . h === 0 ) {
89- console . warn ( `Element did not have expanse: ${ elem . type } ` ) ;
90- return [ ] ;
91- }
92-
93- if ( fillPaths && elem . is_filled ( ) ) {
94- elem . addClass ( className ) ;
95- selection . push ( elem ) ;
96- }
70+ selection . push ( processedElem ) ;
9771 }
9872 }
9973 return selection ;
@@ -343,9 +317,8 @@ Snap.plugin(function (Snap, Element, Paper, global) {
343317
344318 elem . embedAllImages ( ) ;
345319 const fontSet = elem . getUsedFonts ( ) ;
346- const fontDeclarations = WorkingAreaHelper . getFontDeclarations (
347- fontSet
348- ) ;
320+ const fontDeclarations =
321+ WorkingAreaHelper . getFontDeclarations ( fontSet ) ;
349322
350323 let bboxMargin = 0 ;
351324 if ( margin === null ) {
@@ -700,4 +673,48 @@ Snap.plugin(function (Snap, Element, Paper, global) {
700673 else return Math . floor ( bytes / ( 1024 * 1024 ) ) + " MByte" ;
701674 }
702675 }
676+
677+ function processElementByType ( elem , className , fillPaths ) {
678+ if ( elem . type === "g" ) return [ ] ; // means empty group
679+ if ( elem . type === "defs" ) return [ ] ; // means empty defs
680+ // TODO: SW-1446
681+ if ( elem . type === "#text" ) {
682+ if ( elem . node . nodeValue . trim ( ) !== "" ) {
683+ let parent = elem . parent ( ) ;
684+ if ( parent . type === "textPath" ) {
685+ parent = parent . parent ( ) ;
686+ }
687+ parent . addClass ( className ) ;
688+ return parent ;
689+ }
690+ } else if ( elem . type === "text" ) {
691+ // check if <tspan> elements exist in <text> and if they contain any text
692+ const nonEmptyTspan = ( child ) =>
693+ child . nodeName === "tspan" && child . textContent !== null ;
694+ // use node.textContent instead of node.nodeValue as nodeValue returns null regardless of valid fillings
695+ if (
696+ elem . node . textContent !== null ||
697+ Object . values ( elem . node . childNodes ) . some ( nonEmptyTspan )
698+ ) {
699+ elem . addClass ( className ) ;
700+ return elem ;
701+ }
702+ } else if ( elem . type === "image" ) {
703+ elem . addClass ( className ) ;
704+ return elem ;
705+ } else {
706+ // check for non-dimensional elements and out of working area elements
707+ const bb = elem . getBBox ( ) ;
708+ if ( bb . w === 0 || bb . h === 0 ) {
709+ console . warn ( `Element did not have expanse: ${ elem . type } ` ) ;
710+ return [ ] ;
711+ }
712+
713+ if ( fillPaths && elem . is_filled ( ) ) {
714+ elem . addClass ( className ) ;
715+ return elem ;
716+ }
717+ }
718+ return [ ] ;
719+ }
703720} ) ;
0 commit comments