Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] how to handle relative url of images? #50

Closed
sumitbhanushali opened this issue May 22, 2021 · 6 comments
Closed

[QUESTION] how to handle relative url of images? #50

sumitbhanushali opened this issue May 22, 2021 · 6 comments
Labels
question Further information is requested

Comments

@sumitbhanushali
Copy link

Type question here.

@sumitbhanushali sumitbhanushali added the question Further information is requested label May 22, 2021
@tneotia
Copy link
Owner

tneotia commented May 22, 2021

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?

@sumitbhanushali
Copy link
Author

initialText with relative image URLs

@tneotia
Copy link
Owner

tneotia commented May 22, 2021

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.

@sumitbhanushali
Copy link
Author

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

@tneotia
Copy link
Owner

tneotia commented May 22, 2021

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), 
       ]
   );
}

@sumitbhanushali
Copy link
Author

Understood, thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants