Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions Clippit.Tests/PowerPoint/PresentationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,155 @@ public async Task PB010_EmbeddedFontWithEot10Header()
await Assert.That(fonts["New Font"]).IsEquivalentTo(eot22);
}

[Test]
public async Task PB011_SlideWith3DModelSourceUrl()
{
var sourceUrl = new Uri("https://example.com/models/cube");

var sourceDir = new DirectoryInfo("../../../../TestFiles/");
using var sourceStream = new MemoryStream();
sourceStream.Write(File.ReadAllBytes(Path.Combine(sourceDir.FullName, "PB008-3DModel.pptx")));
using (var source = PresentationDocument.Open(sourceStream, true))
{
var sourceSlide = source.PresentationPart!.SlideParts.Single(s =>
s.Model3DReferenceRelationshipParts.Any()
);
var hyperlink = sourceSlide.AddHyperlinkRelationship(sourceUrl, true);
var raster = sourceSlide.GetXDocument().Descendants(AM3D.blip).Single().Parent!;
raster.AddBeforeSelf(new XElement(AM3D.attrSrcUrl, new XAttribute(R.id, hyperlink.Id)));
sourceSlide.PutXDocument();
}

var sources = new List<SlideSource>
{
new(new PmlDocument("PB011-Input.pptx", sourceStream.ToArray()), 1, 1, true),
};
var outputPptx = new FileInfo(Path.Combine(TempDir, "PB011-Output.pptx"));
PresentationBuilder.BuildPresentation(sources).SaveAs(outputPptx.FullName);

using var pptx = PresentationDocument.Open(outputPptx.FullName, false);
await Validate(pptx);

var slidePart = pptx.PresentationPart!.SlideParts.Single();
var relId = (string)slidePart.GetXDocument().Descendants(AM3D.attrSrcUrl).Single().Attribute(R.id);
var copied = slidePart.HyperlinkRelationships.Single(h => h.Id == relId);
await Assert.That(copied.Uri).IsEqualTo(sourceUrl);
await Assert.That(copied.IsExternal).IsTrue();
}

[Test]
public async Task PB012_SlideWithWebExtension()
{
var snapshotUrl = new Uri("https://example.com/snapshot.png");
var snapshot = Convert.FromBase64String(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
);
var webExtension = new XElement(
WE.webextension,
new XAttribute(XNamespace.Xmlns + "we", WE.we),
new XAttribute(XNamespace.Xmlns + "r", R.r),
new XAttribute("id", "{6F1B2A5C-0B5E-4C0E-9A57-2D0C3E6B7A11}"),
new XElement(
WE.reference,
new XAttribute("id", "wa000000000"),
new XAttribute("version", "1.0.0.0"),
new XAttribute("store", "en-US"),
new XAttribute("storeType", "OMEX")
),
new XElement(WE.alternateReferences),
new XElement(WE.properties),
new XElement(WE.bindings),
new XElement(WE.snapshot)
);

var sourceDir = new DirectoryInfo("../../../../TestFiles/");
using var sourceStream = new MemoryStream();
sourceStream.Write(File.ReadAllBytes(Path.Combine(sourceDir.FullName, "PB001-Input1.pptx")));
using (var source = PresentationDocument.Open(sourceStream, true))
{
var presentationPart = source.PresentationPart!;
var firstSlideId = (string)
presentationPart.GetXDocument().Root!.Element(P.sldIdLst)!.Elements(P.sldId).First().Attribute(R.id);
var sourceSlide = (SlidePart)presentationPart.GetPartById(firstSlideId);

var webExtensionPart = sourceSlide.AddNewPart<WebExtensionPart>();
var snapshotPart = webExtensionPart.AddImagePart(ImagePartType.Png);
using (var snapshotStream = new MemoryStream(snapshot))
snapshotPart.FeedData(snapshotStream);
var snapshotLink = webExtensionPart.AddExternalRelationship(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
snapshotUrl
);
webExtension
.Element(WE.snapshot)!
.Add(
new XAttribute(R.embed, webExtensionPart.GetIdOfPart(snapshotPart)),
new XAttribute(R.link, snapshotLink.Id)
);
webExtensionPart.PutXDocument(new XDocument(webExtension));

var spTree = sourceSlide.GetXDocument().Descendants(P.spTree).Single();
spTree.Add(
new XElement(
P.graphicFrame,
new XElement(
P.nvGraphicFramePr,
new XElement(P.cNvPr, new XAttribute("id", "1000"), new XAttribute("name", "Add-in")),
new XElement(P.cNvGraphicFramePr),
new XElement(P.nvPr)
),
new XElement(
P.xfrm,
new XElement(A.off, new XAttribute("x", "0"), new XAttribute("y", "0")),
new XElement(A.ext, new XAttribute("cx", "914400"), new XAttribute("cy", "914400"))
),
new XElement(
A.graphic,
new XElement(
A.graphicData,
new XAttribute("uri", WE.we.NamespaceName),
new XElement(
WE.webextensionref,
new XAttribute(XNamespace.Xmlns + "we", WE.we),
new XAttribute(R.id, sourceSlide.GetIdOfPart(webExtensionPart))
)
)
)
)
);
sourceSlide.PutXDocument();
}

var sources = new List<SlideSource>
{
new(new PmlDocument("PB012-Input.pptx", sourceStream.ToArray()), 0, 1, true),
};
var outputPptx = new FileInfo(Path.Combine(TempDir, "PB012-Output.pptx"));
PresentationBuilder.BuildPresentation(sources).SaveAs(outputPptx.FullName);

using var pptx = PresentationDocument.Open(outputPptx.FullName, false);
await Validate(pptx, DocumentFormat.OpenXml.FileFormatVersions.Office2013);

var slidePart = pptx.PresentationPart!.SlideParts.Single();
var relId = (string)slidePart.GetXDocument().Descendants(WE.webextensionref).Single().Attribute(R.id);
var copiedPart = slidePart.WebExtensionParts.Single();
await Assert.That(slidePart.GetIdOfPart(copiedPart)).IsEqualTo(relId);

var copiedXml = copiedPart.GetXDocument().Root!;
await Assert.That((string)copiedXml.Attribute("id")).IsEqualTo((string)webExtension.Attribute("id"));
await Assert
.That((string)copiedXml.Element(WE.reference)!.Attribute("id"))
.IsEqualTo((string)webExtension.Element(WE.reference)!.Attribute("id"));

var copiedSnapshot = copiedXml.Element(WE.snapshot)!;
var snapshotId = (string)copiedSnapshot.Attribute(R.embed);
await Assert.That(ReadAllBytes(copiedPart.GetPartById(snapshotId))).IsEquivalentTo(snapshot);

var linkedSnapshotId = (string)copiedSnapshot.Attribute(R.link);
var copiedSnapshotLink = copiedPart.ExternalRelationships.Single(r => r.Id == linkedSnapshotId);
await Assert.That(copiedSnapshotLink.Uri).IsEqualTo(snapshotUrl);
}

private static byte[] ReadAllBytes(OpenXmlPart part)
{
using var stream = part.GetStream(FileMode.Open, FileAccess.Read);
Expand Down
32 changes: 32 additions & 0 deletions Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,15 @@ IEnumerable<XElement> newContent
|| name == A.blip
|| name == SVG.svgBlip
|| name == AM3D.blip
|| name == WE.snapshot
)
{
CopyRelatedImage(oldContentPart, newContentPart, element, R.embed);
CopyRelatedImage(oldContentPart, newContentPart, element, R.pict);
CopyRelatedImage(oldContentPart, newContentPart, element, R.id);
CopyRelatedImage(oldContentPart, newContentPart, element, O.relid);
if (name == WE.snapshot)
PBT.CopyRelatedMediaExternalRelationship(oldContentPart, newContentPart, element, R.link);
}
else if (name == A.videoFile || name == A.quickTimeFile)
{
Expand Down Expand Up @@ -491,6 +494,10 @@ IEnumerable<XElement> newContent
{
PBT.CopyModel3DPart(oldContentPart, newContentPart, element, R.embed);
}
else if (name == WE.webextensionref)
{
CopyWebExtensionPart(oldContentPart, newContentPart, element, R.id);
}
else if (name == P.contentPart)
{
PBT.CopyInkPart(oldContentPart, newContentPart, element, R.id);
Expand Down Expand Up @@ -821,6 +828,30 @@ static void PruneCustDataReference(XElement element)
}
}

private void CopyWebExtensionPart(
OpenXmlPart oldContentPart,
OpenXmlPart newContentPart,
XElement webExtensionReference,
XName attributeName
)
{
var relId = (string)webExtensionReference.Attribute(attributeName);
if (newContentPart.HasRelationship(relId) || newContentPart is not SlidePart newSlidePart)
return;

if (
oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId).OpenXmlPart
is not WebExtensionPart oldPart
)
return;

var newPart = newSlidePart.AddNewPart<WebExtensionPart>();
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
webExtensionReference.Attribute(attributeName).Set(newSlidePart.GetIdOfPart(newPart));
PBT.AddRelationships(oldPart, newPart, [newPart.GetXDocument().Root]);
CopyRelatedPartsForContentParts(oldPart, newPart, [newPart.GetXDocument().Root]);
}

private void CopyRelatedImage(
OpenXmlPart oldContentPart,
OpenXmlPart newContentPart,
Expand Down Expand Up @@ -885,6 +916,7 @@ XName attributeName
ThemeOverridePart part => part.AddImagePart(contentType),
ThemePart part => part.AddImagePart(contentType),
VmlDrawingPart part => part.AddImagePart(contentType),
WebExtensionPart part => part.AddImagePart(contentType),
WorksheetPart part => part.AddImagePart(contentType),
_ => null,
};
Expand Down
8 changes: 7 additions & 1 deletion Clippit/PowerPoint/Fluent/PresentationBuilderTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static List<string> GetSlideIdsInOrder(PresentationDocument srcDoc)
{ A.hlinkClick, new[] { R.id } },
{ A.hlinkMouseOver, new[] { R.id } },
{ A.hlinkHover, new[] { R.id } },
{ AM3D.attrSrcUrl, new[] { R.id } },
{ A.relIds, new[] { R.cs, R.dm, R.lo, R.qs } },
{ C.chart, new[] { R.id } },
{ C.externalData, new[] { R.id } },
Expand Down Expand Up @@ -383,7 +384,12 @@ IEnumerable<XElement> newContent
.ToList();
foreach (var e in relevantElements)
{
if (e.Name == A.hlinkClick || e.Name == A.hlinkHover || e.Name == A.hlinkMouseOver)
if (
e.Name == A.hlinkClick
|| e.Name == A.hlinkHover
|| e.Name == A.hlinkMouseOver
|| e.Name == AM3D.attrSrcUrl
)
{
var relId = (string)e.Attribute(R.id);
if (string.IsNullOrEmpty(relId))
Expand Down
1 change: 1 addition & 0 deletions Clippit/PtOpenXmlUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@
public bool Equals(ContentDataKey other) =>
_contentType == other._contentType && _hash.AsSpan().SequenceEqual(other._hash);

public override bool Equals(object obj) => obj is ContentDataKey other && Equals(other);

Check warning on line 1681 in Clippit/PtOpenXmlUtil.cs

View workflow job for this annotation

GitHub Actions / NativeAOT tool install (osx-arm64)

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

Check warning on line 1681 in Clippit/PtOpenXmlUtil.cs

View workflow job for this annotation

GitHub Actions / NativeAOT tool install (linux-x64)

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

public override int GetHashCode()
{
Expand Down Expand Up @@ -4049,6 +4049,7 @@
public static readonly XNamespace am3d = "http://schemas.microsoft.com/office/drawing/2017/model3d";
public static readonly XName model3d = am3d + "model3d";
public static readonly XName blip = am3d + "blip";
public static readonly XName attrSrcUrl = am3d + "attrSrcUrl";
}

public static class A1611
Expand Down
Loading