Skip to content

Commit

Permalink
Internal page links validation for LinkField (#8805)
Browse files Browse the repository at this point in the history
* Added page internal references management in LinkField valid urls (e.g. field.Value = "#someId")

* Code refactoring to check LinkField value.
  • Loading branch information
AndreaPiovanelli authored Oct 29, 2024
1 parent 0d9fccb commit 81570fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Orchard.Localization;
using System;
using System.Collections.Generic;
using System.Security.Policy;

namespace Orchard.Fields.Drivers {
public class LinkFieldDriver : ContentFieldDriver<LinkField> {
Expand Down Expand Up @@ -57,11 +58,33 @@ protected override DriverResult Editor(ContentPart part, LinkField field, IUpdat
if (settings.Required && String.IsNullOrWhiteSpace(field.Value)) {
updater.AddModelError(GetPrefix(field, part), T("Url is required for {0}", T(field.DisplayName)));
}
else if (!String.IsNullOrWhiteSpace(field.Value) && !Uri.IsWellFormedUriString(field.Value, UriKind.RelativeOrAbsolute)) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
else if (settings.LinkTextMode == LinkTextMode.Required && String.IsNullOrWhiteSpace(field.Text)) {
updater.AddModelError(GetPrefix(field, part), T("Text is required for {0}.", T(field.DisplayName)));
} else if (!String.IsNullOrWhiteSpace(field.Value)) {
// Check if it's a valid uri, considering that there may be the link to an anchor only
// 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)
if (field.Value.StartsWith("#")) {
// The field value is a tag id reference
// For html 5, a tag id is valid as long as it doesn't contain white spaces
if (field.Value.IndexOf(' ') >= 0) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
} else {
var urlAndRef = field.Value.Split(new char[] { '#' }, 2);

// Since field value is a proper url and not a tag id only, assume the first part of the array is the actual url to link to
if (!String.IsNullOrWhiteSpace(urlAndRef[0]) && !Uri.IsWellFormedUriString(urlAndRef[0], UriKind.RelativeOrAbsolute)) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
} else if (urlAndRef.Length > 1) {
// The second part of the url is the id reference
// For html 5, a tag id is valid as long as it doesn't contain white spaces
if (urlAndRef[1].IndexOf(' ') >= 0) {
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid url.", field.Value));
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
<div class="editor-field">
@(settings.Required ? Html.TextBoxFor(m => m.Value, new { @class = "text large", required = "required" }) : Html.TextBoxFor(m => m.Value, new { @class = "text large" }))
<span class="hint">@T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, ...")</span>
<span class="hint">@T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, #some_id, ...")</span>
</div>
@if (settings.LinkTextMode == LinkTextMode.Optional || settings.LinkTextMode == LinkTextMode.Required) {
<div class="editor-label">
Expand Down

0 comments on commit 81570fa

Please sign in to comment.