Use JQuery to solve Catastrophic Backtracking with regex (#39112)

* Use JQuery to solve Catastrophic Backtracking with regex

* fix: improve regex for last test

* fix: improved the other tests with regex

* fix: change regex to allow spaces

Co-authored-by: Randell Dawson <rdawson@onepathtech.com>
Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com>
pull/39529/head
PSN221B 2020-09-05 02:36:57 +05:30 committed by GitHub
parent 3e3652a788
commit 3425dadb46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 9 deletions

View File

@ -37,18 +37,54 @@ Edit the markup so there's a <code>head</code> and a <code>body</code>. The <cod
```yml ```yml
tests: tests:
- text: There should be only one <code>head</code> element on the page. - text: There should be only one <code>head</code> element on the page.
testString: assert($('head').length == 1); testString: |
const headElems = code.replace(/\n/g,'').match(/\<head\s*>.*?\<\/head\s*>/g);
assert(headElems && headElems.length === 1);
- text: There should be only one <code>body</code> element on the page. - text: There should be only one <code>body</code> element on the page.
testString: assert($('body').length == 1); testString: |
const bodyElems = code.replace(/\n/g,'').match(/<body\s*>.*?<\/body\s*>/g);
assert(bodyElems && bodyElems.length === 1);
- text: The <code>head</code> element should be a child of the <code>html</code> element. - text: The <code>head</code> element should be a child of the <code>html</code> element.
testString: assert($('html').children('head').length == 1); testString: |
- text: The <code>body</code> element should be a child of the <code>html</code> element. const htmlChildren = code.replace(/\n/g,'').match(/<html\s*>(?<children>.*)<\/html\s*>/);
testString: assert($('html').children('body').length == 1); let foundHead;
- text: The <code>head</code> element should wrap around the <code>title</code> element. if(htmlChildren) {
testString: assert(code.match(/<head>\s*?<title>\s*?.*?\s*?<\/title>\s*?<\/head>/gi)); const { children } = htmlChildren.groups;
- text: The <code>body</code> element should wrap around both the <code>h1</code> and <code>p</code> elements.
testString: assert(code.match(/<body>\s*?(((<h1>\s*?.*?\s*?<\/h1>\s*?)(<p>(.*\s*)*?<\/p>\s*?))|((<p>\s*?.*?\s*?<\/p>\s*?)(<h1>(.*\s*)*?<\/h1>\s*?)))<\/body>/gi));
foundHead = children.match(/<head\s*>.*<\/head\s*>/);
}
assert(foundHead);
- text: The <code>body</code> element should be a child of the <code>html</code> element.
testString: |
const htmlChildren = code.replace(/\n/g,'').match(/<html\s*>(?<children>.*?)<\/html\s*>/);
let foundBody;
if(htmlChildren) {
const { children } = htmlChildren.groups;
foundBody = children.match(/<body\s*>.*<\/body\s*>/);
}
assert(foundBody);
- text: The <code>head</code> element should wrap around the <code>title</code> element.
testString: |
const headChildren = code.replace(/\n/g,'').match(/<head\s*>(?<children>.*?)<\/head\s*>/);
let foundTitle;
if(headChildren) {
const { children } = headChildren.groups;
foundTitle = children.match(/<title\s*>.*?<\/title\s*>/);
}
assert(foundTitle);
- text: The <code>body</code> element should wrap around both the <code>h1</code> and <code>p</code> elements.
testString: |
const bodyChildren = code.replace(/\n/g,'').match(/<body\s*>(?<children>.*?)<\/body\s*>/);
let foundElems;
if(bodyChildren) {
const { children } = bodyChildren.groups;
const h1s = children.match(/<h1\s*>.*<\/h1\s*>/g);
const ps = children.match(/<p\s*>.*<\/p\s*>/g);
const numH1s = h1s ? h1s.length : 0;
const numPs = ps ? ps.length : 0;
foundElems = numH1s === 1 && numPs === 1;
}
assert(foundElems);
``` ```
</section> </section>