public void TestParseAsOpenXML_LinkToURLWithName()
        {
            string testWikiContent = @"This is a test of a [Awesome](http://www.awesome.com)";

            OpenXmlElement body = testWikiContent.MarkdownToOpenXml(this.tdb, this.mainPart);

            Assert.AreEqual(1, body.ChildElements.Count);
            Paragraph para = body.ChildElements[0] as Paragraph;

            Assert.IsNotNull(para);

            Run run = para.ChildElements[1] as Run;

            Assert.IsNotNull(run);
            AssertRunText(run, "This is a test of a ");

            OpenXmlMiscNode comment = para.ChildElements[2] as OpenXmlMiscNode;

            Assert.IsNotNull(comment);

            Hyperlink hyperlink = para.ChildElements[3] as Hyperlink;

            Assert.IsNotNull(hyperlink);
            Run hyperlinkRun = hyperlink.LastChild as Run;

            Assert.IsNotNull(hyperlinkRun);
            AssertRunText(hyperlinkRun, "Awesome");

            AssertOpenXmlValid(body);
        }
示例#2
0
        public void WriteStartElementWithMisc()
        {
            var node = new OpenXmlMiscNode(XmlNodeType.Comment);

            using var miscReader = OpenXmlReader.Create(node, true);
            Assert.True(miscReader.Read());
            Assert.Throws <ArgumentOutOfRangeException>(() => TestWriteStartElement(WConstrWithStream, WriteStartE, miscReader, null, null));
        }
示例#3
0
        public void Bug253893_Write2Declaration()
        {
            using var stream = new MemoryStream();
            using var doc    = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);

            var mainpart = doc.AddMainDocumentPart();

            var writer = OpenXmlWriter.Create(mainpart);

            var miscnode = new OpenXmlMiscNode(XmlNodeType.XmlDeclaration, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");

            writer.WriteStartDocument();

            writer.WriteElement(miscnode);

            writer.Close();
        }
示例#4
0
        public void ElementCloneTest()
        {
            using (var stream = GetStream(TestFiles.mcdoc))
                using (var doc = WordprocessingDocument.Open(stream, false))
                {
                    // Shallow clone the body, which doesn't have attributes
                    var body       = doc.MainDocumentPart.Document.Body;
                    var clonedBody = body.CloneNode(false);
                    Assert.False(clonedBody.HasChildren);
                    Assert.Equal(body.HasAttributes, clonedBody.HasAttributes); //Cloned Body has no attributes.
                    Assert.Equal(body.ExtendedAttributes.Count(), clonedBody.ExtendedAttributes.Count());

                    // Shallow clone the first paragraph, which has attributes
                    var paragraph       = body.Descendants <Paragraph>().First();
                    var clonedParagraph = paragraph.CloneNode(false);
                    Assert.False(clonedParagraph.HasChildren);
                    Assert.True(clonedParagraph.HasAttributes);
                    Assert.Equal(paragraph.Attributes.Length, clonedParagraph.Attributes.Length);
                    Assert.Equal(paragraph.ExtendedAttributes.Count(), clonedParagraph.ExtendedAttributes.Count());

                    // Deep clone the document
                    var clonedDocument = doc.MainDocumentPart.Document.CloneNode(true);
                    Assert.True(clonedDocument.HasChildren);
                    Assert.Equal(doc.MainDocumentPart.Document.ChildElements.Count, clonedDocument.ChildElements.Count);
                    Assert.Equal(doc.MainDocumentPart.Document.Descendants().Count(), clonedDocument.Descendants().Count());
                    var docEnumerator = doc.MainDocumentPart.Document.Descendants().GetEnumerator();
                    foreach (var elem in clonedDocument.Descendants())
                    {
                        Assert.True(docEnumerator.MoveNext());
                        var curElem = docEnumerator.Current;
                        Assert.Equal(curElem.GetType(), elem.GetType());
                        Assert.Equal(curElem.NamespaceUri, elem.NamespaceUri);
                        Assert.Equal(curElem.XmlQualifiedName, elem.XmlQualifiedName);
                        Assert.Equal(curElem.Attributes.Length, elem.Attributes.Length);

                        Assert.Equal(curElem.ExtendedAttributes.Count(), elem.ExtendedAttributes.Count());
                        var a1 = curElem.ExtendedAttributes.ToArray();
                        var a2 = elem.ExtendedAttributes.ToArray();
                        for (var i = 0; i < a1.Length; i++)
                        {
                            Assert.Equal(a1[i].NamespaceUri, a2[i].NamespaceUri);
                            Assert.Equal(a1[i].LocalName, a2[i].LocalName);
                            Assert.Equal(a1[i].Value, a2[i].Value);
                        }

                        Assert.Equal(curElem.MCAttributes == null, elem.MCAttributes == null);
                        if (curElem is OpenXmlLeafTextElement)
                        {
                            Assert.Equal(((OpenXmlLeafTextElement)curElem).Text, ((OpenXmlLeafTextElement)elem).Text);
                        }
                    }

                    // Deep clone the unknown element
                    var unknown       = doc.MainDocumentPart.Document.Descendants <OpenXmlUnknownElement>().Where(e => e.LocalName == "wsp").First();
                    var clonedUnknown = unknown.CloneNode(true);
                    Assert.Equal(unknown.OuterXml, clonedUnknown.OuterXml);

                    // Clone the OpenXmlMiscNode
                    var miscNode       = new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- my comment -->");
                    var clonedMiscNode = miscNode.CloneNode(true) as OpenXmlMiscNode;
                    Assert.NotNull(clonedMiscNode);
                    Assert.Equal(miscNode.XmlNodeType, clonedMiscNode.XmlNodeType);
                    Assert.Equal(miscNode.OuterXml, clonedMiscNode.OuterXml);

                    // Shallow clone mc element
                    var acb = body.Descendants <AlternateContent>().First();
                    acb.MCAttributes                    = new MarkupCompatibilityAttributes();
                    acb.MCAttributes.Ignorable          = "w14";
                    acb.MCAttributes.MustUnderstand     = "wp";
                    acb.MCAttributes.PreserveAttributes = "a";
                    acb.MCAttributes.PreserveElements   = "w14";
                    acb.MCAttributes.ProcessContent     = "wp";
                    var clonedAcb = acb.CloneNode(false);
                    Assert.NotNull(clonedAcb.MCAttributes);
                    Assert.Equal(acb.MCAttributes.Ignorable.Value, clonedAcb.MCAttributes.Ignorable.Value);
                    Assert.Equal(acb.MCAttributes.MustUnderstand.Value, clonedAcb.MCAttributes.MustUnderstand.Value);
                    Assert.Equal(acb.MCAttributes.PreserveAttributes.Value, clonedAcb.MCAttributes.PreserveAttributes.Value);
                    Assert.Equal(acb.MCAttributes.PreserveElements.Value, clonedAcb.MCAttributes.PreserveElements.Value);
                    Assert.Equal(acb.MCAttributes.ProcessContent.Value, clonedAcb.MCAttributes.ProcessContent.Value);
                }
        }
        public void CompositeElementGetSetChildrenTest()
        {
            // test case for OpenXmlCompositeType.OneSequence
            RunProperties target = new RunProperties();
            Bold          bold   = new Bold();

            target.Bold = bold;

            Assert.Same(bold, target.Bold);
            Assert.Same(bold, target.FirstChild);

            Italic italic = new Italic();

            target.AppendChild(italic);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(italic, target.LastChild);

            OpenXmlMiscNode miscNod = new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, @"<!-- my comment --> ");

            target.InsertAfter(miscNod, italic);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(miscNod, target.LastChild);

            Caps caps = new Caps();

            target.Caps = caps;
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(caps, target.Caps);
            Assert.Same(miscNod, target.LastChild);

            Shadow shadow = new Shadow();

            target.InsertAfter(shadow, miscNod);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(caps, target.Caps);
            Assert.Same(shadow, target.Shadow);
            Assert.Same(shadow, target.LastChild);

            // test case for OpenXmlCompositeType.OneChoice
            FieldChar fieldChar = new FieldChar();

            FieldData fldData = new FieldData();

            fieldChar.FieldData = fldData;
            Assert.Same(fldData, fieldChar.FieldData);

            FormFieldData ffData = new FormFieldData();

            fieldChar.FormFieldData = ffData;
            Assert.Same(ffData, fieldChar.FormFieldData);
            Assert.Null(fieldChar.FieldData);

            miscNod = new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, @"<!-- OpenXmlCompositeType.OneChoice --> ");
            fieldChar.PrependChild(miscNod);
            Assert.Same(miscNod, fieldChar.FirstChild);
            Assert.Same(ffData, fieldChar.LastChild);
            Assert.Same(ffData, fieldChar.FormFieldData);
            Assert.Null(fieldChar.FieldData);

            NumberingChange numChange = new NumberingChange();

            fieldChar.NumberingChange = numChange;
            Assert.Same(miscNod, fieldChar.FirstChild);
            Assert.Same(numChange, fieldChar.LastChild);
            Assert.Same(numChange, fieldChar.NumberingChange);
            Assert.Null(fieldChar.FieldData);
        }