@@ -49,7 +49,11 @@ export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
4949 const assetName = splittedUrl [ splittedUrl ?. length - 1 ]
5050 return { type : 'reference' , attrs : { "asset-name" : assetName , "content-type-uid" : "sys_assets" , "asset-link" : el . getAttribute ( 'src' ) , "asset-type" : `image/${ imageType } ` , "display-type" : "display" , "type" : "asset" , "asset-uid" : assetUid } }
5151 }
52- return { type : 'img' , attrs : { url : el . getAttribute ( 'src' ) , width : el . getAttribute ( 'width' ) } }
52+ const imageAttrs : any = { type : 'img' , attrs : { url : el . getAttribute ( 'src' ) } }
53+ if ( el . getAttribute ( 'width' ) ) {
54+ imageAttrs . attrs [ 'width' ] = el . getAttribute ( 'width' )
55+ }
56+ return imageAttrs
5357 } ,
5458 LI : ( ) => ( { type : 'li' , attrs : { } } ) ,
5559 OL : ( ) => ( { type : 'ol' , attrs : { } } ) ,
@@ -631,12 +635,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
631635 const { href, target } = newChildren [ 0 ] . attrs ?. [ "redactor-attributes" ]
632636 extraAttrs [ 'anchorLink' ] = href ;
633637 if ( target && target !== '' ) {
634- extraAttrs [ 'target' ] = target ;
638+ extraAttrs [ 'target' ] = target === "_blank" ;
635639 }
636640 const imageAttrs = newChildren [ 0 ] . children ;
637641
638642 if ( imageAttrs [ 0 ] . type === 'img' ) {
639- sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
643+ if ( imageAttrs [ 0 ] . attrs . width ) {
644+ sizeAttrs . width = imageAttrs [ 0 ] . attrs . width
645+ }
640646 elementAttrs = getFinalImageAttributes ( { elementAttrs, newChildren : imageAttrs , extraAttrs, sizeAttrs} )
641647
642648 }
@@ -661,6 +667,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
661667 elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
662668 return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
663669 }
670+ if ( newChildren [ 0 ] ?. type === 'img' ) {
671+ let extraAttrs : { [ key : string ] : any } = { }
672+ const imageAttrs = newChildren [ 0 ]
673+ elementAttrs = getImageAttributes ( imageAttrs , imageAttrs . attrs || { } , extraAttrs )
674+ elementAttrs . attrs [ 'anchorLink' ] = el . getAttribute ( 'href' )
675+ if ( el . getAttribute ( 'target' ) )
676+ elementAttrs . attrs [ 'target' ] = el . getAttribute ( 'target' )
677+ return jsx ( 'element' , elementAttrs , [ { text : '' } ] )
678+
679+ }
664680 }
665681 if ( nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO' ) {
666682 if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. width ) {
@@ -676,7 +692,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
676692 if ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline ) {
677693 elementAttrs . attrs . inline = Boolean ( elementAttrs ?. attrs ?. [ "redactor-attributes" ] ?. inline )
678694 }
679- if ( elementAttrs . attrs . width ) {
695+ if ( nodeName === "IMG" && elementAttrs . attrs . width ) {
680696 elementAttrs . attrs . style . width = `${ elementAttrs . attrs . width } px`
681697 elementAttrs . attrs . style [ 'max-width' ] = `${ elementAttrs . attrs . width } px`
682698 }
@@ -952,11 +968,8 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
952968 delete childAttrs . caption
953969 }
954970
955- const imageAttrs = getImageAttributes ( elementAttrs , { ... childAttrs , ...extraAttrs } , extraAttrs ) ;
956- if ( imageAttrs . attrs . link ) {
957- imageAttrs . attrs . anchorLink = imageAttrs . attrs . link ;
958- delete imageAttrs . attrs . link ;
959- }
971+ const imageAttrs = getImageAttributes ( elementAttrs , childAttrs , extraAttrs ) ;
972+
960973 delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'anchorlink' ] ;
961974 delete imageAttrs ?. attrs ?. [ 'redactor-attributes' ] ?. [ 'style' ] ;
962975
0 commit comments