private static void AddAttribute(LinkInline link, string name, string value) { HtmlAttributes attributes = link.GetAttributes(); if (!attributes.Properties.Any(x => x.Key == name)) { attributes.AddPropertyIfNotExist(name, value); link.SetAttributes(attributes); } }
public void TestAddProperty() { var attributes = new HtmlAttributes(); attributes.AddProperty("key1", "1"); Assert.NotNull(attributes.Properties); Assert.AreEqual(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("key1", "1") }, attributes.Properties); attributes.AddPropertyIfNotExist("key1", "1"); Assert.NotNull(attributes.Properties); Assert.AreEqual(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("key1", "1") }, attributes.Properties); attributes.AddPropertyIfNotExist("key2", "2"); Assert.AreEqual(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("key1", "1"), new KeyValuePair <string, string>("key2", "2") }, attributes.Properties); }
private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline) { if (linkInline.IsImage && linkInline.Url != null) { Uri uri; // Only process absolute Uri if (Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out uri) && uri.IsAbsoluteUri) { var htmlAttributes = new HtmlAttributes(); var fromAttributes = linkInline.TryGetAttributes(); if (fromAttributes != null) { fromAttributes.CopyTo(htmlAttributes, false, false); } // TODO: this code is not pluggable, so for now, we handle only the following web providers: // - youtube // - vimeo var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); string iFrameUrl = null; if (uri.Host.StartsWith("www.youtube.com", StringComparison.OrdinalIgnoreCase)) { var query = SplitQuery(uri); if (query.Length > 0 && query[0].StartsWith("v=")) { iFrameUrl = $"https://www.youtube.com/embed/{query[0].Substring(2)}"; } } else if (uri.Host.StartsWith("vimeo.com", StringComparison.OrdinalIgnoreCase)) { var items = path.Split('/'); if (items.Length > 0) { iFrameUrl = $"https://player.vimeo.com/video/{items[items.Length - 1]}"; } } if (iFrameUrl != null) { renderer.Write($"<iframe src=\"{iFrameUrl}\""); htmlAttributes.AddPropertyIfNotExist("width", Options.Width); htmlAttributes.AddPropertyIfNotExist("height", Options.Height); htmlAttributes.AddPropertyIfNotExist("frameborder", "0"); htmlAttributes.AddPropertyIfNotExist("allowfullscreen", null); renderer.WriteAttributes(htmlAttributes); renderer.Write("></iframe>"); return(true); } else { // Otherwise try to detect if we have an audio/video from the file extension string mimeType; var lastDot = path.LastIndexOf('.'); if (lastDot >= 0 && Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out mimeType)) { var isAudio = mimeType.StartsWith("audio"); var tagType = isAudio ? "audio" : "video"; renderer.Write($"<{tagType}"); htmlAttributes.AddPropertyIfNotExist("width", Options.Width); if (!isAudio) { htmlAttributes.AddPropertyIfNotExist("height", Options.Height); } htmlAttributes.AddPropertyIfNotExist("controls", null); renderer.WriteAttributes(htmlAttributes); renderer.Write($"><source type=\"{mimeType}\" src=\"{linkInline.Url}\"></source></{tagType}>"); renderer.Write("</iframe>"); return(true); } } } } return(false); }