/// <summary> /// HTML Encodes a String so any that requires entitzing are converted to character entities /// </summary> /// <param name="value">Value to encode</param> /// <returns></returns> public static String HtmlEncode(String value) { #if PORTABLE return(System.Net.WebUtility.HtmlEncode(value)); #else return(HtmlEntity.Entitize(value, true, true)); #endif }
private string WriteToFile(string html) { var filePath = Path.Combine(Path.GetTempPath(), $"sm_element_{Id}.htm"); html = HtmlEntity.Entitize(html); File.WriteAllText(filePath, html + "\r\n<span />", Encoding.UTF8); return(filePath); }
public void Entitize_PassSimpleText_ShouldCorrectlyEntitize() { try { HtmlEntity.UseWebUtility = true; string result = HtmlEntity.Entitize("qwerty"); Assert.AreEqual("qwerty", result); } finally { HtmlEntity.UseWebUtility = false; } }
public void Entitize_PassEmojiUnicode_ShouldCorrectlyEntitize() { try { HtmlEntity.UseWebUtility = true; string result = HtmlEntity.Entitize("😂"); Assert.AreEqual("😂", result); } finally { HtmlEntity.UseWebUtility = false; } }
private List <Reaction> GetReactions(HtmlNode node) { var reactionsNode = node.SelectSingleNode(htmlConstants.MessageReactionsNodeXPath); var reactionsList = new List <Reaction>(); if (reactionsNode == null) { return(reactionsList); } var reactionNodes = reactionsNode.SelectNodes("li"); for (int i = 0; i < reactionNodes.Count; i++) { var nodeText = HtmlEntity.Entitize(reactionNodes[i].InnerText); ReactionType?reactionType = null; string username = null; foreach (var key in messageConstants.EmojiReactionTypesPairs.Keys) { if (nodeText.Contains(key)) { reactionType = messageConstants.EmojiReactionTypesPairs[key]; username = HtmlEntity.DeEntitize(nodeText.Replace(key, "")); break; } } if (reactionType == null) { continue; } if (username == null) { continue; } var user = GetUserByUsername(username); var reaction = new Reaction { User = user, ReactionType = reactionType.Value }; reactionsList.Add(reaction); user.Reactions.Add(reaction); } conversation.Reactions.AddRange(reactionsList); return(reactionsList); }
private static void ConvertInline(Inline inline, HtmlDocument doc, HtmlNode parent, StyleOptions base_style, ref HtmlNode working_node, ref StyleOptions current_style) { Hyperlink link = inline as Hyperlink; if (link != null) { working_node = CreateStyleTags(inline, doc, parent, parent, ref current_style, true); HtmlNode linkbase = working_node; if (link.NavigateUri.Scheme == "http" || link.NavigateUri.Scheme == "https") { linkbase = linkbase.AppendChild(doc.CreateElement("loc")); } HtmlNode linktag = linkbase.AppendChild(doc.CreateElement("a")); linktag.SetAttributeValue("href", link.NavigateUri.ToString()); StyleOptions link_current_style = current_style; var link_working_node = linktag; foreach (var i in link.Inlines) { ConvertInline(i, doc, linktag, base_style, ref link_working_node, ref link_current_style); } return; } var run = inline as Run; if (run != null) { working_node = CreateStyleTags(inline, doc, parent, parent, ref current_style); working_node.InnerHtml += HtmlEntity.Entitize(run.Text).Replace(" ", " "); return; } if (inline is LineBreak) { parent.AppendChild(doc.CreateElement("br")); return; } }
public IAsyncOperation <HtmlNode> TranslateAsync(string targetLangCode) { return(AsyncInfo.Run(async token => { var node = HtmlNode.CreateNode(this.Content.OuterHtml); foreach (var item in node.Descendants("#text")) { var data = item.GetInnerText(); var uri = $"https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8" + $"&sl=auto&tl={targetLangCode}&q={Uri.EscapeDataString(data)}"; var transRetHtml = await transClient.GetStringAsync(new Uri(uri)); var obj = JsonConvert.DeserializeObject <JArray>(transRetHtml); var objarr = (JArray)obj[0]; var translated = string.Concat(objarr.Select(a => a[0].ToString())); item.InnerHtml = HtmlEntity.Entitize(translated); } this.TranslatedContent = node; return node; })); }
public Task <string> ToHtml(string markdown) { this.ErrorLogs.Clear(); try { markdown = HtmlEntity.Entitize(markdown, false); } catch (Exception) { this.ErrorLogs.Add("Unable to process markdown text. Possible cause: Invalid characters"); } try { var convertedHtml = MarkdownConverter.ToHtml(markdown, this.templatePath); return(convertedHtml); } catch (Exception) { this.ErrorLogs.Add("Unable to convert markdown to HTML"); } return(Task.FromResult(markdown)); }
private string GetTinyUrl(string url) { return(new WebClient().DownloadString("http://tinyurl.com/api-create.php?url=" + HtmlEntity.Entitize(url))); }
/// <summary> /// HTML Encodes a String so any that requires entitzing are converted to character entities /// </summary> /// <param name="value">Value to encode</param> /// <returns></returns> public static String HtmlEncode(String value) { return(HtmlEntity.Entitize(value, true, true)); }
public static string HtmlEncode(string text) => HtmlEntity.Entitize(text, true, true);