private DocumentText CheckDocumentWithStyles(List<TriggeringNodeDefinition> existingTriggers, string stylesheet_xml, string document_xml, out XmlDocument xmlDoc) { using (Stream from = File.Open(stylesheet_xml, FileMode.Open)) { StyleSheet sr = new StyleSheet(new CommonNamespaces(OpenXmlFormat.Transitional)); sr.ConnectToInputStream(from); sr.Execute(); sr.Resolve(); List<TriggeringNodeDefinition> newTriggers = sr.GenerateStyleTriggers(existingTriggers); foreach (TriggeringNodeDefinition tnd in newTriggers) { existingTriggers.Add(tnd); } } return CheckDocument(existingTriggers, document_xml, out xmlDoc); }
public void TestResolve() { using (Stream from = File.Open(TESTFILE_DIR + "styles.xml", FileMode.Open)) { StyleSheet sr = new StyleSheet(new CommonNamespaces(OpenXmlFormat.Transitional)); sr.ConnectToInputStream(from); sr.Execute(); foreach (Style st in sr.Styles) { Assert.IsFalse(st.Resolved); } sr.Resolve(); foreach (Style st2 in sr.Styles) { Assert.IsTrue(st2.Resolved, "didnt resolve style " + st2.Name); } Style s = sr.GetStyle("CommentSubjectChar"); Assert.IsNotNull(s); Assert.IsTrue(s.HasProperty("sz")); Assert.IsTrue(s.HasProperty("rFonts")); Assert.IsTrue(s.HasProperty("b"), "missing the bold property - no inheritance?"); Assert.IsTrue(s.HasProperty("bCs")); Assert.AreEqual("20", s.GetProperty("sz").GetAttributeValue("val")); Assert.AreEqual("", s.GetProperty("rFonts").GetAttributeValue("val")); } }
public void TestDiscoverRealHiddenTextWithStyles() { List<TriggeringNodeDefinition> existingTriggers = DocxMetadataDefinitions.HiddenDocumentText; using (Stream from = File.Open(TESTFILE_DIR + "hidden_styles.xml", FileMode.Open)) { StyleSheet sr = new StyleSheet(new CommonNamespaces(OpenXmlFormat.Transitional)); sr.ConnectToInputStream(from); sr.Execute(); sr.Resolve(); List<TriggeringNodeDefinition> newTriggers = sr.GenerateStyleTriggers(existingTriggers); foreach (TriggeringNodeDefinition tnd in newTriggers) { existingTriggers.Add(tnd); } } using (Stream from2 = File.Open(TESTFILE_DIR + "hidden_styles_document.xml", FileMode.Open)) { StateMachineBasedXmlFilter xfb = new StateMachineBasedXmlFilter(new CommonNamespaces(OpenXmlFormat.Transitional)); xfb.ConnectToInputStream(from2); foreach (TriggeringNodeDefinition tnd in existingTriggers) { xfb.AddNodeToTriggerList(tnd); } DocumentText dt = xfb.DocumentText; xfb.Execute(); TextType ttHidden = dt.GetTextTypes(ContentType.HiddenText)[0] as TextType; Assert.IsNotNull(ttHidden); Assert.IsTrue(ttHidden.GetChildCount() > 0); Assert.AreEqual("This is some text in the document in a hidden paragraph.", ttHidden.GetChild(0).GetInfo("Content")[0].value); Assert.AreEqual("This para contains ", ttHidden.GetChild(1).GetInfo("Content")[0].value); Assert.AreEqual(" text.", ttHidden.GetChild(2).GetInfo("Content")[0].value); Assert.AreEqual("This hidden para contains ", ttHidden.GetChild(3).GetInfo("Content")[0].value); Assert.AreEqual(" text.", ttHidden.GetChild(4).GetInfo("Content")[0].value); Assert.AreEqual("This text in hidden style with ", ttHidden.GetChild(5).GetInfo("Content")[0].value); Assert.AreEqual("embedded.", ttHidden.GetChild(6).GetInfo("Content")[0].value); Assert.AreEqual("This text in hidden style with ", ttHidden.GetChild(7).GetInfo("Content")[0].value); Assert.AreEqual(" embedded.", ttHidden.GetChild(8).GetInfo("Content")[0].value); } }
public void TestCleanRealHiddenTextWithStyles() { List<TriggeringNodeDefinition> existingTriggers = DocxMetadataDefinitions.HiddenDocumentText; using (Stream from = File.Open(TESTFILE_DIR + "hidden_styles.xml", FileMode.Open)) { StyleSheet sr = new StyleSheet(new CommonNamespaces(OpenXmlFormat.Transitional)); sr.ConnectToInputStream(from); sr.Execute(); sr.Resolve(); List<TriggeringNodeDefinition> newTriggers = sr.GenerateStyleTriggers(existingTriggers); foreach (TriggeringNodeDefinition tnd in newTriggers) { existingTriggers.Add(tnd); } } using (Stream from2 = File.Open(TESTFILE_DIR + "hidden_styles_document.xml", FileMode.Open)) { StateMachineBasedXmlFilter xfb = new StateMachineBasedXmlFilter(new CommonNamespaces(OpenXmlFormat.Transitional)); xfb.ConnectToInputStream(from2); MemoryStream ms = new MemoryStream(); xfb.ConnectToOutputStream(ms); foreach (TriggeringNodeDefinition tnd in existingTriggers) { xfb.AddNodeToTriggerList(tnd); } DocumentText dt = xfb.DocumentText; xfb.Execute(); ms.Position = 0; XmlDocument doc = new XmlDocument(); doc.Load(ms); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); CommonNamespaces cns = xfb.CommonNamespaces; nsmgr.AddNamespace("w", cns.GetAtomicName(NamespaceId.w)); XmlNodeList nodes = doc.SelectNodes("//w:r/w:t", nsmgr); StringBuilder bld = new StringBuilder(); foreach (XmlNode node in nodes) { bld.Append(node.InnerText); } Assert.AreEqual("non-hiddennon-hidden styleThis para non-hidden. unhidden text anti-hidden style", bld.ToString(), "oops, didn't clean correctly - probably lots to do with w:r's going missing"); DocxTestUtilities.ValidateDocxMainStream(doc, TESTFILE_DIR); } }
public void TestGenerateStyleTriggers() { List<TriggeringNodeDefinition> existingTriggers = DocxMetadataDefinitions.HiddenDocumentText; using (Stream from = File.Open(TESTFILE_DIR + "styles.xml", FileMode.Open)) { StyleSheet sr = new StyleSheet(new CommonNamespaces(OpenXmlFormat.Transitional)); sr.ConnectToInputStream(from); sr.Execute(); sr.Resolve(); foreach (TriggeringNodeDefinition tnd in existingTriggers) { tnd.NormalizeStrings(sr.CommonNamespaces.NameTable); } List<TriggeringNodeDefinition> newTriggers = sr.GenerateStyleTriggers(existingTriggers); Assert.AreEqual(2, newTriggers.Count); TriggeringNodeDefinition tnd1 = newTriggers[0]; Assert.AreEqual(NamespaceId.w, tnd1.namespaceId); Assert.AreEqual("rStyle", tnd1.nodeName); Assert.IsTrue(tnd1.attributeFilter.Matches(new AttribDetails("", "val", "", "FooterCharHidden"))); { MemoryStream ms = new MemoryStream(); StreamWriter tw = new StreamWriter(ms); tw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:rStyle xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:val=\"FooterCharHidden\"/>"); tw.Flush(); ms.Position = 0; XmlReaderSettings set = new XmlReaderSettings(); set.NameTable = sr.CommonNamespaces.NameTable; set.ConformanceLevel = ConformanceLevel.Fragment; XmlReader r = XmlReader.Create(ms, set); r.Read(); XmlNodeInformation xni = new XmlNodeInformation(r, sr.CommonNamespaces); TriggerCollection snui = new TriggerCollection(newTriggers); snui.UpdateStringsUsingNametable(set.NameTable); Assert.AreEqual(1, snui.WhatDoesThisNodeTrigger(xni, null).Count); TriggeringNodeDefinition tnd = snui.WhatDoesThisNodeTrigger(xni, null)[0]; Assert.AreEqual("rStyle", tnd.nodeName); Assert.IsTrue(tnd.attributeFilter.Matches(new AttribDetails("", NameTableUtils.NormalizeString(sr.CommonNamespaces.NameTable,"val") , "", "FooterCharHidden"))); Assert.AreEqual(NamespaceId.w, tnd.namespaceId); Assert.AreEqual(1, tnd.CreateEffects( xni).Count); Effect t = tnd.CreateEffects(xni)[0]; Assert.AreEqual(ContentType.HiddenText, t.ContentType); Assert.AreEqual(true, t.BlockPolarity, "expect the polairty of blocking to be +ve as this is a style with hidden on"); } { MemoryStream ms = new MemoryStream(); StreamWriter tw = new StreamWriter(ms); tw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:rStyle xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:val=\"FooterCharNotHidden\"/>"); tw.Flush(); ms.Position = 0; XmlReaderSettings set = new XmlReaderSettings(); set.NameTable = sr.CommonNamespaces.NameTable; set.ConformanceLevel = ConformanceLevel.Fragment; XmlReader r = XmlReader.Create(ms, set); XmlNodeInformation xni = new XmlNodeInformation(r, sr.CommonNamespaces); TriggerCollection snui = new TriggerCollection(newTriggers); Assert.AreEqual(1, snui.WhatDoesThisNodeTrigger(xni, null).Count); TriggeringNodeDefinition tnd = snui.WhatDoesThisNodeTrigger(xni, null)[0]; Assert.AreEqual("rStyle", tnd.nodeName); Assert.IsTrue(tnd.attributeFilter.Matches(new AttribDetails("", NameTableUtils.NormalizeString(sr.CommonNamespaces.NameTable, "val") , "", "FooterCharNotHidden"))); Assert.AreEqual(NamespaceId.w, tnd.namespaceId); Assert.AreEqual(1, tnd.CreateEffects( xni).Count); Effect t = tnd.CreateEffects( xni)[0]; Assert.AreEqual(ContentType.HiddenText, t.ContentType); Assert.AreEqual(false, t.BlockPolarity, "should have -ve polarity because the style specifies not hidden"); } Assert.AreEqual(true, existingTriggers[0].GetDescriptors()[0].DefaultPolarity, "Should not change the default polarity of the base triggerdescriptor for the non-style tag"); } }