Skip to content

Commit

Permalink
Merge 6cd7ff4 into 456fbe5
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Feb 4, 2021
2 parents 456fbe5 + 6cd7ff4 commit 08d4688
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
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
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 08d4688

Please sign in to comment.