Example #1
0
        private void TestTextJsonSerialisation(PostStyle.Style style, string expected, string input, bool isEditor)
        {
            var target = new CommentInfo()
            {
                PostStyle = style,
                text = CommentInfo.FormatComment(input, style, CommentStatus.Hidden.NotHidden, isEditor)
            };

            MemoryStream stream = (MemoryStream)StringUtils.SerializeToJson(target);
            var doc = Encoding.UTF8.GetString(stream.ToArray());

            var returnedObject = (CommentInfo)StringUtils.DeserializeJSONObject(doc, target.GetType());
            Assert.AreEqual(expected, returnedObject.text);
            //var regText = new Regex("\"text\"\\:\"(.*)\",");
            //Assert.IsTrue(regText.IsMatch(doc));
            //MatchCollection matches = regText.Matches(doc);
            //foreach (Match match in matches)
            //{
            //    var actual = match.Value.Substring(8, match.Value.Length - 10);
            //    actual = actual.Replace("\\/", "/");
            //    actual = actual.Replace("/\\", "/");
            //    Assert.AreEqual(expected, actual);
            //}
            
        }
Example #2
0
        /// <summary>
        /// Formats the comment string
        /// </summary>
        /// <param name="text"></param>
        /// <param name="style"></param>
        /// <param name="hidden"></param>
        /// <returns></returns>
        public static string FormatComment(string text, PostStyle.Style style, CommentStatus.Hidden hidden, bool isEditor)
        {
            if (hidden == CommentStatus.Hidden.Hidden_AwaitingPreModeration ||
                hidden == CommentStatus.Hidden.Hidden_AwaitingReferral)
            {
                return "This post is awaiting moderation.";
            }
            if (hidden != CommentStatus.Hidden.NotHidden)
            {
                return "This post has been removed.";
            }
            string _text = text;
            switch (style)
            {
                case Api.PostStyle.Style.plaintext:
                    if (!isEditor)
                    {
                        _text = HtmlUtils.RemoveAllHtmlTags(_text);
                    }
                    _text = HtmlUtils.ReplaceCRsWithBRs(_text);
                    _text = LinkTranslator.TranslateExLinksToHtml(_text);
                    break;

                case Api.PostStyle.Style.richtext:
                    if (!isEditor)
                    {
                        _text = HtmlUtils.CleanHtmlTags(_text, false, false);
                    }
                    _text = HtmlUtils.ReplaceCRsWithBRs(_text);
                    //<dodgey>
                    var temp = "<RICHPOST>" + _text + "</RICHPOST>";
                    temp = HtmlUtils.TryParseToValidHtml(temp);
                    _text = temp.Replace("<RICHPOST>", "").Replace("</RICHPOST>", "");
                    //</dodgey>

                    _text = LinkTranslator.TranslateExLinksToHtml(_text);
                    break;

                case Api.PostStyle.Style.rawtext:
                    //do nothing
                    break;

                case Api.PostStyle.Style.tweet:
                    if (!isEditor)
                    {
                        _text = HtmlUtils.RemoveAllHtmlTags(_text);
                    }
                    _text = LinkTranslator.TranslateExLinksToHtml(_text);
                    _text = LinkTranslator.TranslateTwitterTags(_text);
                    break;

                case Api.PostStyle.Style.unknown:
                    //do nothing
                    break;
            }
            return _text;
        }
Example #3
0
        private void TestTextXmlSerialisation(PostStyle.Style style, string expected, string input, bool isEditor)
        {
            var target = new CommentInfo
            {
                PostStyle = style,
                text = CommentInfo.FormatComment(input, style, CommentStatus.Hidden.NotHidden, isEditor)
            };
            var docExpected = new XmlDocument();
            docExpected.Load(StringUtils.SerializeToXml(expected));
            expected = docExpected.DocumentElement.InnerXml;

            var doc = new XmlDocument();
            doc.Load(target.ToXml());
            Assert.AreEqual(expected, doc.DocumentElement["text"].InnerXml);
        }