Skip to content

Commit

Permalink
Merge pull request #32 from am11/master
Browse files Browse the repository at this point in the history
Code: Added exception for Placehold text
  • Loading branch information
madskristensen committed Feb 17, 2014
2 parents 2a55ade + c08d1d9 commit b7addf4
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 107 deletions.
22 changes: 15 additions & 7 deletions ZenCoding.Test/Html/LoremPixel.cs
Expand Up @@ -14,7 +14,7 @@ public void Initialize()
}

[TestMethod]
public void LoremPixel1()
public void LoremPixelBasic()
{
string result = _parser.Parse("pix", ZenType.HTML);
string expected = "<img src=\"http://lorempixel.com/30/30/\" alt=\"\" />";
Expand All @@ -23,7 +23,7 @@ public void LoremPixel1()
}

[TestMethod]
public void LoremPixel2()
public void LoremPixelGrayScale()
{
string result = _parser.Parse("pix-g", ZenType.HTML);
string expected = "<img src=\"http://lorempixel.com/g/30/30/\" alt=\"\" />";
Expand All @@ -32,7 +32,16 @@ public void LoremPixel2()
}

[TestMethod]
public void LoremPixel3()
public void LoremPixelSingleDimension()
{
string result = _parser.Parse("pix-40", ZenType.HTML);
string expected = "<img src=\"http://lorempixel.com/40/40/\" alt=\"\" />";

Assert.AreEqual(expected, result);
}

[TestMethod]
public void LoremPixelCategory()
{
string result = _parser.Parse("pix-120x1879-sports-SomeRandomText", ZenType.HTML);
string expectedStart = "http://lorempixel.com/120/1879/sports/";
Expand All @@ -43,7 +52,7 @@ public void LoremPixel3()
}

[TestMethod]
public void LoremPixel4()
public void LoremPixelText()
{
string result = _parser.Parse("pix-g-999x1920-animals-SomeRandomText", ZenType.HTML);

Expand All @@ -55,7 +64,7 @@ public void LoremPixel4()
}

[TestMethod]
public void LoremPixel5()
public void LoremPixelOverflowedDimensions()
{
string result = _parser.Parse("pix-20000x3599-SomeRandomText", ZenType.HTML);
string expected = "<img src=\"http://lorempixel.com/790/1678/\" alt=\"\" />"; // the allowed bound is 0-1920
Expand All @@ -64,13 +73,12 @@ public void LoremPixel5()
}

[TestMethod]
public void LoremPixelWithAttributes()
public void LoremPixelWithAttributesWithMarkup()
{
string result = _parser.Parse("pix[alt=\"tag's here\" title=\"picture title\" data-foo=\"bar\"]", ZenType.HTML);
string expected = "<img src=\"http://lorempixel.com/30/30/\" alt=\"tag's here\" title=\"picture title\" data-foo=\"bar\" />";

Assert.AreEqual(expected, result);
}

}
}
18 changes: 9 additions & 9 deletions ZenCoding.Test/Html/Placehold.cs
Expand Up @@ -14,7 +14,7 @@ public void Initialize()
}

[TestMethod]
public void PlaceHold1()
public void PlaceHoldBasic()
{
string result = _parser.Parse("place", ZenType.HTML);
string expected = "<img src=\"http://placehold.it/30x30/\" alt=\"\" />";
Expand All @@ -23,7 +23,7 @@ public void PlaceHold1()
}

[TestMethod]
public void PlaceHold2()
public void PlaceHoldFormat()
{
string result = _parser.Parse("place-30-png", ZenType.HTML);
string expected = "<img src=\"http://placehold.it/30x30/png/\" alt=\"\" />";
Expand All @@ -32,26 +32,26 @@ public void PlaceHold2()
}

[TestMethod]
public void PlaceHold3()
public void PlaceHoldText()
{
string result = _parser.Parse("place-120x1879-jpeg-t=SomeRandomText", ZenType.HTML);
string expected = "http://placehold.it/120x1879/jpeg/&text=SomeRandomText";
string result = _parser.Parse("place-120x1879-jpeg-t=Some%20Random%20Text=", ZenType.HTML);
string expected = "http://placehold.it/120x1879/jpeg/&text=Some%20Random%20Text="; // Text is: "Some Random Text="

StringAssert.Contains(result, expected);
}

[TestMethod]
public void PlaceHold4()
public void PlaceHoldColors()
{
string result = _parser.Parse("place-999x1920-EEE-FFFFFF-t=Some%20Random%20Text", ZenType.HTML);
string result = _parser.Parse("place-999x1920-EEE-FFFFFF-t=Some%20Random%20Text!", ZenType.HTML);

string expected = "http://placehold.it/999x1920/EEE/FFFFFF/&text=Some%20Random%20Text";
string expected = "http://placehold.it/999x1920/EEE/FFFFFF/&text=Some%20Random%20Text!";

StringAssert.Contains(result, expected);
}

[TestMethod]
public void PlaceHold5()
public void PlaceHoldOverflowedDimensions()
{
string result = _parser.Parse("place-20000x3599-t=SomeRandomText", ZenType.HTML);
string expected = "<img src=\"http://placehold.it/790x1678/&text=SomeRandomText\" alt=\"\" />"; // the allowed bound is 0-1920
Expand Down
7 changes: 7 additions & 0 deletions ZenCoding/Commons/IParser.cs
@@ -0,0 +1,7 @@
namespace ZenCoding
{
interface IZenParser
{
string Parse(string zenSyntax);
}
}
2 changes: 1 addition & 1 deletion ZenCoding/Css/CssParser.cs
Expand Up @@ -2,7 +2,7 @@

namespace ZenCoding
{
public class CssParser
public class CssParser : IZenParser
{
public string Parse(string zenSyntax)
{
Expand Down
5 changes: 2 additions & 3 deletions ZenCoding/Helpers/Extensions.cs
@@ -1,5 +1,4 @@

namespace ZenCoding
namespace ZenCoding
{
public static class Extensions
{
Expand All @@ -10,4 +9,4 @@ public static bool IsHex(this char value)
(value >= 'A' && value <= 'F');
}
}
}
}
2 changes: 1 addition & 1 deletion ZenCoding/Html/CustomHtmlAnchor.cs
Expand Up @@ -2,7 +2,7 @@
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace ZenCoding.Html
namespace ZenCoding
{
public class CustomHtmlAnchor : HtmlAnchor
{
Expand Down
2 changes: 1 addition & 1 deletion ZenCoding/Html/CustomHtmlImage.cs
Expand Up @@ -2,7 +2,7 @@
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace ZenCoding.Html
namespace ZenCoding
{
public class CustomHtmlImage : HtmlImage
{
Expand Down
2 changes: 1 addition & 1 deletion ZenCoding/Html/CustomHtmlTextArea.cs
Expand Up @@ -2,7 +2,7 @@
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace ZenCoding.Html
namespace ZenCoding
{
class CustomHtmlTextArea : HtmlTextArea
{
Expand Down
22 changes: 18 additions & 4 deletions ZenCoding/Html/HtmlElementFactory.cs
Expand Up @@ -5,7 +5,6 @@
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ZenCoding.Html;

namespace ZenCoding
{
Expand Down Expand Up @@ -84,6 +83,16 @@ public static HtmlControl Create(string tagName, Type type, bool isClone)
if (tagName == null)
return null;

HtmlControl control = TryCreateSepcialControl(tagName, type, isClone);

if (control != null)
return control;

return new BlockHtmlControl(tagName);
}

private static HtmlControl TryCreateSepcialControl(string tagName, Type type, bool isClone)
{
if (tagName.StartsWith("lorem", System.StringComparison.Ordinal) ||
(type == typeof(LoremControl) && isClone))
{
Expand All @@ -98,6 +107,11 @@ public static HtmlControl Create(string tagName, Type type, bool isClone)
return new PlaceHold(tagName);
}

return TryCreateFromStateMachine(tagName);
}

private static HtmlControl TryCreateFromStateMachine(string tagName)
{
switch (tagName)
{
case "":
Expand Down Expand Up @@ -267,10 +281,10 @@ public static HtmlControl Create(string tagName, Type type, bool isClone)
return new HtmlGenericSelfClosing(tagName);
}

return new BlockHtmlControl(tagName);
return null;
}

public static Control CreateDoctypes(string part, ref List<Control> current)
public static Control CreateDoctypes(string part, ref IEnumerable<Control> current)
{
if (part == null)
return null;
Expand Down Expand Up @@ -306,7 +320,7 @@ public static Control CreateDoctypes(string part, ref List<Control> current)
using (HtmlControl body = HtmlElementFactory.Create("body"))
{
html.Controls.Add(body);
current = new List<Control>() { body };
current = new Control[] { body };
}
return root;
}
Expand Down
32 changes: 17 additions & 15 deletions ZenCoding/Html/HtmlParser.cs
Expand Up @@ -10,7 +10,7 @@

namespace ZenCoding
{
public class HtmlParser
public class HtmlParser : IZenParser
{
private static char[] _attr = new[] { '#', '.', '[', '{' };
private static char[] _elem = new[] { '>', '+', '^' };
Expand All @@ -20,25 +20,27 @@ public class HtmlParser

public static bool IsValid(string zenSyntax)
{
if (zenSyntax == null || zenSyntax.Length == 0 || zenSyntax.StartsWith("asp:", StringComparison.OrdinalIgnoreCase))
if (string.IsNullOrEmpty(zenSyntax) || zenSyntax.StartsWith("asp:", StringComparison.OrdinalIgnoreCase))
return false;

int indexSpace = zenSyntax.IndexOf(' ');

if (indexSpace > -1 && (indexSpace < zenSyntax.IndexOfAny(new[] { '[', '{', '"' }) || indexSpace > zenSyntax.LastIndexOfAny(new[] { ']', '}', '"' })))
return false;

if (zenSyntax.Contains("{") || zenSyntax.Contains("}"))
{
if (zenSyntax.Count(c => c == '{') != zenSyntax.Count(c => c == '}'))
return false;
}
if ((zenSyntax.Contains("{") || zenSyntax.Contains("}")) && (zenSyntax.Count(c => c == '{') != zenSyntax.Count(c => c == '}')))
return false;

if (zenSyntax.Contains("<") || zenSyntax.Contains("|") || zenSyntax.Contains("@"))
return false;

char last = zenSyntax.Last();
if (!char.IsLetterOrDigit(last) && last != ']' && last != '}' && last != '+' && !char.IsWhiteSpace(last))
return false;
if (!zenSyntax.StartsWith("place", StringComparison.CurrentCultureIgnoreCase))
{
char last = zenSyntax.Last();

if (!char.IsLetterOrDigit(last) && last != ']' && last != '}' && last != '+' && !char.IsWhiteSpace(last))
return false;
}

if (zenSyntax.Count(z => z == ']') != zenSyntax.Count(z => z == '['))
return false;
Expand Down Expand Up @@ -114,13 +116,13 @@ public static string ParseGroup(string zenSyntax)
if (!IsValidHtmlElements(parts))
return string.Empty;

List<Control> current = new List<Control>() { root };
IEnumerable<Control> current = new Control[] { root };

HandleDoctypes(ref root, parts, ref current);

if (root == null) return null;

BuildControlTree(CloneStack<string>(parts), current[0], -1);
BuildControlTree(CloneStack<string>(parts), current.First(), -1);

return RenderControl(root);
}
Expand All @@ -133,7 +135,7 @@ public static string ParseGroup(string zenSyntax)
}
}

private static void HandleDoctypes(ref Control root, List<string> parts, ref List<Control> current)
private static void HandleDoctypes(ref Control root, List<string> parts, ref IEnumerable<Control> current)
{
if (parts[0] == "html:4t" || parts[0] == "html:4s" || parts[0] == "html:xt" || parts[0] == "html:xs" || parts[0] == "html:xxs" || parts[0] == "html:5")
{
Expand All @@ -154,7 +156,7 @@ private static void AdjustImplicitTagNames(List<string> parts)
currentDefault = "span";
else if (i != 0 && parts[i - 1] == "table" && parts[i][0] == '>')
currentDefault = "tr";
else if (i != 0 && (parts[i - 1] == "tr" || parts[i - 1].StartsWith(">tr")) && parts[i][0] == '>')
else if (i != 0 && (parts[i - 1] == "tr" || parts[i - 1].StartsWith(">tr", StringComparison.CurrentCultureIgnoreCase)) && parts[i][0] == '>')
currentDefault = "td";
else if ((parts[i][0] == '>' && currentDefault == "td") || (currentDefault != "div" && parts[i][0] == '^'))
currentDefault = "div";
Expand Down Expand Up @@ -415,7 +417,7 @@ private static int GetCountAndName(string part, out string cleanPart, char[] sym
int index = part.IndexOf('*');
int count = 1;

if (index > -1 && part.Length > index && !char.IsNumber(part[index + 1]))
if (index > -1 && part.Length > index + 1 && !char.IsNumber(part[index + 1]))
index = -1;

if (index > -1 && int.TryParse(part.Substring(index + 1), out count))
Expand Down

0 comments on commit b7addf4

Please sign in to comment.