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

feat: add un-escape special symbols functionality #2426

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/test-examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Test2366 from './src/Test2366';
import Test2397 from './src/Test2397';
import Test2403 from './src/Test2403';
import Test2407 from './src/Test2407';
import Test2411 from './src/Test2411';

export default function App() {
return <ColorTest />;
Expand Down
12 changes: 12 additions & 0 deletions apps/test-examples/src/Test2411.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {SafeAreaView} from 'react-native';
import {SvgFromXml} from 'react-native-svg';

export default function App() {
return (
<SafeAreaView>
<SvgFromXml
xml={`<svg><text x="10" y="32" fontSize="32">&amp; &lt; &gt;</text></svg>`}
/>
</SafeAreaView>
);
}
13 changes: 10 additions & 3 deletions src/xml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ export function parse(source: string, middleware?: Middleware): JsxAST | null {
);
}

function decodeEntities(text: string): string {
return text
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
Comment on lines +277 to +279
Copy link
Member

@jakex7 jakex7 Aug 23, 2024

Choose a reason for hiding this comment

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

Making an exception for some character just seems wrong. What about &apos; or &quot;? There are dozens of characters that could be unescaped.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, you're right.

Comment on lines +277 to +279
Copy link
Member

Choose a reason for hiding this comment

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

Additionally, what about numeric character references? According to the spec, it's also a valid text content. Example:&lt; can be represented as &#60;, &gt; as &#62; and &amp; as &#38;

}

function metadata() {
while (
i + 1 < length &&
Expand All @@ -296,7 +303,7 @@ export function parse(source: string, middleware?: Middleware): JsxAST | null {
}

if (/\S/.test(text)) {
children.push(text);
children.push(decodeEntities(text)); // Decode entities before pushing text to children
}

if (source[i] === '<') {
Expand Down Expand Up @@ -391,7 +398,7 @@ export function parse(source: string, middleware?: Middleware): JsxAST | null {
error('expected ]]>');
}

children.push(source.slice(i + 7, index));
children.push(decodeEntities(source.slice(i + 7, index))); // Decode entities in CDATA section

i = index + 2;
return neutral;
Expand Down Expand Up @@ -457,7 +464,7 @@ export function parse(source: string, middleware?: Middleware): JsxAST | null {
i += 1;
allowSpaces();

value = getAttributeValue();
value = decodeEntities(getAttributeValue()); // Decode entities in attribute values
if (!isNaN(+value) && value.trim() !== '') {
value = +value;
}
Expand Down
Loading