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

Internal page links validation for LinkField #8805

Merged

Conversation

AndreaPiovanelli
Copy link
Contributor

We need to validate internal page references to a tag id in LinkFields, so I patched the LinkFieldDriver to validate urls like "mysite/mypage#mydiv" or just "#mydiv".
I also changed the hint in the editor shape to add "#someId" to the examples for valid field values.

The field value is divided in prefix and anchor to be sure to properly validate the format of the actual page the field is linking to:
A value like "http://mysite.com/page#div" is divided in two separate validations:

  • "http://mysite.com/page" is validated with the original Uri.IsWellFormedUriString(string) function.
  • "div" is validated by checking for the presence of white spaces inside the string (white space is the only forbidden character for html 5 tag ids).

// e.g.: field.Value = "#divId"
// Take everything before the first "#" character and check if it's a valid uri.
// If there is no character before the first "#", consider the value as a valid one (because it is a reference to a div inside the same page)
var index = field.Value.IndexOf('#');
Copy link
Member

Choose a reason for hiding this comment

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

A technique I like to use for these cases is field.Value.Split('#', 2). It will only give you an array or 1 or 2 values even if there are more #. This way you have access to both parts directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As suggested, refactored the code to use string split function.

@MatteoPiovanelli-Laser MatteoPiovanelli-Laser merged commit 81570fa into OrchardCMS:1.10.x Oct 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants