private DescriptionEntry GenerateDescriptionEntry(Dictionary <string, string> values) { DateTime now = DateTime.UtcNow; DescriptionTextRenderType renderType = DescriptionTextRenderType.Plaintext; string text = values[FieldNames.Incident.Description]; string xhtmlValid; string errors; // Try to convert from HTML to XHTML, if failed, just leave it as plain text if (XmlSanitizer.TryMakeXHtml(text, out xhtmlValid, out errors)) { string xhtmlSanitized; if (XmlSanitizer.SanitizeXml(xhtmlValid, false, false, out xhtmlSanitized, out errors)) { text = xhtmlSanitized; renderType = DescriptionTextRenderType.Html; } } return(new DescriptionEntry { Cause = DescriptionEntryCause.Created, Date = now, ChangedBy = values[FieldNames.Incident.CreatedBy], SubmitDate = now, SubmittedBy = values[FieldNames.Incident.CreatedBy], Text = text, RenderType = renderType }); }
public void WhenHasIllegalChars_ThenRemoveIllegalChars() { char illegalChar = '\0'; string s = $"this {illegalChar} that {illegalChar} this"; var result = XmlSanitizer.Sanitize(s); Assert.That(result, Is.EqualTo("this that this")); }
public static DescriptionEntry GenerateDescriptionEntry(Dictionary <string, string> values, int maxHyperlinkLength) { DateTime now = DateTime.UtcNow; DescriptionTextRenderType renderType = DescriptionTextRenderType.Plaintext; string text = values[FieldNames.Incident.Description]; if (text.IndexOf("html", StringComparison.OrdinalIgnoreCase) >= 0) { // Try to convert from HTML to XHTML, if failed, just leave it as plain text string xhtmlValid; string errors; if (XmlSanitizer.TryMakeXHtml(text, out xhtmlValid, out errors)) { string xhtmlSanitized; if (XmlSanitizer.SanitizeXml(xhtmlValid, false, false, out xhtmlSanitized, out errors)) { text = xhtmlSanitized; renderType = DescriptionTextRenderType.Html; } } if (!string.IsNullOrEmpty(errors)) { Logger.Info("Failed to convert message body to HTML. Defaulting to plain text. Conversion message: " + errors); } else if (text.Length > DescriptionLengthMax) { // Truncate string if too long. IcM limits the number of characters in the DescriptionEntry.Text property. text = TruncateXml(text, DescriptionLengthMax, maxHyperlinkLength); } } else if (text.Length > DescriptionLengthMax) { // Truncate string if too long. IcM limits the number of characters in the DescriptionEntry.Text property. text = text.Substring(0, DescriptionLengthMax); text += TruncationMessage; } var descriptionEntry = new DescriptionEntry { Cause = DescriptionEntryCause.Created, Date = now, ChangedBy = values[FieldNames.Incident.CreatedBy], SubmitDate = now, SubmittedBy = values[FieldNames.Incident.CreatedBy], Text = text, RenderType = renderType, }; return(descriptionEntry); }
public void WhenIsEmpty_ThenReturnEmpty() { var result = XmlSanitizer.Sanitize(string.Empty); Assert.That(result, Is.Empty); }
public void WhenIsNull_ThenReturnNull() { var result = XmlSanitizer.Sanitize(null); Assert.That(result, Is.Null); }