Skip to content

Conversation

hnvn
Copy link

@hnvn hnvn commented May 20, 2020

Currently, CustomRender returns a Widget, it makes the application can't manipulate the alignment of the WidgetSpan which layout of custom tag (it's required in some cases, for example: align an inline image)

My pull request will modify CustomRender, it will return a dynamic type instead of Widget, so the application can return WidgetSpan instead of Widget. In case of returning WidgetSpan, html_parser will return the customRender directly instead of wrapping it inside a WidgetSpan:

...
typedef CustomRender = dynamic Function(RenderContext context,
    Widget parsedChild, Map<String, String> attributes, dom.Element element);
...

InlineSpan parseTree(RenderContext context, StyledElement tree) {
   ...
   if (customRender?.containsKey(tree.name) ?? false) {
      final render = customRender[tree.name].call(
        newContext,
        ContainerSpan(
          newContext: newContext,
          style: tree.style,
          shrinkWrap: context.parser.shrinkWrap,
          children: tree.children
                  ?.map((tree) => parseTree(newContext, tree))
                  ?.toList() ??
              [],
        ),
        tree.attributes,
        tree.element,
      );
      return render is InlineSpan
          ? render
          : WidgetSpan(
              child: ContainerSpan(
                newContext: newContext,
                style: tree.style,
                shrinkWrap: context.parser.shrinkWrap,
                child: render,
              ),
            );
    }
   ...
}

With this approach, the application can freely style the WidgetSpan of custom tag.

@erickok
Copy link
Contributor

erickok commented Jul 8, 2020

Nice job. Looks good. Can you add any concrete example of a typical use?

@hnvn
Copy link
Author

hnvn commented Jul 9, 2020

For example, I want to add a small inline image between text content, laTeX image in my case, these images should be aligned with text content in baseline instead of center by default.

@DFelten
Copy link
Contributor

DFelten commented Aug 1, 2020

What's the status of this merge request? This would be great.

Copy link
Contributor

@erickok erickok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this and it is working well. Unfortunately it is also coming with a bunch of unrelated changes and it has a merge conflict with #384 , When that latter PR is merged I'd like to add this PR's code too.

erickok added a commit to vrtdev/flutter_html that referenced this pull request Nov 20, 2020
erickok added a commit to vrtdev/flutter_html that referenced this pull request Jan 15, 2021
Same as Sub6Resources#298 but only the minimal required changes to make this work
@erickok
Copy link
Contributor

erickok commented Jan 15, 2021

I re-implemented this in #498 with just the minimal changes required to allow InlineSpans as customerRender.

@hnvn
Copy link
Author

hnvn commented Jan 15, 2021

As @erickok 's comment, this PR can be closed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants