Skip to content

Commit

Permalink
Test: refactor: makeAnchorElement()
Browse files Browse the repository at this point in the history
  • Loading branch information
dieulot committed Mar 7, 2023
1 parent 33ccd06 commit fda8883
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ async function requestListener(req, res) {
content += `<a href="/${page}?${getRandomId()}" data-no-instant><span>Manually blacklisted link</span></a>`
content += `<a href="/${page}?${getRandomId()}"><span>Non-whitelisted link</span></a>`

const openingTag = `<a href="/${page}?${getRandomId()}">`
content += `${openingTag}<span>Query string <small>${openingTag.replace('<', '&lt;').replace('>', '&gt;')}</small></span></a>`
content += makeAnchorElement('Query string', `<a href="/${page}?${getRandomId()}">`)

content += `<a href="https://www.google.com/" ${dataInstantAttribute}><span>External link</span></a>`
content += `<a><span>&lt;a&gt; without <code>href</code></span></a>`
Expand Down Expand Up @@ -177,3 +176,21 @@ function sha384(data) {
function getRandomId() {
return crypto.randomUUID().split('-')[0]
}

function makeAnchorElement(text, openingTag) {
return `
${openingTag}
<span>
${text}
<small>${escapeHTMLTags(openingTag)}</small>
</span>
</a>
`
}

function escapeHTMLTags(html) {
const escaped = html
.replace('<', '&lt;')
.replace('>', '&gt;')
return escaped
}

0 comments on commit fda8883

Please sign in to comment.