fix: parsing < inside comments (#2277)

# 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)
This commit is contained in:
Jakub Grzywacz
2024-05-16 12:46:24 +02:00
committed by GitHub
parent 0db87030da
commit c0e5e58f9a
3 changed files with 27 additions and 1 deletions
+1
View File
@@ -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 />;
+20
View File
@@ -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>
);
};
+6 -1
View File
@@ -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 = /['"]/;
@@ -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++;
}