Skip to content

Commit

Permalink
0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed Aug 24, 2019
1 parent 9083e1d commit 787c937
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 47 deletions.
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ parts that are relevant to CSHTML tokenization is supported:
* Variables in attributes @
* Statements in attributes @(...)

## CSS

* Classes
* Declarations

## Usage

```
Expand Down
15 changes: 2 additions & 13 deletions src/CSHTMLTokenizer.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ internal class Program
{
private static void Main(string[] args)
{
string str = @"color: red;
font-family: ""open sans"", serif;
h1 {
color: pink;
}
h2 { color: blue; }
@media only screen and (min-width: 320px) and (max-width: 480px) {
h1 {
color: green;
}
string str = @"@media only screen and (min-width: 320px) and (max-width: 480px) {
color: green;
}";

List<Line> lines = Tokenizer.Parse(str);
Expand Down
16 changes: 0 additions & 16 deletions src/CSHTMLTokenizer.Test/CSHTMLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,6 @@ public void ForLoopError()
Assert.AreEqual(9, lines.Count);
}

[TestMethod]
public void TestBraces()
{
string str = @"<Styled @bind-Classname=""@hover"">
&:hover {
color: @color;
}
</Styled>";
List<Line> lines = Tokenizer.Parse(str);
Assert.AreEqual(5, lines.Count);
Assert.AreEqual(TokenType.Text, lines[1].Tokens[0].TokenType);
Assert.AreEqual("&:hover {", ((Text)lines[1].Tokens[0]).Content.Trim());
Assert.AreEqual(TokenType.Text, lines[3].Tokens[0].TokenType);
Assert.AreEqual("}", ((Text)lines[3].Tokens[0]).Content.Trim());
}

[TestMethod]
public void TestBlazotStrap()
{
Expand Down
43 changes: 43 additions & 0 deletions src/CSHTMLTokenizer.Test/CSSTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,48 @@ public void TestCss()
Assert.AreEqual(TokenType.CSSCloseClass, lines[7].Tokens[3].TokenType);

}

[TestMethod]
public void EnsureNoCSStartBlock()
{
string str = @"@media only screen and (min-width: 320px) and (max-width: 480px) {
color: green;
}";

List<Line> lines = Tokenizer.Parse(str);
Assert.AreEqual(3, lines.Count);
Assert.AreEqual(2, lines[0].Tokens.Count);
Assert.AreEqual(TokenType.CSSOpenClass, lines[0].Tokens[0].TokenType);
Assert.IsTrue(((CSSOpenClass)lines[0].Tokens[0]).Content.Trim().StartsWith("@"));
}

[TestMethod]
public void TestCSSClass()
{
string str = @"<Styled @bind-Classname=""@hover"">
&:hover {
color: @color;
}
</Styled>";
List<Line> lines = Tokenizer.Parse(str);
Assert.AreEqual(5, lines.Count);
Assert.AreEqual(TokenType.CSSOpenClass, lines[1].Tokens[0].TokenType);
Assert.AreEqual("&:hover", ((CSSOpenClass)lines[1].Tokens[0]).Content.Trim());
Assert.AreEqual(TokenType.CSSCloseClass, lines[3].Tokens[1].TokenType);
}

[TestMethod]
public void EnsureNoCSSCloseTag()
{
string str = @"@code {
private void onclick()
{
_showBig = !_showBig;
}
}";
List<Line> lines = Tokenizer.Parse(str);
Assert.AreEqual(6, lines.Count);
Assert.AreEqual(TokenType.CSBlockEnd, lines[5].Tokens[0].TokenType);
}
}
}
2 changes: 1 addition & 1 deletion src/CSHTMLTokenizer/CSHTMLTokenizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<IsPackable>true</IsPackable>
<LangVersion>7.3</LangVersion>
<PackageId>CSHTMLTokenizer</PackageId>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<Authors>Chanan Braunstein</Authors>
<Title>CSHTML Tokenizer</Title>
<Description>Tokenize CSHTML</Description>
Expand Down
114 changes: 98 additions & 16 deletions src/CSHTMLTokenizer/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,27 +243,79 @@ private List<Line> ParseRazor(string str)

private void ParseCSS()
{
int openClasses = 0;
//CssOpenClass
bool inCodeBlock = false;
foreach (Line line in Lines)
{
int foundColonIndex = -1;
int foundColonToken = -1;
int tokenNum = 0;
foreach (IToken token in line.Tokens)
{
if (token.TokenType == TokenType.Text)
if(token.TokenType == TokenType.CSBlockStart)
{
CSBlockStart cs = (CSBlockStart)token;
if (cs.IsCode || cs.IsFunctions || cs.IsOpenBrace)
{
inCodeBlock = true;
}
}
if (token.TokenType == TokenType.CSBlockEnd)
{
inCodeBlock = false;
}
if (token.TokenType == TokenType.Text && !inCodeBlock)
{
Text text = (Text)token;
if (text.Content.IndexOf("{") != -1)
{
openClasses++;
line.Tokens = ParseOpenClass(line);
}
}
}
}
//CssCloseClass
inCodeBlock = false;
int openClasses = 0;
foreach (Line line in Lines)
{
foreach (IToken token in line.Tokens)
{
if (token.TokenType == TokenType.CSBlockStart)
{
CSBlockStart cs = (CSBlockStart)token;
if(cs.IsCode || cs.IsFunctions || cs.IsOpenBrace)
{
inCodeBlock = true;
}
}
if (token.TokenType == TokenType.CSBlockEnd)
{
inCodeBlock = false;
}
if (token.TokenType == TokenType.CSSOpenClass && !inCodeBlock)
{
openClasses++;
}
if (token.TokenType == TokenType.Text && !inCodeBlock)
{
Text text = (Text)token;
if (text.Content.IndexOf("}") != -1 && openClasses > 0)
{
openClasses--;
line.Tokens = ParseCloseClass(line);
}
}
}
}
//Css Declerations
foreach (Line line in Lines)
{
int foundColonIndex = -1;
int foundColonToken = -1;
int tokenNum = 0;
foreach (IToken token in line.Tokens)
{
if (token.TokenType == TokenType.Text)
{
Text text = (Text)token;
if (text.Content.IndexOf(':') != -1 && foundColonIndex == -1)
{
foundColonIndex = text.Content.IndexOf(':');
Expand Down Expand Up @@ -315,24 +367,54 @@ private List<IToken> ParseOpenClass(Line line)
{
List<IToken> list = new List<IToken>();
bool found = false;
foreach (IToken token in line.Tokens)
bool previousCS = false;
bool doesNotContainCS = line.Tokens.Where(t => t.TokenType == TokenType.CSBlockStart).Count() == 0;
for (int i = 0; i < line.Tokens.Count; i++)
{
IToken token = line.Tokens[i];
if (token.TokenType == TokenType.Text && !found && ((Text)token).Content.IndexOf("{") != -1)
{
found = true;
Text text = (Text)token;
CSSOpenClass cssOpenClass = new CSSOpenClass(text.Content.Substring(0, text.Content.IndexOf("{")));
list.Add(cssOpenClass);
if (text.Content.IndexOf("{") < text.Content.Length)
if (doesNotContainCS || (previousCS && ((Text)token).Content.Trim().StartsWith("media") ||
((Text)token).Content.Trim().StartsWith("font") ||
((Text)token).Content.Trim().StartsWith("keyframe")))
{
string rest = text.Content.Substring(text.Content.IndexOf("{") + 1);
Text textToken = new Text(rest);
list.Add(textToken);
found = true;
Text text = (Text)token;
string prefix = previousCS ? "@" : string.Empty;
previousCS = false;
CSSOpenClass cssOpenClass = new CSSOpenClass(prefix + text.Content.Substring(0, text.Content.IndexOf("{")));
list.Add(cssOpenClass);
if (text.Content.IndexOf("{") < text.Content.Length)
{
string rest = text.Content.Substring(text.Content.IndexOf("{") + 1);
Text textToken = new Text(rest);
list.Add(textToken);
}
}
else
{
list.Add(token);
previousCS = false;
}
}
else
{
list.Add(token);
if (token.TokenType == TokenType.CSBlockStart &&
i != line.Tokens.Count - 1 &&
line.Tokens[i + 1].TokenType == TokenType.Text &&
(
((Text)line.Tokens[i + 1]).Content.Trim().StartsWith("media") ||
((Text)line.Tokens[i + 1]).Content.Trim().StartsWith("font") ||
((Text)line.Tokens[i + 1]).Content.Trim().StartsWith("keyframe")
)
)
{
previousCS = true;
}
else
{
list.Add(token);
}
}
}
return list;
Expand Down
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSBlockEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public string ToHtml()
{
return "}";
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSBlockStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public string ToHtml()

return sb.ToString();
}

public override string ToString()
{
return ToHtml();
}
}
}
4 changes: 4 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ private string GetLineType(CSLineType lineType)
default:
return "";
}
}

public override string ToString()
{
return ToHtml();
}
}
}
7 changes: 6 additions & 1 deletion src/CSHTMLTokenizer/Tokens/CSSCloseClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public void Append(char ch)

public string ToHtml()
{
return ";";
return "}";
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSSOpenClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public string ToHtml()
{
return _content.ToString() + " {";
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSSProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public string ToHtml()
{
return _content.ToString();
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/CSSValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public void Add(IToken token)
{
Tokens.Add(token);
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/EndTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public void Append(char ch)
{
_name.Append(ch);
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ public void Add(IToken token)
{
Tokens.Add(token);
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/QuotedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ private string GetQuoteChar()
return string.Empty;
}
}

public override string ToString()
{
return ToHtml();
}
}
}
5 changes: 5 additions & 0 deletions src/CSHTMLTokenizer/Tokens/StartTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ public void Append(char ch)
{
_name.Append(ch);
}

public override string ToString()
{
return ToHtml();
}
}
}

0 comments on commit 787c937

Please sign in to comment.