Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix url validation errors #11835

Merged
merged 5 commits into from Nov 16, 2018
Merged

Fix url validation errors #11835

merged 5 commits into from Nov 16, 2018

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Nov 14, 2018

Description

When adding href validation in #11286 I made a few errors, which this PR addresses

  1. Fragments in URLs were not being checked correctly, any existence of a fragment would make a URL fail the validation 🤦‍♂️ . This was happening due to the wrong value being passed to isValidFragment (the entire href was being passed instead of just the fragment).
  2. Because of the issue above, tests were also returning a false positive when validating incorrect fragments with two hashes like '#test#test'. This was happening because getFragment only captured from the last occurrence of a hash, now it captures from the first.
  3. Validation of HTTP urls didn't catch issues like the incorrect number of forward slashes following the protocol. I've added a special case for HTTP urls.
  4. isValidProtocol considered a protocol with trailing forward slashes as valid. I realised this didn't match what getProtocol considered a protocol (characters up to and including the colon, e.g. 'http:'), so I've made it stricter.

How has this been tested?

  • Added test cases to unit tests
  • Manual testing
  1. Try adding one of the following URLs as a link:
  1. Expect the validation to fail (the link should be displayed in red text).
  2. Try adding the following URL as a link:
  1. Expect the validation to succeed (the link should be displayed normally).

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@talldan talldan self-assigned this Nov 14, 2018
@talldan talldan added the [Type] Bug An existing feature does not function as intended label Nov 14, 2018
@noisysocks noisysocks self-assigned this Nov 15, 2018
@noisysocks noisysocks added this to the 4.5 milestone Nov 16, 2018
Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 looks good!

@@ -60,7 +66,7 @@ export function isValidHref( href ) {
}

const fragment = getFragment( trimmedHref );
if ( fragment && ! isValidFragment( trimmedHref ) ) {
if ( fragment && ! isValidFragment( fragment ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants