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

SVG from XML: inlineSize in <tspan ...> is ignored because xml parser wraps TSpan in TSpan #2206

Closed
sschulz-t opened this issue Jan 11, 2024 · 1 comment
Labels
Close when stale This issue is going to be closed when there is no activity for a while Missing repro This issue need minimum repro scenario

Comments

@sschulz-t
Copy link

Bug

When creating a SVG from xml via:

let xml = `
    <?xml version="1.0" encoding="UTF-8"?>
    <svg viewBox="0 0 800 505">
      <text x="111" y="110" font-size="40">
        <tspan inline-size="50">hello world world world world</tspan>
      </text>
    </svg>`;

    let svg = <SVG.SvgXml xml={xml} />;

The tspan attribute inline-size is ignored.

When using JSX, the linebreak works :

let svg = 
<SVG.Svg viewBox="0 0 800 505">
  <SVG.Text x="110" y="110" fontSize="40">
    <SVG.TSpan inlineSize="50">hello world world world world</SVG.TSpan>
  </SVG.Text>
</SV.Svg>;

I traced it back to

const textChildren =
typeof children === 'string' || typeof children === 'number' ? (
container ? (
<TSpan>{String(children)}</TSpan>
) : null
) : Children.count(children) > 1 || Array.isArray(children) ? (
Children.map(children, getChild)
) : (
children
);

...
function getChild(child: TextChild) {
if (typeof child === 'string' || typeof child === 'number') {
return <TSpan>{String(child)}</TSpan>;
} else {
return child;
}
}

which wraps text components in a new TSpan, which does not forward the inlineSize.

My workaround is the following diff:

diff --color -r node_modules/react-native-svg/src/lib/extract/extractText.tsx /tmp/react-native-svg/src/lib/extract/extractText.tsx
139c139
< function getChild(child: TextChild) {
---
> function getChild(child: TextChild, inlineSize: NumberProp) {
141c141
<     return <TSpan>{String(child)}</TSpan>;
---
>     return <TSpan inlineSize={inlineSize}>{String(child)}</TSpan>;
177c177
<         <TSpan>{String(children)}</TSpan>
---
>         getChild(children, inlineSize)
180c180
<       Children.map(children, getChild)
---
>       Children.map(children, (child) => getChild(child, inlineSize))

I would probably make sense to detect that the currently processed item is already a TSpan and not to wrap it in another TSpan.

@bohdanprog
Copy link
Member

Hello @sschulz-t,
Do you still have that issue?
I couldn't reproduce that. If you still have that issue, please share the example to reproduce that.
Thank you.

@bohdanprog bohdanprog added Missing repro This issue need minimum repro scenario semantic-release Close when stale This issue is going to be closed when there is no activity for a while maybe fixed and removed semantic-release maybe fixed labels Jun 13, 2024
@github-actions github-actions bot closed this as completed Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Close when stale This issue is going to be closed when there is no activity for a while Missing repro This issue need minimum repro scenario
Projects
None yet
Development

No branches or pull requests

2 participants