Skip to content

Commit

Permalink
refactor(utils): Simplify quickExpr (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Feb 4, 2021
1 parent d622964 commit 4aa3d39
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 8 additions & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ exports.cloneDom = function (dom) {
return clone;
};

/** A simple way to check for HTML strings or ID strings. */
var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w-]*)$)/;
/**
* 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
*/
var quickExpr = /<[a-zA-Z][^]*>/;

/**
* Check if string is HTML.
Expand All @@ -96,16 +101,6 @@ var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w-]*)$)/;
* @returns {boolean} Indicates if `str` is HTML.
*/
exports.isHtml = function (str) {
// Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
if (
str.charAt(0) === '<' &&
str.charAt(str.length - 1) === '>' &&
str.length >= 3
) {
return true;
}

// Run the regex
var match = quickExpr.exec(str);
return !!(match && match[1]);
return quickExpr.test(str);
};
8 changes: 7 additions & 1 deletion test/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('cheerio', function () {
});

it('should gracefully degrade on complex, unmatched queries', function () {
var $elem = cheerio('Eastern States Cup #8-fin&nbsp;<br>Downhill&nbsp;');
var $elem = cheerio('Eastern States Cup #8-fin&nbsp;<1br>Downhill&nbsp;');
expect($elem).toHaveLength(0);
});

Expand Down Expand Up @@ -407,6 +407,12 @@ describe('cheerio', function () {
expect(utils.isHtml('<html>')).toBe(true);
expect(utils.isHtml('\n<html>\n')).toBe(true);
expect(utils.isHtml('#main')).toBe(false);
expect(utils.isHtml('\n<p>foo<p>bar\n')).toBe(true);
expect(utils.isHtml('dog<p>fox<p>cat')).toBe(true);
expect(utils.isHtml('<p>fox<p>cat')).toBe(true);
expect(utils.isHtml('\n<p>fox<p>cat\n')).toBe(true);
expect(utils.isHtml('#<p>fox<p>cat#')).toBe(true);
expect(utils.isHtml('<123>')).toBe(false);
});
});
});

0 comments on commit 4aa3d39

Please sign in to comment.