Skip to content

Fix url validation errors#11835

Merged
noisysocks merged 5 commits into
masterfrom
fix/url-validation-cases
Nov 16, 2018
Merged

Fix url validation errors#11835
noisysocks merged 5 commits into
masterfrom
fix/url-validation-cases

Conversation

@talldan
Copy link
Copy Markdown
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
@talldan talldan force-pushed the fix/url-validation-cases branch from d926d48 to e810f59 Compare November 14, 2018 06:41
@talldan talldan requested a review from noisysocks November 14, 2018 10:17
@noisysocks noisysocks self-assigned this Nov 15, 2018
@noisysocks noisysocks added this to the 4.5 milestone Nov 16, 2018
Copy link
Copy Markdown
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!


const fragment = getFragment( trimmedHref );
if ( fragment && ! isValidFragment( trimmedHref ) ) {
if ( fragment && ! isValidFragment( fragment ) ) {
Copy link
Copy Markdown
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.

2 participants