Skip to content

Commit c453bab

Browse files
authored
Merge pull request #8 from leodr/fix/jsx-with-only-html-tags
Detect JSX by searching for className attributes
2 parents 3c1f2f3 + 6cac918 commit c453bab

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

UNRELEASED.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
Keep track of any changes you make here, so they can just be copied into the
44
changelog after releasing them.
5+
6+
### Fixed
7+
8+
- Detect that string is JSX if it contains a `className`-attribute

src/isHTML.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,42 @@ test("should match long HTML snippet", () => {
356356

357357
expect(isHTML(text)).toBe(true);
358358
});
359+
360+
test("should not match JSX that contains className attribute", () => {
361+
const text = html`
362+
<div className="bg-gray-50">
363+
<div
364+
className="max-w-7xl mx-auto py-12 px-4 sm:px-6 lg:py-16 lg:px-8 lg:flex lg:items-center lg:justify-between"
365+
>
366+
<h2
367+
className="text-3xl font-extrabold tracking-tight text-gray-900 sm:text-4xl"
368+
>
369+
<span className="block">Ready to dive in?</span>
370+
<span className="block text-indigo-600"
371+
>Start your free trial today.</span
372+
>
373+
</h2>
374+
<div className="mt-8 lex lg:mt-0 lg:flex-shrink-0">
375+
<div className="inline-flex rounded-md shadow">
376+
<a
377+
href="#"
378+
className="inline-flex items-center justify-center px-5 py-3 border border-transparent text-base font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700"
379+
>
380+
Get started
381+
</a>
382+
</div>
383+
<div className="ml-3 inline-flex rounded-md shadow">
384+
<a
385+
href="#"
386+
className="inline-flex items-center justify-center px-5 py-3 border border-transparent text-base font-medium rounded-md text-indigo-600 bg-white hover:bg-indigo-50"
387+
>
388+
Learn more
389+
</a>
390+
</div>
391+
</div>
392+
</div>
393+
</div>
394+
`;
395+
396+
expect(isHTML(text)).toBe(false);
397+
});

src/isHTML.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const UPPERCASE_TAGS = /<\s*[A-Z]/;
2+
const CLASSNAME_ATTRIBUTE = /<.*className.*>/;
23

34
/**
45
* Checks if a given input string is HTML code.
@@ -15,5 +16,9 @@ export function isHTML(text: string): boolean {
1516
return false;
1617
}
1718

19+
if (CLASSNAME_ATTRIBUTE.test(trimmedText)) {
20+
return false;
21+
}
22+
1823
return true;
1924
}

0 commit comments

Comments
 (0)