private string Convert(object source, bool preview) { if (source == null) { return(null); } var sourceString = source.ToString(); // ensures string is parsed for {localLink} and URLs and media are resolved correctly sourceString = _linkParser.EnsureInternalLinks(sourceString, preview); sourceString = _urlParser.EnsureUrls(sourceString); sourceString = _imageSourceParser.EnsureImageSources(sourceString); // ensure string is parsed for macros and macros are executed correctly sourceString = RenderRteMacros(sourceString, preview); // find and remove the rel attributes used in the Umbraco UI from img tags var doc = new HtmlDocument(); doc.LoadHtml(sourceString); if (doc.ParseErrors.Any() == false && doc.DocumentNode != null) { // Find all images with rel attribute var imgNodes = doc.DocumentNode.SelectNodes("//img[@rel]"); var modified = false; if (imgNodes != null) { foreach (var img in imgNodes) { var nodeId = img.GetAttributeValue("rel", string.Empty); if (int.TryParse(nodeId, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)) { img.Attributes.Remove("rel"); modified = true; } } } // Find all a and img tags with a data-udi attribute var dataUdiNodes = doc.DocumentNode.SelectNodes("(//a|//img)[@data-udi]"); if (dataUdiNodes != null) { foreach (var node in dataUdiNodes) { node.Attributes.Remove("data-udi"); modified = true; } } if (modified) { return(doc.DocumentNode.OuterHtml); } } return(sourceString); }
public HtmlString ParseHtmlString(string input) { var ensured = _localLinkParser.EnsureInternalLinks(input); var parsed = new HtmlString(ensured); return(parsed); }
public object Convert(object value) { using (var cRef = _umbracoContextFactory.EnsureUmbracoContext()) { var parsedHtml = _localLinkParser.EnsureInternalLinks(value.ToString(), false); parsedHtml = _localImageParser.EnsureImageSources(parsedHtml); return(parsedHtml); } }
public void ParseLocalLinks(string input, string result) { // setup a mock URL provider which we'll use for testing var contentUrlProvider = new Mock <IUrlProvider>(); contentUrlProvider .Setup(x => x.GetUrl(It.IsAny <IPublishedContent>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns(UrlInfo.Url("/my-test-url")); var contentType = new PublishedContentType(Guid.NewGuid(), 666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var publishedContent = new Mock <IPublishedContent>(); publishedContent.Setup(x => x.Id).Returns(1234); publishedContent.Setup(x => x.ContentType).Returns(contentType); var mediaType = new PublishedContentType(Guid.NewGuid(), 777, "image", PublishedItemType.Media, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var media = new Mock <IPublishedContent>(); media.Setup(x => x.ContentType).Returns(mediaType); var mediaUrlProvider = new Mock <IMediaUrlProvider>(); mediaUrlProvider.Setup(x => x.GetMediaUrl(It.IsAny <IPublishedContent>(), It.IsAny <string>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns(UrlInfo.Url("/media/1001/my-image.jpg")); var umbracoContextAccessor = new TestUmbracoContextAccessor(); IUmbracoContextFactory umbracoContextFactory = TestUmbracoContextFactory.Create( umbracoContextAccessor: umbracoContextAccessor); var webRoutingSettings = new WebRoutingSettings(); var publishedUrlProvider = new UrlProvider( umbracoContextAccessor, Microsoft.Extensions.Options.Options.Create(webRoutingSettings), new UrlProviderCollection(() => new[] { contentUrlProvider.Object }), new MediaUrlProviderCollection(() => new[] { mediaUrlProvider.Object }), Mock.Of <IVariationContextAccessor>()); using (UmbracoContextReference reference = umbracoContextFactory.EnsureUmbracoContext()) { var contentCache = Mock.Get(reference.UmbracoContext.Content); contentCache.Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent.Object); contentCache.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent.Object); var mediaCache = Mock.Get(reference.UmbracoContext.Media); mediaCache.Setup(x => x.GetById(It.IsAny <int>())).Returns(media.Object); mediaCache.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(media.Object); var linkParser = new HtmlLocalLinkParser(umbracoContextAccessor, publishedUrlProvider); var output = linkParser.EnsureInternalLinks(input); Assert.AreEqual(result, output); } }
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) { if (source == null) { return(null); } var sourceString = source.ToString(); // ensures string is parsed for {localLink} and URLs are resolved correctly sourceString = _linkParser.EnsureInternalLinks(sourceString, preview); sourceString = _urlParser.EnsureUrls(sourceString); return(sourceString); }
public void ParseLocalLinks(string input, string result) { //setup a mock url provider which we'll use for testing var contentUrlProvider = new Mock <IUrlProvider>(); contentUrlProvider .Setup(x => x.GetUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns(UrlInfo.Url("/my-test-url")); var contentType = new PublishedContentType(666, "alias", PublishedItemType.Content, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var publishedContent = new Mock <IPublishedContent>(); publishedContent.Setup(x => x.Id).Returns(1234); publishedContent.Setup(x => x.ContentType).Returns(contentType); var mediaType = new PublishedContentType(777, "image", PublishedItemType.Media, Enumerable.Empty <string>(), Enumerable.Empty <PublishedPropertyType>(), ContentVariation.Nothing); var media = new Mock <IPublishedContent>(); media.Setup(x => x.ContentType).Returns(mediaType); var mediaUrlProvider = new Mock <IMediaUrlProvider>(); mediaUrlProvider.Setup(x => x.GetMediaUrl(It.IsAny <UmbracoContext>(), It.IsAny <IPublishedContent>(), It.IsAny <string>(), It.IsAny <UrlMode>(), It.IsAny <string>(), It.IsAny <Uri>())) .Returns(UrlInfo.Url("/media/1001/my-image.jpg")); var umbracoContextAccessor = new TestUmbracoContextAccessor(); var umbracoContextFactory = TestUmbracoContextFactory.Create( urlProvider: contentUrlProvider.Object, mediaUrlProvider: mediaUrlProvider.Object, umbracoContextAccessor: umbracoContextAccessor); using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of <HttpContextBase>())) { var contentCache = Mock.Get(reference.UmbracoContext.Content); contentCache.Setup(x => x.GetById(It.IsAny <int>())).Returns(publishedContent.Object); contentCache.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(publishedContent.Object); var mediaCache = Mock.Get(reference.UmbracoContext.Media); mediaCache.Setup(x => x.GetById(It.IsAny <int>())).Returns(media.Object); mediaCache.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(media.Object); var linkParser = new HtmlLocalLinkParser(umbracoContextAccessor); var output = linkParser.EnsureInternalLinks(input); Assert.AreEqual(result, output); } }