function labelForInputMatches()

in src/js/other-websites/email_detector.js [32:52]


function labelForInputMatches(element, regex) {
    // First check for labels correctly associated with the <input> element
    for (const label of Array.from(element.labels)) {
        const numFound = numRegexMatches(regex, label.innerText);
        if (numFound > 0) return true;
    }

    // Then check for a common mistake found in the training set: using the
    // <input>'s `name` attribute instead of its `id` to associate with a label
    const form = element.form;
    if (element.name.length > 0 && form !== null) { // look at nearby elements in general, not just in parent form?
        for (const label of Array.from(form.getElementsByTagName("label"))) {
            if (label.htmlFor.length > 0 && (label.htmlFor === element.name)) {
                const numFound = numRegexMatches(regex, label.innerText);
                if (numFound > 0) return true;
            }
        }
    }

    return false;
}