Skip to content

Fix rendering of foreignObject in SVGs#32417

Merged
captainsafia merged 3 commits into
mainfrom
safia/svg-fix
May 5, 2021
Merged

Fix rendering of foreignObject in SVGs#32417
captainsafia merged 3 commits into
mainfrom
safia/svg-fix

Conversation

@captainsafia
Copy link
Copy Markdown
Contributor

@captainsafia captainsafia commented May 5, 2021

Resolves #30973.

SVG supports a foreignObject element that can be used to display arbitrary HTML within an SVG. Particularly for Blazor, this HTML can represent a render fragment or Blazor component, like so:

<svg id="svg-with-two-way-binding">
    <foreignObject width="200" height="200">
        <div>
            <input @bind="value" @bind:event="oninput" type="number"/> Two-way binding
        </div>
    </foreignObject>
</svg>

Currently, our client-side rendering logic renders the SVG element and all its children under the SVG namespace by invoking document.createElementNS('http://www.w3.org/2000/svg', tagName). However, for the browser to correctly render the HTML content under the foreignObject it must be rendered under an XHTML namespace.

To resolve this, we add an additional check to set the namespace to XHTML for elements that are children of a foreignObject.

Previous:

- svg (SVG namespace)
  - foreignObject (SVG namespace)
    - div (SVG namespace)
      - input (SVG namespace)

Current:

- svg (SVG namespace)
  - foreignObject (SVG namespace)
    - div (XHTML namespace)
      - input (XHTML namespace)

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label May 5, 2021
Comment thread src/Components/Web.JS/src/Rendering/BrowserRenderer.ts Outdated
@captainsafia captainsafia marked this pull request as ready for review May 5, 2021 14:59
@captainsafia captainsafia requested a review from a team as a code owner May 5, 2021 14:59
Copy link
Copy Markdown
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Looks great!

@captainsafia captainsafia enabled auto-merge (squash) May 5, 2021 16:52
// browsers will fail to render the foreign object content. Here, we ensure that if
// we encounter a `foreignObject` in the SVG, then all its children will be placed
// under the XHTML namespace.
const isSvg = (tagName === 'svg' || isSvgElement(parent)) && !isForeignObject(parent);
Copy link
Copy Markdown
Member

@SteveSandersonMS SteveSandersonMS May 5, 2021

Choose a reason for hiding this comment

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

Note that this isn't the only place where we check isSvgElement(parent). We also do that in insertMarkup to vary how we parse markup strings. My guess is that we'd also need to update insertMarkup in the same way as this in case we're inserting a markup block as the top-level thing inside a foreign element. Does that sound correct to you?

If this is all true, it suggests to me that instead of having both isSvgElement and isForeignObject as separate things we have to remember to use together, we should instead replace them both with a single shouldInsertImmediateChildrenAsSvg function. This would also have an efficiency benefit, since that function would only need to evaluate getClosestDomElement(element) once instead of two methods both evaluating that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great catch!

Yep -- it makes sense to refactor things a bit with an efficiency focus if these check happens more than once.

Will update PR accordingly.

@captainsafia captainsafia disabled auto-merge May 5, 2021 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix rendering Blazor elements inside SVG

5 participants