From 81570fab35bf93d698a14045f55101b2d72e66a5 Mon Sep 17 00:00:00 2001 From: Andrea Piovanelli <83577153+AndreaPiovanelli@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:14:13 +0100 Subject: [PATCH] Internal page links validation for LinkField (#8805) * Added page internal references management in LinkField valid urls (e.g. field.Value = "#someId") * Code refactoring to check LinkField value. --- .../Orchard.Fields/Drivers/LinkFieldDriver.cs | 29 +++++++++++++++++-- .../EditorTemplates/Fields/Link.Edit.cshtml | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs index 3f314311c..48dbf43ca 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs @@ -6,6 +6,7 @@ using Orchard.Fields.Settings; using Orchard.Localization; using System; using System.Collections.Generic; +using System.Security.Policy; namespace Orchard.Fields.Drivers { public class LinkFieldDriver : ContentFieldDriver { @@ -57,11 +58,33 @@ namespace Orchard.Fields.Drivers { 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)); + } + } + } } } diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Link.Edit.cshtml b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Link.Edit.cshtml index 89bcbfdbf..c35df6491 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Link.Edit.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Link.Edit.cshtml @@ -25,7 +25,7 @@
@(settings.Required ? Html.TextBoxFor(m => m.Value, new { @class = "text large", required = "required" }) : Html.TextBoxFor(m => m.Value, new { @class = "text large" })) - @T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, ...") + @T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, #some_id, ...")
@if (settings.LinkTextMode == LinkTextMode.Optional || settings.LinkTextMode == LinkTextMode.Required) {