예제 #1
0
        /// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="attributes">Addtiional attributes to add. Do not include href or title</param>
        /// <param name="contents">Content to go in the link instead of the standard text</param>
        /// <returns>An "a" HTML element</returns>
        public virtual string RenderLink(Fields.Link link, NameValueCollection attributes, string contents)
        {
            if (link == null)
            {
                return("");
            }
            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }

            string format = "<a href='{0}{1}' title='{2}' target='{3}' class='{4}' {5}>{6}</a>";

            string cls    = attributes.AllKeys.Any(x => x == "class") ? attributes["class"] : link.Class;
            string anchor = link.Anchor.IsNullOrEmpty() ? "" : "#" + link.Anchor;
            string target = attributes.AllKeys.Any(x => x == "target") ? attributes["target"] : link.Target;


            AttributeCheck(attributes, "class", link.Class);
            AttributeCheck(attributes, "target", link.Target);
            AttributeCheck(attributes, "title", link.Title);

            attributes.Remove("href");


            return(format.Formatted(link.Url, anchor, link.Title, target, cls, Utilities.ConvertAttributes(attributes), contents.IsNullOrEmpty() ? link.Text : contents));
        }
예제 #2
0
        /// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="attributes">Addtiional parameters to add. Do not include href or title</param>
        /// <param name="contents">Content to go in the link instead of the standard text</param>
        /// <returns>An "a" HTML element</returns>
        public static RenderingResult BeginRenderLink(Fields.Link link, SafeDictionary <string> attributes, string contents,
                                                      TextWriter writer)
        {
            if (link == null)
            {
                return(new RenderingResult(writer, string.Empty, string.Empty));
            }
            if (attributes == null)
            {
                attributes = new SafeDictionary <string>();
            }

            string format = "<a href='{0}' {1}>{2}";

            contents = contents == null ? link.Text ?? link.Title : contents;

            AttributeCheck(attributes, "class", link.Class);
            AttributeCheck(attributes, "target", link.Target);
            AttributeCheck(attributes, "title", link.Title);

            string firstPart = format.Formatted(link.BuildUrl(attributes), Utilities.ConvertAttributes(attributes), contents);
            string lastPart  = "</a>";

            return(new RenderingResult(writer, firstPart, lastPart));
        }
예제 #3
0
        /// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="attributes">Addtiional attributes to add. Do not include href or title</param>
        /// <param name="contents">Content to go in the link instead of the standard text</param>
        /// <returns>An "a" HTML element</returns>
        public static RenderingResult BeginRenderLink(Fields.Link link, NameValueCollection attributes, string contents, TextWriter writer)
        {
            if (link == null)
            {
                return(new RenderingResult(writer, string.Empty, string.Empty));
            }
            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }

            string format = "<a href='{0}{1}' title='{2}' model='{3}' class='{4}' {5}>{6}";

            string cls    = attributes.AllKeys.Any(x => x == "class") ? attributes["class"] : link.Class;
            string anchor = link.Anchor.IsNullOrEmpty() ? "" : "#" + link.Anchor;
            string target = attributes.AllKeys.Any(x => x == "model") ? attributes["model"] : link.Target;


            contents = contents == null ? link.Text ?? link.Title : contents;

            AttributeCheck(attributes, "class", link.Class);
            AttributeCheck(attributes, "model", link.Target);
            AttributeCheck(attributes, "title", link.Title);

            attributes.Remove("href");

            string firstPart = format.Formatted(link.Url, anchor, link.Title, target, cls, Utilities.ConvertAttributes(attributes), contents);
            string lastPart  = "</a>";

            return(new RenderingResult(writer, firstPart, lastPart));
        }
예제 #4
0
        public virtual string RenderLink(Fields.Link link, NameValueCollection attributes, string contents)
        {
            var sb     = new StringBuilder();
            var writer = new StringWriter(sb);

            BeginRenderLink(link, attributes, contents, writer);
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
예제 #5
0
        /// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="attributes">Addtiional attributes to add. Do not include href or title</param>
        /// <param name="contents">Content to go in the link instead of the standard text</param>
        /// <returns>An "a" HTML element</returns>
        public static RenderingResult BeginRenderLink(Fields.Link link, NameValueCollection attributes, string contents, TextWriter writer)
        {
            if (link == null)
            {
                return(new RenderingResult(writer, string.Empty, string.Empty));
            }
            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }


            string format = "<a href='{0}{1}' {2}>{3}";

            contents = contents == null ? link.Text ?? link.Title : contents;


            Func <string, Func <string>, string> getValue = (key, func) =>
            {
                var value = attributes.AllKeys.Any(x => x == key) ? attributes[key] : func();
                attributes.Remove(key);
                return(value);
            };

            UrlBuilder builder = new UrlBuilder(link.Url);

            var query  = getValue("query", () => link.Query);
            var anchor = getValue("anchor", () => link.Anchor);



            if (query.IsNotNullOrEmpty())
            {
                builder.AddQueryString(query);
            }

            AttributeCheck(attributes, "class", link.Class);
            AttributeCheck(attributes, "target", link.Target);
            AttributeCheck(attributes, "title", link.Title);

            string firstPart = format.Formatted(builder.ToString(), anchor.IsNullOrEmpty() ? "" : "#" + anchor, Utilities.ConvertAttributes(attributes), contents);
            string lastPart  = "</a>";

            return(new RenderingResult(writer, firstPart, lastPart));
        }
        public void SetField_MedialLinkMissingItem_ThrowsException()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();


            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "_blank",
                TargetId = new Guid("{11111111-CF15-4067-A3F4-85148606F9CD}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Media,
                Url = ""
            };


            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
        }
예제 #7
0
 public virtual string RenderLink(Fields.Link link, NameValueCollection attributes)
 {
     return(RenderLink(link, attributes, string.Empty));
 }
예제 #8
0
 public virtual string RenderLink(Fields.Link link)
 {
     return(RenderLink(link, null, string.Empty));
 }
        public void SetField_MailToLink_MailToLinkSetOnField()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver());
                var expected =
                    "<link url=\"mailto:[email protected]\" text=\"Test description\" class=\"testClass\" title=\"test alternative\" linktype=\"mailto\" />";

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "",
                    Class = "testClass",
                    Query = "",
                    Target = "",
                    TargetId = Guid.Empty,
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.MailTo,
                    Url = "mailto:[email protected]"
                };


                using (new ItemEditing(item, true))
                {
                    field.Value = string.Empty;
                }

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, value, null, null);
                }

                //Assert
                AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
            }
        }
예제 #10
0
        /// <summary>
        /// Gets the field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Object.</returns>
        public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {

            if (field == null || field.Value.Trim().IsNullOrEmpty()) return null;



            Link link = new Link();
            LinkField linkField = new LinkField(field);

            link.Anchor = linkField.Anchor;
            link.Class = linkField.Class;
            link.Text = linkField.Text;
            link.Title = linkField.Title;
            link.Target = linkField.Target;
            link.Query = linkField.QueryString;

            switch (linkField.LinkType)
            {
                case "anchor":
                    link.Url = linkField.Anchor;
                    link.Type = LinkType.Anchor;
                    break;
                case "external":
                    link.Url = linkField.Url;
                    link.Type = LinkType.External;
                    break;
                case "mailto":
                    link.Url = linkField.Url;
                    link.Type = LinkType.MailTo;
                    break;
                case "javascript":
                    link.Url = linkField.Url;
                    link.Type = LinkType.JavaScript;
                    break;
                case "media":
                    if (linkField.TargetItem == null)
                        link.Url = string.Empty;
                    else
                    {
                        global::Sitecore.Data.Items.MediaItem media =
                            new global::Sitecore.Data.Items.MediaItem(linkField.TargetItem);
                        link.Url = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                    }
                    link.Type = LinkType.Media;
                    link.TargetId = linkField.TargetID.Guid;
                    break;
                case "internal":
                    var urlOptions = Utilities.CreateUrlOptions(config.UrlOptions);
                    if (linkField.TargetItem == null) link.Url = string.Empty;
                    else link.Url = LinkManager.GetItemUrl(linkField.TargetItem, urlOptions);
                    link.Type = LinkType.Internal;
                    link.TargetId = linkField.TargetID.Guid;
                    link.Text =  linkField.Text.IsNullOrEmpty() ? (linkField.TargetItem == null ? string.Empty : linkField.TargetItem.DisplayName) : linkField.Text;
                    break;
                default:
                    return null;
            }


         

            return link;
        }
예제 #11
0
 public static RenderingResult BeginRenderLink(Fields.Link link, NameValueCollection attributes, string contents, TextWriter writer)
 {
     return(BeginRenderLink(link, attributes.ToSafeDictionary(), contents, writer));
 }
        public void SetField_MedialLink_MediaLinkSetOnField()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver());
                var expected =
                    "<link target=\"_blank\" title=\"test alternative\" linktype=\"media\" id=\"{0}\" url=\"Media.Aspx\" class=\"testClass\" text=\"Test description\" />"
                                            .Formatted(targetId.Guid.ToString("B").ToUpperInvariant());



                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "",
                    Class = "testClass",
                    Query = "",
                    Target = "_blank",
                    TargetId = targetId.Guid,
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.Media,
                    Url = ""
                };


                using (new ItemEditing(item, true))
                {
                    field.Value = string.Empty;
                }

                Sitecore.Resources.Media.MediaProvider mediaProvider =
                  NSubstitute.Substitute.For<Sitecore.Resources.Media.MediaProvider>();

                mediaProvider
                    .GetMediaUrl(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == targetId))
                    .Returns("Media.Aspx");

                // substitute the original provider with the mocked one
                using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
                {
                    //Act
                    using (new ItemEditing(item, true))
                    {
                        mapper.SetField(field, value, null, null);
                    }

                    //Assert
                    AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
                }
            }
        }
        public void SetField_InternalLinkWithSpecialCharacters_InternalLinkSetOnField()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver());
                var expected =
                    "<link target=\"testTarget\" title=\"test alternative\" querystring=\"q%3ds%253d\" linktype=\"internal\" id=\"{0}\" anchor=\"testAnchor\" url=\"/en/sitecore/content/Target.aspx\" class=\"testClass\" text=\"Test description\" />"
                        .Formatted(targetId.Guid.ToString("B").ToUpperInvariant());

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "testAnchor",
                    Class = "testClass",
                    Query = "q=s%3d",
                    Target = "testTarget",
                    TargetId = targetId.Guid,
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.Internal,
                    Url = ""
                };


                using (new ItemEditing(item, true))
                {
                    field.Value = string.Empty;
                }

                //Act
                using (new ItemEditing(item, true))
                {
                    mapper.SetField(field, value, null, null);
                }

                //Assert
                AssertHtml.AreHtmlElementsEqual(expected, field.Value, "link");
            }
        }
        public void SetField_MedialLink_MediaLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                  "<link target=\"_blank\" title=\"test alternative\" linktype=\"media\" id=\"{8E1C32F3-CF15-4067-A3F4-85148606F9CD}\" url=\"/~/media/8E1C32F3CF154067A3F485148606F9CD.ashx\" class=\"testClass\" text=\"Test description\" />";


            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "_blank",
                TargetId = new Guid("{8E1C32F3-CF15-4067-A3F4-85148606F9CD}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Media,
                Url = ""
            };


            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_InternalLinkMissingTarget_ThorwsException()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "testAnchor",
                Class = "testClass",
                Query = "q=s",
                Target = "testTarget",
                TargetId = new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Internal,
                Url = ""
            };


            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
        }
        public void SetField_InternalLink_InternalLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                "<link target=\"testTarget\" title=\"test alternative\" querystring=\"q=s\" linktype=\"internal\" id=\"{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}\" anchor=\"testAnchor\" url=\"/en/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/Target.aspx\" class=\"testClass\" text=\"Test description\" />";
            
            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "testAnchor",
                Class = "testClass",
                Query = "q=s",
                Target = "testTarget",
                TargetId = new Guid("{1AE37F34-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.Internal,
                Url = ""
            };


            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_MailToLink_MailToLinkSetOnField()
        {
            //Assign
            var mapper = new SitecoreFieldLinkMapper();
            var expected =
                "<link url=\"mailto:[email protected]\" text=\"Test description\" class=\"testClass\" title=\"test alternative\" linktype=\"mailto\" />";

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldLinkMapper/SetField");
            var field = item.Fields[FieldName];

            var value = new Link()
            {
                Anchor = "",
                Class = "testClass",
                Query = "",
                Target = "",
                TargetId = Guid.Empty,
                Text = "Test description",
                Title = "test alternative",
                Type = LinkType.MailTo,
                Url = "mailto:[email protected]"
            };


            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, value, null, null);
            }

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
        public void SetField_InternalLinkMissingTarget_ThorwsException()
        {
            //Assign
            var templateId = ID.NewID;
            var targetId = ID.NewID;
            var fieldName = "Field";

            using (Db database = new Db
            {
                new DbTemplate(templateId)
                {
                    {fieldName, ""}
                },
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),

            })
            {
                var mapper = new SitecoreFieldLinkMapper(new FakeUrlOptionsResolver());

                var item = database.GetItem("/sitecore/content/Target");
                var field = item.Fields[fieldName];

                var value = new Link()
                {
                    Anchor = "testAnchor",
                    Class = "testClass",
                    Query = "q=s",
                    Target = "testTarget",
                    TargetId = new Guid("{11111111-3B6F-4F5F-A5C2-FD2B9D5A47A0}"),
                    Text = "Test description",
                    Title = "test alternative",
                    Type = LinkType.Internal,
                    Url = ""
                };


                using (new ItemEditing(item, true))
                {
                    field.Value = string.Empty;
                }

                //Act
                using (new ItemEditing(item, true))
                {
                    Assert.Throws<MapperException>(() =>
                    {
                        mapper.SetField(field, value, null, null);

                    });
                }

                //Assert
            }
        }