diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/LinkFieldDriver.cs index 3f314311c21..48dbf43ca1d 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.Localization; using System; using System.Collections.Generic; +using System.Security.Policy; namespace Orchard.Fields.Drivers { public class LinkFieldDriver : ContentFieldDriver { @@ -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)); + } + } + } } } 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 89bcbfdbf47..c35df6491ac 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) {