react-native-htmlview icon indicating copy to clipboard operation
react-native-htmlview copied to clipboard

Displays images with incorrect aspect ratio: <p><img..../></p>

Open AdwardNguyen opened this issue 6 years ago • 9 comments

I have a problem when I want render data from API by html resource Example data <p> demo data </p><img src='https:/abc.com/up/image1.jpg'.. /><p><img src='https:/abc.com/up/image2.jpg'.. /> </p>

Image 1: working fine Image 2: not working fine because inline paragraph tag. display with incorrect height item

Please help me solve this problem! Thank you!

screen shot 2018-10-29 at 09 41 38 am

AdwardNguyen avatar Oct 29 '18 08:10 AdwardNguyen

Source code(in htmlToElement.js Eighty-eighth lines):

if (node.name === 'img') { return <Img key={index} attribs={node.attribs} />; }

Change to:

if (node.children && node.children.length > 0 && node.children[0].name === 'img') { return <Img key={index} attribs={node.children[0].attribs} />; }

thirteenthEyes avatar Nov 08 '18 07:11 thirteenthEyes

I hava the same issue in android.Have you solved it?

nongh4704 avatar Jan 26 '19 03:01 nongh4704

I solved this issue from the backend by removing any p tags that surround images (here is the expression in PHP): preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $text);

djarrin avatar Mar 07 '19 16:03 djarrin

I have this problem !

<p class="xxxxxx"><img src="...."></p> => <Text><Image source="" /></Text>

It seems that the problem comes with the attributes of TEXT for example the line heigth margin or padding

resolve

I solved it by eliminating the

inside the image.

so react compile:

stringHtml.replace(/<\s*(p+).*?>/g,'<$1>').replace(/<(\/)?p>/g , '')

herel avatar Mar 07 '19 23:03 herel

Has anyone found a way to solve this without stripping out p tags?

Reidweb1 avatar May 10 '19 21:05 Reidweb1

Any update here?

rochapablo avatar Jul 01 '19 15:07 rochapablo

I use another repository react-native-render-html

nongh4704 avatar Jul 15 '19 05:07 nongh4704

@herel for some reason after applying your "patch" my styles:

const htmlStyles = StyleSheet.create({
  p: {
    fontSize: 15,
    color: textColor,
    textAlign: 'justify',
  },
});

do not work. What is your approach here?

atais avatar Dec 15 '20 18:12 atais

@AdwardNguyen Please can you share the exact html string you're trying to display.

I had aspect ratio problems too and I solved it like this:

if (node.name === 'img') {
    const img = node.attribs
    const aspectRatio = Number(img.width) / Number(img.height)
    return (
      <TouchableOpacity key={index} onPress={handleChangeImageZoomViewerState}>
        <Image
          source={{ uri: img.src }}
          style={[styles.img, { aspectRatio }]}
          resizeMode="cover"
        />
      </TouchableOpacity>
    )
  }

anthlasserre avatar Jan 30 '21 22:01 anthlasserre