@@ -700,3 +700,146 @@ it("should convert colspan and rowspan to colSpan and rowSpan in table rendering
700700 expect ( screen . getByTestId ( "cell1" ) . element ( ) ) . toHaveAttribute ( "colSpan" , "2" ) ;
701701 expect ( screen . getByTestId ( "cell2" ) . element ( ) ) . toHaveAttribute ( "rowSpan" , "2" ) ;
702702} ) ;
703+
704+ it ( "should strip all inline styles when stripStyles is true" , ( ) => {
705+ const screen = render (
706+ < UmbracoRichText
707+ data = { {
708+ tag : "#root" ,
709+ elements : [
710+ {
711+ tag : "div" ,
712+ attributes : { style : "color: red; font-size: 16px;" } ,
713+ elements : [
714+ {
715+ tag : "p" ,
716+ attributes : { style : "margin: 10px; padding: 5px;" } ,
717+ elements : [ { tag : "#text" , text : "Text with stripped styles" } ] ,
718+ } ,
719+ ] ,
720+ } ,
721+ ] ,
722+ } }
723+ stripStyles = { true }
724+ /> ,
725+ ) ;
726+
727+ const div = screen . container . querySelector ( "div" ) ;
728+ const p = screen . container . querySelector ( "p" ) ;
729+
730+ expect ( div ) . not . toHaveAttribute ( "style" ) ;
731+ expect ( p ) . not . toHaveAttribute ( "style" ) ;
732+ } ) ;
733+
734+ it ( "should strip styles only from specified tags" , ( ) => {
735+ const screen = render (
736+ < UmbracoRichText
737+ data = { {
738+ tag : "#root" ,
739+ elements : [
740+ {
741+ tag : "div" ,
742+ attributes : { style : "color: red; font-size: 16px;" } ,
743+ elements : [
744+ {
745+ tag : "p" ,
746+ attributes : { style : "margin: 10px; padding: 5px;" } ,
747+ elements : [
748+ { tag : "#text" , text : "Text with partially stripped styles" } ,
749+ ] ,
750+ } ,
751+ ] ,
752+ } ,
753+ ] ,
754+ } }
755+ stripStyles = { { tags : [ "p" ] } }
756+ /> ,
757+ ) ;
758+
759+ const div = screen . container . querySelector ( "div" ) ;
760+ const p = screen . container . querySelector ( "p" ) ;
761+
762+ expect ( div ) . toHaveAttribute ( "style" , "color: red; font-size: 16px;" ) ;
763+ expect ( p ) . not . toHaveAttribute ( "style" ) ;
764+ } ) ;
765+
766+ it ( "should preserve styles for excepted tags" , ( ) => {
767+ const screen = render (
768+ < UmbracoRichText
769+ data = { {
770+ tag : "#root" ,
771+ elements : [
772+ {
773+ tag : "div" ,
774+ attributes : { style : "color: red; font-size: 16px;" } ,
775+ elements : [
776+ {
777+ tag : "p" ,
778+ attributes : { style : "margin: 10px; padding: 5px;" } ,
779+ elements : [ { tag : "#text" , text : "Text with excepted styles" } ] ,
780+ } ,
781+ {
782+ tag : "span" ,
783+ attributes : { style : "font-weight: bold;" } ,
784+ elements : [ { tag : "#text" , text : "Bold text" } ] ,
785+ } ,
786+ ] ,
787+ } ,
788+ ] ,
789+ } }
790+ stripStyles = { { except : [ "p" ] } }
791+ /> ,
792+ ) ;
793+
794+ const div = screen . container . querySelector ( "div" ) ;
795+ const p = screen . container . querySelector ( "p" ) ;
796+ const span = screen . container . querySelector ( "span" ) ;
797+
798+ expect ( div ) . not . toHaveAttribute ( "style" ) ;
799+ expect ( p ) . toHaveAttribute ( "style" , "margin: 10px; padding: 5px;" ) ;
800+ expect ( span ) . not . toHaveAttribute ( "style" ) ;
801+ } ) ;
802+
803+ it ( "should combine tags and except properties correctly" , ( ) => {
804+ const screen = render (
805+ < UmbracoRichText
806+ data = { {
807+ tag : "#root" ,
808+ elements : [
809+ {
810+ tag : "div" ,
811+ attributes : { style : "color: red;" } ,
812+ elements : [
813+ {
814+ tag : "p" ,
815+ attributes : { style : "margin: 10px;" } ,
816+ elements : [ { tag : "#text" , text : "Paragraph" } ] ,
817+ } ,
818+ {
819+ tag : "span" ,
820+ attributes : { style : "font-weight: bold;" } ,
821+ elements : [ { tag : "#text" , text : "Span" } ] ,
822+ } ,
823+ {
824+ tag : "h1" ,
825+ attributes : { style : "font-size: 24px;" } ,
826+ elements : [ { tag : "#text" , text : "Heading" } ] ,
827+ } ,
828+ ] ,
829+ } ,
830+ ] ,
831+ } }
832+ stripStyles = { { tags : [ "div" , "p" , "span" ] , except : [ "span" ] } }
833+ /> ,
834+ ) ;
835+
836+ const div = screen . container . querySelector ( "div" ) ;
837+ const p = screen . container . querySelector ( "p" ) ;
838+ const span = screen . container . querySelector ( "span" ) ;
839+ const h1 = screen . container . querySelector ( "h1" ) ;
840+
841+ expect ( div ) . not . toHaveAttribute ( "style" ) ;
842+ expect ( p ) . not . toHaveAttribute ( "style" ) ;
843+ expect ( span ) . toHaveAttribute ( "style" , "font-weight: bold;" ) ;
844+ expect ( h1 ) . toHaveAttribute ( "style" , "font-size: 24px;" ) ;
845+ } ) ;
0 commit comments