v1
12/7/2024 by mirrorsky -00
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const escapeRE = /["'&<>]/

function escapeHtmlWithoutRegExp(string) {
    const str = '' + string

    let html = ''
    let escaped
    let index
    let lastIndex = 0
    for (index = 0; index < str.length; index++) {
        switch (str.charCodeAt(index)) {
            case 34: // "
                escaped = '&quot;'
                break
            case 38: // &
                escaped = '&amp;'
                break
            case 39: // '
                escaped = '&#39;'
                break
            case 60: // <
                escaped = '&lt;'
                break
            case 62: // >
                escaped = '&gt;'
                break
            default:
                continue
        }

        if (lastIndex !== index) {
            html += str.slice(lastIndex, index)
        }

        lastIndex = index + 1
        html += escaped
    }

    return lastIndex !== index ? html + str.slice(lastIndex, index) : html
}

function escapeHtmlWithRegExp(string) {
    const str = '' + string
    const match = escapeRE.exec(str)

    if (!match) {
        return str
    }

    let html = ''
    let escaped
    let index
    let lastIndex = 0
    for (index = match.index; index < str.length; index++) {
        switch (str.charCodeAt(index)) {
            case 34: // "
                escaped = '&quot;'
                break
            case 38: // &
                escaped = '&amp;'
                break
            case 39: // '
                escaped = '&#39;'
                break
            case 60: // <
                escaped = '&lt;'
                break
            case 62: // >
                escaped = '&gt;'
                break
            default:
                continue
        }

        if (lastIndex !== index) {
            html += str.slice(lastIndex, index)
        }

        lastIndex = index + 1
        html += escaped
    }

    return lastIndex !== index ? html + str.slice(lastIndex, index) : html
}
delete caserun single casemove downdrag and drop case


ready



escapeHtmlWithoutRegExp(`foo`)
escapeHtmlWithoutRegExp(true)
escapeHtmlWithoutRegExp(false)
delete caserun single casemove upmove downdrag and drop case


ready



escapeHtmlWithoutRegExp(`a && b`)
escapeHtmlWithoutRegExp(`"foo"`)
escapeHtmlWithoutRegExp(`'bar'`)
escapeHtmlWithoutRegExp(`<div>`)
delete caserun single casemove upmove downdrag and drop case


ready



escapeHtmlWithRegExp(`foo`)
escapeHtmlWithRegExp(true)
escapeHtmlWithRegExp(false)
delete caserun single casemove updrag and drop case


ready



escapeHtmlWithRegExp(`a && b`)
escapeHtmlWithRegExp(`"foo"`)
escapeHtmlWithRegExp(`'bar'`)
escapeHtmlWithRegExp(`<div>`)
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN