Skip to content

Commit

Permalink
refactor(utils): Hand-roll isHTML
Browse files Browse the repository at this point in the history
Should perform slightly better, and we are not at the whim of a regex engine for guaranteed linear performance.
  • Loading branch information
fb55 committed Jun 15, 2021
1 parent 2707c82 commit 1fe07b5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/utils.ts
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)
);
}

0 comments on commit 1fe07b5

Please sign in to comment.