tneotia/html-editor-enhanced

[QUESTION] how to handle relative url of images?

sumitbhanushali opened this issue · 6 comments

Type question here.

What is your use case? Do you have HTML as initialText with relative image URLs? Or do you expect the user to insert images with relative URLs?

initialText with relative image URLs

The best way would be to use the html package:

final doc = parse(htmlData);
doc.getElementsByTagName("img").forEach((element) {
   element.attributes['src'] = "prefix here" + element.attributes['src']!;
});
String newHtmlData = doc.outerHtml;

And use newHtmlData as initialText.

Thank you...
Actually there is a API in flutter_html package for handling relative urls.
I was thinking, whether we can map with that?

That is correct it does, however I cannot implement it in the same way in this package (and you cannot use it to help you outside of flutter_html).

I would have to use the code above since I never parse the HTML, it is handled by the webview. I would prefer not to add an extra dependency for a small feature, that's why I think its best the user handle those things on their own in initState or something similar.

Btw if you pass to flutter_html you just have to use the above once and you don't need the customImageRender API at all. Looks like this:

var doc;
String newHtmlData;

@override
initState() {
   doc = parse(htmlData);
   doc.getElementsByTagName("img").forEach((element) {
      element.attributes['src'] = "prefix here" + element.attributes['src']!;
   });
   newHtmlData = doc.outerHtml;
   super.initState();
}

@override
Widget build(BuildContext context) {
   return Column(
      children: [
         Html.fromDom(doc), //no need for customImageRender
         HtmlEditor(initialText: newHtmlData), 
       ]
   );
}

Understood, thanks a lot