Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome's Regexes - Can I view them?

Html5 input types includes many new types.

(range , Email , date etc...)

For example :

<input type="url" >

enter image description here

I know that IE used to have regex store ( on one of its internal folders)

Question :

Can I see in what regexes does chrome use to validate the input ?

Is it under a viewable file or something ? / how can I see those regexs ?

like image 717
Royi Namir Avatar asked Oct 03 '22 00:10

Royi Namir


1 Answers

I looked up the source code of Blink. Keep in mind I never saw it before today, so I might be completely off. Assuming I found the right place -

For type="url" fields there is URLInputType, with the code:

bool URLInputType::typeMismatchFor(const String& value) const
{
    return !value.isEmpty() && !KURL(KURL(), value).isValid();
}

typeMismatchFor is called from HTMLInputElement::isValidValue

bool HTMLInputElement::isValidValue(const String& value) const
{
    if (!m_inputType->canSetStringValue()) {
        ASSERT_NOT_REACHED();
        return false;
    }
    return !m_inputType->typeMismatchFor(value) // <-- here
        && !m_inputType->stepMismatch(value)
        && !m_inputType->rangeUnderflow(value)
        && !m_inputType->rangeOverflow(value)
        && !tooLong(value, IgnoreDirtyFlag)
        && !m_inputType->patternMismatch(value)
        && !m_inputType->valueMissing(value);
}

KURL seems like a proper implementation of a URL, used everywhere in Blink.

In comparison, the implementation for EmailInputType, typeMismatchFor calls isValidEmailAddress, which does use a regex:

static const char emailPattern[] =
    "[a-z0-9!#$%&'*+/=?^_`{|}~.-]+" // local part
    "@"
    "[a-z0-9-]+(\\.[a-z0-9-]+)*"; // domain part

static bool isValidEmailAddress(const String& address)
{
    int addressLength = address.length();
    if (!addressLength)
        return false;

    DEFINE_STATIC_LOCAL(const RegularExpression, regExp,
                        (emailPattern, TextCaseInsensitive));

    int matchLength;
    int matchOffset = regExp.match(address, 0, &matchLength);

    return !matchOffset && matchLength == addressLength;
}

These elements and more can be found on the /html folder. It seems most of them are using proper parsing and checking of the input, not regular expressions.

like image 193
Kobi Avatar answered Oct 07 '22 18:10

Kobi