();
- foreach (string key in cssClassesKeys)
- {
- xofficeCssClasses.Add(key);
- }
-
- foreach (string key in xofficeCssClasses)
- {
- cssPropsStr = ((string)cssClasses[key]).Replace('{', ' ').Replace('}', ' ').Trim();
- cssProperties = cssPropsStr.Split(separator, StringSplitOptions.RemoveEmptyEntries);
- cssPropsList.Clear();
- foreach (string property in cssProperties)
- {
- cssPropsList.Add(property.Trim() + ";");
- }
- cssPropsList.Sort();
- cssPropsStr = "{";
- foreach (string property in cssPropsList)
- {
- cssPropsStr += property;
- }
- cssPropsStr += "}";
- cssClasses[key] = cssPropsStr;
- }
-
- //compare CSS classes and group redundant ones
- //using an inverted hash of the cssClasses
- Hashtable invertedHash = new Hashtable(cssClasses.Count);
- foreach (string key in cssClasses.Keys)
- {
- string val = (string)cssClasses[key];
- string cssClass = "";
- if (invertedHash.ContainsKey(val))
- {
- cssClass = (string)invertedHash[val];
- cssClass += ", ";
- }
- cssClass += key;
- invertedHash[val] = cssClass;
- }
- cssClasses.Clear();
- foreach (string properties in invertedHash.Keys)
- {
- string groupedClasses = (string)invertedHash[properties];
- string props = properties.Replace('{', ' ').Replace('}', ' ').Trim();
- cssClasses.Add(groupedClasses, props);
- }
- }
-
private void InsertCssClassesInHeader(ref XmlNode headNode, ref XmlDocument xmlDoc)
{
XmlNode styleNode = xmlDoc.CreateNode(XmlNodeType.Element, "style", xmlDoc.NamespaceURI);
Index: xword/ContentFiltering/Test/Html/CSSUtilTest.cs
===================================================================
--- xword/ContentFiltering/Test/Html/CSSUtilTest.cs (revision 22344)
+++ xword/ContentFiltering/Test/Html/CSSUtilTest.cs (working copy)
@@ -6,6 +6,7 @@
using System.Xml;
using ContentFiltering.Html;
using ContentFiltering.Test.Util;
+using System.Collections;
namespace ContentFiltering.Test.Html
{
@@ -102,7 +103,7 @@
long stopTicks = DateTime.Now.Ticks;
//inline operation duration, in miliseconds
- double inlineTimeMS = (1.0*stopTicks - startTicks) / 10000;
+ double inlineTimeMS = (1.0 * stopTicks - startTicks) / 10000;
//inline operation should take less than 500 miliseconds
@@ -169,6 +170,65 @@
sb.Append("Text with CSS ID ").Append(i).Append("
");
}
return sb.ToString();
- }
+ }
+
+ ///
+ /// Tests the GroupCSSSelectors method.
+ ///
+ [Test]
+ public void TestGroupCSSSelectors()
+ {
+ Hashtable initialCSSClasses = new Hashtable();
+ Hashtable optimizedCSSClasses = new Hashtable();
+
+ //same initial CSS properties
+ string props1 = "font-family:sans-serif;font-size:100%;color:red;";
+ string props2 = "color:red;font-size:100%;font-family:sans-serif;";
+
+ //the expected properties (ordered alphabetically)
+ string expectedProp = "color:red;font-family:sans-serif;font-size:100%";
+
+ //initial CSS selectors
+ string[] classes1 = { ".xoffice0", ".xoffice1", ".xoffice2", "textarea" };
+ string[] classes2 = { ".xoffice3", ".xoffice4", ".myCssClass", "#one", "p#two" };
+
+ for (int i = 0; i < classes1.Length; i++)
+ {
+ initialCSSClasses.Add(classes1[i], props1);
+ }
+
+ for (int i = 0; i < classes2.Length; i++)
+ {
+ initialCSSClasses.Add(classes2[i], props2);
+ }
+
+
+ optimizedCSSClasses = CSSUtil.GroupCSSSelectors(initialCSSClasses);
+
+ ICollection optimizedKeys = optimizedCSSClasses.Keys;
+
+ //all the selectors should be grouped
+ Assert.IsTrue(optimizedKeys.Count == 1);
+
+ foreach (string key in optimizedKeys)
+ {
+ string val = optimizedCSSClasses[key].ToString();
+
+ //in the new key there must be all initial CSS selectors
+ foreach (string cssClass in classes1)
+ {
+ Assert.IsTrue(key.IndexOf(cssClass) >= 0);
+ }
+ foreach (string cssClass in classes2)
+ {
+ Assert.IsTrue(key.IndexOf(cssClass) >= 0);
+ }
+
+ //expected properties are the same, but ordered alphabetically
+ Assert.IsTrue(val.IndexOf(expectedProp) >= 0);
+ }
+
+ }
+
}
}