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

refactor(utils): Hand-roll isHTML #1935

Merged
merged 1 commit into from
Jun 15, 2021
Merged
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
24 changes: 14 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,27 @@ export function cloneDom<T extends Node>(dom: T | T[]): T[] {
return clone;
}

/**
* A simple way to check for HTML strings. Tests for a `<` within a string,
* immediate followed by a letter and eventually followed by a `>`.
*
* @private
*/
const quickExpr = /<[a-zA-Z][^]*>/;

/**
* Check if string is HTML.
*
* Tests for a `<` within a string, immediate followed by a letter and
* eventually followed by a `>`.
*
* @private
* @category Utils
* @param str - String to check.
* @returns Indicates if `str` is HTML.
*/
export function isHtml(str: string): boolean {
// Run the regex
return quickExpr.test(str);
const tagStart = str.indexOf('<');

if (tagStart < 0 || tagStart > str.length - 3) return false;

const tagChar = str.charAt(tagStart + 1);

return (
((tagChar >= 'a' && tagChar <= 'z') ||
(tagChar >= 'A' && tagChar <= 'Z')) &&
str.includes('>', tagStart + 2)
);
}