public void AcbValidationTest()
        {
            MCContext mcContext = new MCContext();
            ParagraphProperties pPr;
            Run run1, run2;
            Paragraph p = new Paragraph(pPr = new ParagraphProperties() { WordWrap = new WordWrap() { Val = true } },
                                         new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comments -->"),
                                         run1 = new Run(new Text("Text 1.")),
                                         run2 = new Run(new Text("Text 2.")));

            p.AddNamespaceDeclaration("w14test", "http://w14.com");

            OpenXmlUnknownElement ignorableElement = new OpenXmlUnknownElement("w14test", "span", "http://w14.com");
            ignorableElement.MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14test" };
            p.InsertAfter(ignorableElement, pPr);

            Run runInAcb = new Run(new Text("Text in ACB."));
            Run run2InAcb = new Run(new Text("Text 2 in ACB."));
            AlternateContent acb = new AlternateContent(new AlternateContentChoice() { Requires = "w14test" },
                                                        new AlternateContentFallback(runInAcb, run2InAcb));
            p.InsertAfter(acb, pPr);

            var validator = new OpenXmlValidator();
            var errors = validator.Validate(p);
            Assert.Equal(0, errors.Count());

            p.AppendChild(new OpenXmlUnknownElement("w15test", "art", "http://w15.com"));
            errors = validator.Validate(p);
            Assert.Equal(1, errors.Count());
            p.RemoveChild(p.LastChild);

            acb.LastChild.Append(new OpenXmlUnknownElement("w15test", "art", "http://w15.com"));
            errors = validator.Validate(p);
            Assert.Equal(1, errors.Count());

        }
Beispiel #2
0
        public void NSDeclTest()
        {
            var p = new Paragraph();

            p.ParagraphId = "123";

            //NamespaceDeclarations is not null
            Assert.Empty(p.NamespaceDeclarations);

            p.AddNamespaceDeclaration("a", "http://b");
            p.SetAttribute(new OpenXmlAttribute("t", "tt", "http://t", "abcd"));

            Assert.Null(p.LookupPrefix("http://t"));
            Assert.Null(p.LookupPrefix("http://schemas.microsoft.com/office/word/2010/wordml\""));

            var xmlcontent = "<w:p xmlns:a=\"http://b\" w14:paraId=\"123\" t:tt=\"abcd\" xmlns:t=\"http://t\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" />";

            Assert.Equal(xmlcontent, p.OuterXml);
            Assert.Single(p.ExtendedAttributes);
            Assert.Single(p.NamespaceDeclarations);

            p = new Paragraph(xmlcontent);
            Assert.Single(p.ExtendedAttributes);
            Assert.Equal(4, p.NamespaceDeclarations.Count());

            Assert.Equal("a", p.LookupPrefix("http://b"));

            Assert.Equal("t", p.LookupPrefix("http://t"));
            Assert.Equal("w14", p.LookupPrefix("http://schemas.microsoft.com/office/word/2010/wordml"));

            p.RemoveNamespaceDeclaration("t");
            Assert.Null(p.LookupPrefix("http://t"));
        }
Beispiel #3
0
        public void Bug704004(FileFormatVersions version)
        {
            var validator = new OpenXmlValidator(version);

            // <w:p>
            //    <ve:AlternateContent />
            //    <w:r>
            //        <w:t>Acb</w:t>
            //        <w:rPr>
            //            <w:rFonts w:hint="eastAsia"/>
            //        </w:rPr>
            //    </w:r>
            // </w:p>
            Paragraph        p   = new Paragraph();
            AlternateContent acb = p.AppendChild(new AlternateContent());

            // one error, w:rPr should before the w:t
            p.AppendChild(new Run(
                              new DocumentFormat.OpenXml.Wordprocessing.Text()
            {
                Text = "Acb"
            },
                              new RunProperties(new RunFonts()
            {
                Hint = FontTypeHintValues.EastAsia
            })));

            // an empty "AlternateContent"
            var errors = validator.Validate(p); // should no hang, no OOM

            Assert.Collection(
                errors.OrderBy(e => e.Id),
                error =>
            {
                Assert.Equal("Sch_IncompleteContentExpectingComplex", error.Id);
                Assert.Same(p.FirstChild, error.Node);      // acb should have a choice
            },
                error =>
            {
                Assert.Equal("Sch_UnexpectedElementContentExpectingComplex", error.Id);
                Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), error.RelatedNode);
            });

            // append an empty "Choice"
            p.AddNamespaceDeclaration("w14", "http://w14");
            acb.AppendChild(new AlternateContentChoice()
            {
                Requires = "w14"
            });
            errors = validator.Validate(p); // should no hang, no OOM
            Assert.Single(errors);
            Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), errors.ElementAt(0).RelatedNode);

            // append an empty "Fallback"
            acb.AppendChild(new AlternateContentFallback());
            errors = validator.Validate(p); // should no hang, no OOM
            Assert.Single(errors);
            Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), errors.ElementAt(0).RelatedNode);
        }
        public void AcbValidationTest()
        {
            MCContext           mcContext = new MCContext();
            ParagraphProperties pPr;
            Run       run1, run2;
            Paragraph p = new Paragraph(
                pPr = new ParagraphProperties()
            {
                WordWrap = new WordWrap()
                {
                    Val = true
                }
            },
                new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comments -->"),
                run1 = new Run(new Text("Text 1.")),
                run2 = new Run(new Text("Text 2.")));

            p.AddNamespaceDeclaration("w14test", "http://w14.com");

            OpenXmlUnknownElement ignorableElement = new OpenXmlUnknownElement("w14test", "span", "http://w14.com");

            ignorableElement.MCAttributes = new MarkupCompatibilityAttributes()
            {
                Ignorable = "w14test"
            };
            p.InsertAfter(ignorableElement, pPr);

            Run runInAcb         = new Run(new Text("Text in ACB."));
            Run run2InAcb        = new Run(new Text("Text 2 in ACB."));
            AlternateContent acb = new AlternateContent(
                new AlternateContentChoice()
            {
                Requires = "w14test"
            },
                new AlternateContentFallback(runInAcb, run2InAcb));

            p.InsertAfter(acb, pPr);

            var validator = new OpenXmlValidator();
            var errors    = validator.Validate(p);

            Assert.Empty(errors);

            p.AppendChild(new OpenXmlUnknownElement("w15test", "art", "http://w15.com"));
            errors = validator.Validate(p);
            Assert.Single(errors);
            p.RemoveChild(p.LastChild);

            acb.LastChild.Append(new OpenXmlUnknownElement("w15test", "art", "http://w15.com"));
            errors = validator.Validate(p);
            Assert.Single(errors);
        }
        public void GetChildMcTest()
        {
            MCContext           mcContext = new MCContext();
            ParagraphProperties pPr;
            Run       run1, run2;
            Paragraph p = new Paragraph(
                pPr = new ParagraphProperties()
            {
                WordWrap = new WordWrap()
                {
                    Val = true
                }
            },
                new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comments -->"),
                run1 = new Run(new Text("Text 1.")),
                run2 = new Run(new Text("Text 2.")));

            var target = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);

            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);

            OpenXmlUnknownElement ignorableElement = new OpenXmlUnknownElement("w14test", "span", "http://w14.com");

            p.AddNamespaceDeclaration("w14test", "http://w14.com");

            ignorableElement.MCAttributes = new MarkupCompatibilityAttributes()
            {
                Ignorable = "w14test"
            };
            p.InsertAfter(ignorableElement, pPr);

            mcContext = new MCContext();
            target    = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);
            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);

            Run runInAcb         = new Run(new Text("Text in ACB."));
            AlternateContent acb = new AlternateContent(
                new AlternateContentChoice()
            {
                Requires = "w14test"
            },
                new AlternateContentFallback(runInAcb));

            p.InsertAfter(acb, pPr);

            mcContext = new MCContext();
            target    = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);
            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(runInAcb, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);
        }
        public void CompatibilityRuleAttributesValidationTest()
        {
            var validator = new SchemaValidator();
            var element   = new Paragraph();
            var run       = new Run();

            element.AppendChild(run);

            var result = validator.Validate(element);

            Assert.Empty(result);

            element.AddNamespaceDeclaration("o15", "http://o15.com");
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes           = new MarkupCompatibilityAttributes();
            run.MCAttributes.Ignorable = "o15";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.AddNamespaceDeclaration("w15", "http://w15.com");
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.Ignorable = "o15 w15";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.PreserveAttributes = "  o15:id w15:*";
            run.MCAttributes.PreserveElements   = "o15:*  w15:*  ";
            run.MCAttributes.ProcessContent     = "  o15:newE   w15:newW  ";
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.PreserveElements = "x15:* ";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);

            run.MCAttributes.Ignorable          = null;
            run.MCAttributes.PreserveAttributes = null;
            run.MCAttributes.PreserveElements   = "w15:*";
            run.MCAttributes.ProcessContent     = string.Empty;
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);

            run.MCAttributes.Ignorable          = "o15";
            run.MCAttributes.PreserveAttributes = string.Empty;
            run.MCAttributes.PreserveElements   = "w15:*";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result[0].Id);
            run.MCAttributes.PreserveElements = null;

            run.MCAttributes.ProcessContent = "w14:newW";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidProcessContentAttribute", result[0].Id);
            run.MCAttributes.ProcessContent = null;

            var spaceAttribute = new OpenXmlAttribute("xml:space", "http://www.w3.org/XML/1998/namespace", "preserve");

            run.MCAttributes.ProcessContent = "o15:newP";
            run.SetAttribute(spaceAttribute);
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttributeWithProcessContent", result[0].Id);
            run.MCAttributes.ProcessContent = null;

            run.SetAttribute(new OpenXmlAttribute("o15:id", "http://o15.com", "1"));
            result = validator.Validate(element);
            Assert.Empty(result);

            run.MCAttributes.Ignorable = "o15 w15 x15";
            result = validator.Validate(element);
            Assert.Single(result);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result[0].ErrorType);
            Assert.Equal("MC_InvalidIgnorableAttribute", result[0].Id);

            run.MCAttributes.Ignorable = "o15 w15";
            run.SetAttribute(new OpenXmlAttribute("x15:id", "http://x15.com", "1"));
            result = validator.Validate(element);
            Assert.Single(result);
        }
        private void Bug704004(OpenXmlValidator validator)
        {
            //<w:p>
            //    <ve:AlternateContent />
            //    <w:r>
            //        <w:t>Acb</w:t>
            //        <w:rPr>
            //            <w:rFonts w:hint="eastAsia"/>
            //        </w:rPr>
            //    </w:r>
            //</w:p>
            Paragraph p = new Paragraph();
            AlternateContent acb = p.AppendChild(new AlternateContent());
            // one error, w:rPr should before the w:t
            p.AppendChild(new Run(new DocumentFormat.OpenXml.Wordprocessing.Text() { Text = "Acb" },
                                  new RunProperties(new RunFonts() { Hint = FontTypeHintValues.EastAsia })
                                  )
                          );

            // an empty "AlternateContent"
            var errors = validator.Validate(p); // should no hang, no OOM
            Assert.Equal(2, errors.Count());
            Assert.Equal("Sch_IncompleteContentExpectingComplex", errors.First().Id);
            Assert.Same(p.FirstChild, errors.First().Node);  // acb should have a choice
            Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), errors.ElementAt(1).RelatedNode);

            // append an empty "Choice"
            p.AddNamespaceDeclaration("w14", "http://w14");
            acb.AppendChild(new AlternateContentChoice() { Requires = "w14" });
            errors = validator.Validate(p); // should no hang, no OOM
            Assert.Equal(1, errors.Count());
            Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), errors.ElementAt(0).RelatedNode);

            // append an empty "Fallback"
            acb.AppendChild(new AlternateContentFallback() );
            errors = validator.Validate(p); // should no hang, no OOM
            Assert.Equal(1, errors.Count());
            Assert.Same(p.FirstChild.NextSibling().FirstChild.NextSibling(), errors.ElementAt(0).RelatedNode);
        }
        public void GetChildMcTest()
        {
            MCContext mcContext = new MCContext();
            ParagraphProperties pPr;
            Run run1, run2;
            Paragraph p = new Paragraph(pPr = new ParagraphProperties() { WordWrap = new WordWrap() { Val = true } },
                                         new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comments -->"),
                                         run1 = new Run(new Text("Text 1.")),
                                         run2 = new Run(new Text("Text 2.")));

            var target = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);
            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);

            OpenXmlUnknownElement ignorableElement = new OpenXmlUnknownElement("w14test", "span", "http://w14.com");
            p.AddNamespaceDeclaration("w14test", "http://w14.com");

            ignorableElement.MCAttributes = new MarkupCompatibilityAttributes() { Ignorable="w14test" };
            p.InsertAfter(ignorableElement, pPr);
            
            mcContext = new MCContext();
            target = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);
            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);

            Run runInAcb = new Run(new Text("Text in ACB."));
            AlternateContent acb = new AlternateContent(new AlternateContentChoice() { Requires = "w14test" },
                                                        new AlternateContentFallback(runInAcb));
            p.InsertAfter(acb, pPr);

            mcContext = new MCContext();
            target = p.GetFirstChildMc(mcContext, FileFormatVersions.Office2007);
            Assert.Same(pPr, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(runInAcb, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run1, target);
            target = p.GetNextChildMc(target, mcContext, FileFormatVersions.Office2007);
            Assert.Same(run2, target);



        }
        public void CompatibilityRuleAttributesValidationTest()
        {
            var validator = new SchemaValidator();
            var element = new Paragraph();
            var run = new Run();
            
            element.AppendChild(run);

            var result = validator.Validate(element);
            Assert.True(result.Valid);

            element.AddNamespaceDeclaration("o15", "http://o15.com");
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.MCAttributes = new MarkupCompatibilityAttributes();
            run.MCAttributes.Ignorable = "o15";
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.AddNamespaceDeclaration("w15", "http://w15.com");
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.MCAttributes.Ignorable = "o15 w15";
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.MCAttributes.PreserveAttributes = "  o15:id w15:*";
            run.MCAttributes.PreserveElements = "o15:*  w15:*  ";
            run.MCAttributes.ProcessContent = "  o15:newE   w15:newW  ";
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.MCAttributes.PreserveElements = "x15:* ";
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result.Errors[0].Id);

            run.MCAttributes.Ignorable = null;
            run.MCAttributes.PreserveAttributes = null;
            run.MCAttributes.PreserveElements = "w15:*";
            run.MCAttributes.ProcessContent = "";
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result.Errors[0].Id);

            run.MCAttributes.Ignorable = "o15";
            run.MCAttributes.PreserveAttributes = "";
            run.MCAttributes.PreserveElements = "w15:*";
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidPreserveElementsAttribute", result.Errors[0].Id);
            run.MCAttributes.PreserveElements = null;

            run.MCAttributes.ProcessContent = "w14:newW";
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidProcessContentAttribute", result.Errors[0].Id);
            run.MCAttributes.ProcessContent = null;

            var spaceAttribute = new OpenXmlAttribute("xml:space", "http://www.w3.org/XML/1998/namespace", "preserve");
            run.MCAttributes.ProcessContent = "o15:newP";
            run.SetAttribute(spaceAttribute);
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidXmlAttributeWithProcessContent", result.Errors[0].Id);
            run.MCAttributes.ProcessContent = null;
            
            run.SetAttribute(new OpenXmlAttribute("o15:id", "http://o15.com", "1"));
            result = validator.Validate(element);
            Assert.True(result.Valid);

            run.MCAttributes.Ignorable = "o15 w15 x15";
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
            Assert.Equal(ValidationErrorType.MarkupCompatibility, result.Errors[0].ErrorType);
            Assert.Equal("MC_InvalidIgnorableAttribute", result.Errors[0].Id);

            run.MCAttributes.Ignorable = "o15 w15";
            run.SetAttribute(new OpenXmlAttribute("x15:id", "http://x15.com", "1"));
            result = validator.Validate(element);
            Assert.False(result.Valid);
            Assert.Equal(1, result.Errors.Count);
        }
        public void NSDeclTest()
        {
            var p = new Paragraph();
            p.ParagraphId = "123";
            //NamespaceDeclarations is not null
            Assert.Equal(0, p.NamespaceDeclarations.Count());

            p.AddNamespaceDeclaration("a", "http://b");
            p.SetAttribute(new OpenXmlAttribute("t", "tt", "http://t", "abcd"));

            Assert.Equal(null, p.LookupPrefix("http://t"));
            Assert.Equal(null, p.LookupPrefix("http://schemas.microsoft.com/office/word/2010/wordml\""));

            var xmlcontent = "<w:p xmlns:a=\"http://b\" w14:paraId=\"123\" t:tt=\"abcd\" xmlns:t=\"http://t\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" />";
            Assert.Equal(xmlcontent, p.OuterXml);
            Assert.Equal(1, p.ExtendedAttributes.Count());
            Assert.Equal(1, p.NamespaceDeclarations.Count());

            p = new Paragraph(xmlcontent);
            Assert.Equal(1, p.ExtendedAttributes.Count());
            Assert.Equal(4, p.NamespaceDeclarations.Count());

            Assert.Equal("a", p.LookupPrefix("http://b"));

            Assert.Equal("t", p.LookupPrefix("http://t"));
            Assert.Equal("w14", p.LookupPrefix("http://schemas.microsoft.com/office/word/2010/wordml"));

            p.RemoveNamespaceDeclaration("t");
            Assert.Equal(null, p.LookupPrefix("http://t"));
        }