Skip to content

Commit

Permalink
fix: parsing < inside comments (#2277)
Browse files Browse the repository at this point in the history
# Summary

When comment occurred before first tag, there was a possible parsing
error if it contains `<` character.
Here is an example (line 2 and 3 would cause separate errors):

```xml
<!-- sample rectangle -->
<!-- <sample rectangle -->
<!-- <sample> rectangle -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect width="100" height="100" x="50" y="50" fill="red" />
</svg>
```

Fixes #2276

## Test Plan

Manual tests in `TestsExample` app. (Test2276)
  • Loading branch information
jakex7 committed May 16, 2024
1 parent 0db8703 commit c0e5e58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Test2089 from './src/Test2089';
import Test2196 from './src/Test2196';
import Test2266 from './src/Test2266';
import Test1986 from './src/Test1986';
import Test2276 from './src/Test2276';

export default function App() {
return <ColorTest />;
Expand Down
20 changes: 20 additions & 0 deletions TestsExample/src/Test2276.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import {View} from 'react-native';
import {SvgXml} from 'react-native-svg';

const xml = `<!-- sample rectangle -->
<!-- <sample rectangle -->
<!-- <sample> rectangle -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" x="50" y="50" fill="red" />
<text fill="black" x="100" y="100">test</text>
</svg>
`;

export default () => {
return (
<View style={{marginTop: 100}}>
<SvgXml xml={xml} />
</View>
);
};
7 changes: 6 additions & 1 deletion src/xml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ function locate(source: string, i: number) {
}

const validNameCharacters = /[a-zA-Z0-9:_-]/;
const commentStart = /<!--/;
const whitespace = /[\s\t\r\n]/;
const quotemarks = /['"]/;

Expand All @@ -315,7 +316,11 @@ export function parse(source: string, middleware?: Middleware): JsxAST | null {
function metadata() {
while (
i + 1 < length &&
(source[i] !== '<' || !validNameCharacters.test(source[i + 1]))
(source[i] !== '<' ||
!(
validNameCharacters.test(source[i + 1]) ||
commentStart.test(source.slice(i, i + 4))
))
) {
i++;
}
Expand Down

0 comments on commit c0e5e58

Please sign in to comment.