forked from Sitecore/Habitat
/
PrependCssSelectorTransform.cs
116 lines (103 loc) · 3.83 KB
/
PrependCssSelectorTransform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Sitecore.Diagnostics;
using Sitecore.Configuration;
using ExCSS;
using System;
using System.Text;
using System.Web.Optimization;
namespace Sitecore.Feature.Fxm.Bundle
{
public class PrependCssSelectorTransform : IBundleTransform
{
private string _selector;
public void Process(BundleContext context, BundleResponse response)
{
_selector = Settings.GetSetting("Bundle.PrependCssSelectorProcessor.Selector");
try
{
StyleSheet styleSheet = new Parser().Parse(response.Content);
StringBuilder stringBuilder = new StringBuilder();
foreach (RuleSet sheetRule in styleSheet.Rules)
{
try
{
stringBuilder.AppendLine(this.FormatRuleSet(sheetRule));
}
catch (Exception ex)
{
Log.Error("Exception occured", ex, (object)this);
}
}
response.Content = stringBuilder.ToString();
response.ContentType = "text/css";
}
catch (Exception ex)
{
Log.Error("Exception occured in FxmTransform method", ex, this);
throw;
}
}
private string FormatRuleSet(RuleSet sheetRule)
{
string str1 = string.Empty;
if (!(sheetRule is AggregateRule))
return this.FormatRule(sheetRule);
foreach (RuleSet rule in (sheetRule as AggregateRule).RuleSets)
{
string str2 = this.FormatRule(rule);
if (str1 != string.Empty)
str1 += Environment.NewLine;
str1 += str2;
}
if (!(sheetRule is MediaRule))
return str1;
return "@media " + (sheetRule as MediaRule).Media.MediaType + "{" + Environment.NewLine + str1 + Environment.NewLine + "}";
}
private string FormatRule(RuleSet rule)
{
string str2;
if (rule is StyleRule)
{
StyleRule styleRule = rule as StyleRule;
str2 = this.ModifySelector(styleRule.Selector.ToString()) + " {" + Environment.NewLine + this.FormatDeclarations(styleRule.Declarations.ToString()) + "}";
}
else
str2 = rule.ToString();
return str2;
}
private string FormatDeclarations(string declarations)
{
string str1 = string.Empty;
string str2 = declarations.Trim();
foreach (string str3 in str2.Split(';'))
{
if (!string.IsNullOrEmpty(str3.Trim()))
str1 = str1 + str3 + ";" + Environment.NewLine;
}
return str1;
}
private string ModifySelector(string selector)
{
string[] strArray = selector.Split(',');
StringBuilder stringBuilder = new StringBuilder();
int index = 0;
while (index < strArray.Length)
{
string item = strArray[index];
if (!item.Trim().Equals(_selector, StringComparison.InvariantCultureIgnoreCase))
{
stringBuilder.Append(_selector + " " + item);
if (index < checked(strArray.Length - 1))
stringBuilder.Append("," + Environment.NewLine);
}
else
{
stringBuilder.Append(item);
if (index < checked(strArray.Length - 1))
stringBuilder.Append("," + Environment.NewLine);
}
checked { ++index; }
}
return stringBuilder.ToString();
}
}
}