Problem/Motivation
While working in some issues I realized the need to improve UrlHelper::isValid()(
in the file ./core/lib/Drupal/Component/Utility/UrlHelper.php
The issues I've found:
- Punycode (International Domain Names) URLs cannot be validated
- It does not accept protocol-relative URLs
- It does not take in account the allowed protocols because it does not use
UrlHelper::getAllowedProtocols()
- It uses custom regex, while we could use Symfony
UrlValidator
which has a similar regex
Currently the Symfony UrlValidator
also has some issues:
- It depends of context, which is not available at
UrlHelper::isValid()
- Its code has some bugs displaying the exception's messages
Proposed resolution
- Fix Punycode validation - We could use a library for it or just PHP intl' extension with the function idn_to_ascii()
- Accept protocol-relative URLs - currently being fixed in another issue, but fully rewriting this
UrlHelper::isValid()(
maybe would be a better solution: #1783278: Scheme-relative URL rejected by validation - Validate with the allowed protocols by using
UrlHelper::getAllowedProtocols()
- Check the possibility of fixing/using Symfony
UrlValidator
: http://symfony.com/doc/current/reference/constraints/Url.html