Fixed br in ol and ul

This commit is contained in:
Cristian Trapattoni 2011-02-28 18:27:45 +01:00
parent 4056860d9b
commit d9719bccc5
3 changed files with 51 additions and 16 deletions

View file

@ -27,22 +27,22 @@ namespace ScrewTurn.Wiki.Tests {
[TestCase("<div class=\"box\">text</div>", "(((text)))\r\n")] [TestCase("<div class=\"box\">text</div>", "(((text)))\r\n")]
[TestCase("<div>text</div>", "\r\ntext\r\n")] [TestCase("<div>text</div>", "\r\ntext\r\n")]
[TestCase("<html>riga1\r\n<b>riga2</b>\r\nriga3</html>","riga1\r\n'''riga2'''\r\nriga3")] [TestCase("<html>riga1\r\n<b>riga2</b>\r\nriga3</html>","riga1\r\n'''riga2'''\r\nriga3")]
[TestCase("<html><ol><li>1</li><li>2</li><li>3<ol><li>3.1</li><li>3.2<ol><li>3.2.1</li></ol></li><li>3.3</li></ol></li><li>4<br /></li></ol><br /></html>", "# 1\r\n# 2\r\n# 3\r\n## 3.1\r\n## 3.2\r\n### 3.2.1\r\n## 3.3\r\n# 4\r\n# \r\n\r\n\r\n\r\n")] [TestCase("<html><ol><li>1</li><li>2</li><li>3<ol><li>3.1</li><li>3.2<ol><li>3.2.1</li></ol></li><li>3.3</li></ol></li><li>4<br /></li></ol><br /></html>", "# 1\r\n# 2\r\n# 3\r\n## 3.1\r\n## 3.2\r\n### 3.2.1\r\n## 3.3\r\n# 4\r\n\r\n\r\n")]
[TestCase("<ol><li>1</li><li>2</li></ol>", "# 1\r\n# 2\r\n\r\n")] [TestCase("<ol><li>1</li><li>2</li></ol>", "# 1\r\n# 2\r\n")]
[TestCase("<ul><li>1</li><li>2</li></ul>", "* 1\r\n* 2\r\n\r\n")] [TestCase("<ul><li>1</li><li>2</li></ul>", "* 1\r\n* 2\r\n")]
[TestCase("<html><ul><li>Punto 1</li><li>Punto 2</li><li>Punto 3</li><li>Punto 4</li><li>Punto 5</li></ul></html>", "* Punto 1\r\n* Punto 2\r\n* Punto 3\r\n* Punto 4\r\n* Punto 5\r\n\r\n")] [TestCase("<html><ul><li>Punto 1</li><li>Punto 2</li><li>Punto 3</li><li>Punto 4</li><li>Punto 5</li></ul></html>", "* Punto 1\r\n* Punto 2\r\n* Punto 3\r\n* Punto 4\r\n* Punto 5\r\n")]
[TestCase("<ul><li>it 1<ul><li>1.1</li><li>1.2</li></ul></li><li>it2</li></ul>", "* it 1\r\n** 1.1\r\n** 1.2\r\n* it2\r\n\r\n")] [TestCase("<ul><li>it 1<ul><li>1.1</li><li>1.2</li></ul></li><li>it2</li></ul>", "* it 1\r\n** 1.1\r\n** 1.2\r\n* it2\r\n")]
[TestCase("<ul><li>it 1<ol><li>1.1</li><li>1.2</li></ol></li><li>it2</li></ul>", "* it 1\r\n*# 1.1\r\n*# 1.2\r\n* it2\r\n\r\n")] [TestCase("<ul><li>it 1<ol><li>1.1</li><li>1.2</li></ol></li><li>it2</li></ul>", "* it 1\r\n*# 1.1\r\n*# 1.2\r\n* it2\r\n")]
[TestCase("<ul><li><b>1</b></li><li>2</li></ul>", "* '''1'''\r\n* 2\r\n\r\n")] [TestCase("<ul><li><b>1</b></li><li>2</li></ul>", "* '''1'''\r\n* 2\r\n")]
[TestCase("<html><a id=\"Init\" />I'm an anchor</html>", "[anchor|#init]I'm an anchor")] [TestCase("<html><a id=\"Init\" />I'm an anchor</html>", "[anchor|#init]I'm an anchor")]
[TestCase("<html><a class=\"internallink\" href=\"#init\" title=\"This recall an anchor\">This recall an anchor</a></html>", "[#init|This recall an anchor]")] [TestCase("<html><a class=\"internallink\" href=\"#init\" title=\"This recall an anchor\">This recall an anchor</a></html>", "[#init|This recall an anchor]")]
[TestCase("<html><a class=\"externallink\" href=\"google.com\" title=\"BIG TITLE\" target=\"_blank\">BIG TITLE</a></html>", "[^google.com|BIG TITLE]")] [TestCase("<html><a class=\"externallink\" href=\"google.com\" title=\"BIG TITLE\" target=\"_blank\">BIG TITLE</a></html>", "[^google.com|BIG TITLE]")]
[TestCase("<esc>try to esc tag</esc>", "<esc>try to esc tag</esc>")] [TestCase("<esc>try to esc tag</esc>", "<esc>try to esc tag</esc>")]
[TestCase("<div class=\"imageleft\"><a target=\"_blank\" href=\"www.link.com\" title=\"left Align\"><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"left Align\" /></a><p class=\"imagedescription\">leftalign</p></div>", "[imageleft|leftalign|{UP(MainPage)}image.png|^www.link.com]\r\n")] [TestCase("<div class=\"imageleft\"><a target=\"_blank\" href=\"www.link.com\" title=\"left Align\"><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"left Align\" /></a><p class=\"imagedescription\">leftalign</p></div>", "[imageleft|leftalign|{UP(MainPage)}image.png|^www.link.com]\r\n")]
[TestCase("<img src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"inlineimage\" />", "[image|inlineimage|{UP(MainPage)}image.png]\r\n")] [TestCase("<img src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"inlineimage\" />", "[image||{UP(MainPage)}image.png]\r\n")]
[TestCase("<a target=\"_blank\" href=\"www.google.it\" title=\"description\"><img src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"description\" /></a>", "[image|description|{UP(MainPage)}image.png|^www.google.it]\r\n")] [TestCase("<a target=\"_blank\" href=\"www.google.it\" title=\"description\"><img src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"description\" /></a>", "[image||{UP(MainPage)}image.png|^www.google.it]\r\n")]
[TestCase("<table class=\"imageauto\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"autoalign\" /><p class=\"imagedescription\">autoalign</p></td></tr></tbody></table>", "[imageauto|autoalign|{UP(MainPage)}image.png]\r\n")] [TestCase("<table class=\"imageauto\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"autoalign\" /><p class=\"imagedescription\">autoalign</p></td></tr></tbody></table>", "[imageauto|autoalign|{UP(MainPage)}image.png]\r\n")]
[TestCase("<table class=\"imageauto\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><a href=\"www.link.com\" title=\"Auto align\"><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"Auto align\" /></a><p class=\"imagedescription\">Auto align</p></td></tr></tbody></table>", "[imageauto|Auto align|{UP(MainPage)}image.png|www.link.com]\r\n")] [TestCase("<table class=\"imageauto\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><a href=\"www.link.com\" title=\"Auto align\"><img class=\"image\" src=\"GetFile.aspx?Page=MainPage&File=image.png\" alt=\"Auto align\" /></a><p class=\"imagedescription\">Auto align</p></td></tr></tbody></table>", "[imageauto||{UP(MainPage)}image.png|www.link.com]\r\n")]
public void PlainTest(string input, string output) { public void PlainTest(string input, string output) {
Assert.AreEqual(output, ReverseFormatter.ReverseFormat(input)); Assert.AreEqual(output, ReverseFormatter.ReverseFormat(input));
} }

View file

@ -1207,7 +1207,7 @@ namespace ScrewTurn.Wiki {
/// <param name="sb">The <see cref="T:StringBuilder" /> containing the text to process.</param> /// <param name="sb">The <see cref="T:StringBuilder" /> containing the text to process.</param>
/// <param name="bareBones">A value indicating whether the formatting is being done in bare-bones mode.</param> /// <param name="bareBones">A value indicating whether the formatting is being done in bare-bones mode.</param>
private static void ProcessLineBreaks(StringBuilder sb, bool bareBones) { private static void ProcessLineBreaks(StringBuilder sb, bool bareBones) {
if(bareBones || AreSingleLineBreaksToBeProcessed()) { if(AreSingleLineBreaksToBeProcessed()) {
// Replace new-lines only when not enclosed in <nobr> tags // Replace new-lines only when not enclosed in <nobr> tags
Match match = NoSingleBr.Match(sb.ToString()); Match match = NoSingleBr.Match(sb.ToString());
while(match.Success) { while(match.Success) {
@ -1234,9 +1234,9 @@ namespace ScrewTurn.Wiki {
match = NoSingleBr.Match(sb.ToString(), match.Index + 1); match = NoSingleBr.Match(sb.ToString(), match.Index + 1);
} }
sb.Replace("\n", "<br />"); sb.Replace("\n\n", "<br /><br />");
sb.Replace(SingleBrPlaceHolder, "\n"); sb.Replace(SingleBrPlaceHolder, "<br /><br />");
} }

View file

@ -15,6 +15,26 @@ namespace ScrewTurn.Wiki {
/// </summary> /// </summary>
public static class ReverseFormatter { public static class ReverseFormatter {
/// <summary>
/// Searches the description.
/// </summary>
/// <param name="nodes">The nodes.</param>
/// <returns></returns>
private static string searchDescription (XmlNodeList nodes){
string description = "";
foreach (XmlNode n in nodes){
if(n.Name.ToLowerInvariant() == "p") {
foreach(XmlAttribute att in n.Attributes) {
if(att.Value.ToLowerInvariant().ToString() == "imagedescription")
description += processChild(n.ChildNodes);
}
}
}
return description;
}
/// <summary> /// <summary>
/// Processes order or unorder lists and sublists. /// Processes order or unorder lists and sublists.
/// </summary> /// </summary>
@ -28,6 +48,11 @@ namespace ScrewTurn.Wiki {
foreach(XmlNode node in nodes) { foreach(XmlNode node in nodes) {
if(node.Name.ToString() == "li") { if(node.Name.ToString() == "li") {
foreach(XmlNode child in node.ChildNodes) { foreach(XmlNode child in node.ChildNodes) {
if(child.NodeType == XmlNodeType.Text) {
string ie = child.Value.ToString();
ie = ie.Replace("\r", string.Empty).Replace("\n", string.Empty);
child.Value = ie;
}
switch(child.Name.ToString()) { switch(child.Name.ToString()) {
case "ol": case "ol":
result += processList(child.ChildNodes, marker + ol); result += processList(child.ChildNodes, marker + ol);
@ -35,6 +60,11 @@ namespace ScrewTurn.Wiki {
case "ul": case "ul":
result += processList(child.ChildNodes, marker + ul); result += processList(child.ChildNodes, marker + ul);
break; break;
case "br":
StringReader aa = new StringReader(child.OuterXml);
XmlDocument ne = FromHTML((TextReader)aa);
result += processChild(ne.ChildNodes);
break;
default: default:
StringReader a = new StringReader(child.OuterXml); StringReader a = new StringReader(child.OuterXml);
XmlDocument n = FromHTML((TextReader)a); XmlDocument n = FromHTML((TextReader)a);
@ -76,10 +106,12 @@ namespace ScrewTurn.Wiki {
string p = ""; string p = "";
string url = ""; string url = "";
string result = ""; string result = "";
bool hasDescription = false;
foreach(XmlNode node in nodes) { foreach(XmlNode node in nodes) {
if(node.Name.ToLowerInvariant() == "img") if(node.Name.ToLowerInvariant() == "img")
image += processImage(node); image += processImage(node);
else if(node.Name.ToLowerInvariant() == "p") { else if(node.Name.ToLowerInvariant() == "p") {
hasDescription = true;
p += "|" + processChild(node.ChildNodes) + "|"; p += "|" + processChild(node.ChildNodes) + "|";
} }
else if(node.Name.ToLowerInvariant() == "a") { else if(node.Name.ToLowerInvariant() == "a") {
@ -99,6 +131,8 @@ namespace ScrewTurn.Wiki {
url = "|" + target + link; url = "|" + target + link;
} }
} }
if (!hasDescription)
p = "||";
result = p + image + url; result = p + image + url;
return result; return result;
} }
@ -203,10 +237,10 @@ namespace ScrewTurn.Wiki {
result += processChild(node.ChildNodes); result += processChild(node.ChildNodes);
break; break;
case "ol": case "ol":
result += processList(node.ChildNodes, "#") + "\r\n"; result += processList(node.ChildNodes, "#");
break; break;
case "ul": case "ul":
result += processList(node.ChildNodes, "*") + "\r\n"; result += processList(node.ChildNodes, "*");
break; break;
case "li": case "li":
result += processChild(node.ChildNodes); result += processChild(node.ChildNodes);
@ -257,7 +291,8 @@ namespace ScrewTurn.Wiki {
if(node.Attributes.Count != 0) { if(node.Attributes.Count != 0) {
foreach(XmlAttribute attName in node.Attributes) { foreach(XmlAttribute attName in node.Attributes) {
if(attName.Name.ToString() == "alt") if(attName.Name.ToString() == "alt")
description = attName.Value.ToString(); description = searchDescription(node.ParentNode.ChildNodes);
//description = attName.Value.ToString();
if(attName.Name.ToString() == "class") if(attName.Name.ToString() == "class")
hasClass = true; hasClass = true;
} }