Skip to content

Commit a99c642

Browse files
committed
fix (Cname): Setting name now validates value rather than previous set private name.
Credits original author @jeanbern empira/PDFsharp#86
1 parent 14080aa commit a99c642

File tree

2 files changed

+80
-22
lines changed

2 files changed

+80
-22
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using FluentAssertions;
3+
using PdfSharpCore.Pdf.Content.Objects;
4+
using Xunit;
5+
6+
namespace PdfSharpCore.Test.Pdfs.Content.Objects
7+
{
8+
public class CNameTests
9+
{
10+
[Theory]
11+
[InlineData("/Foo")]
12+
public void SetNameTests(string name)
13+
{
14+
var cName = new CName
15+
{
16+
Name = name
17+
};
18+
19+
cName.Name.Should().Be(name);
20+
}
21+
22+
[Fact]
23+
public void SetNameNullThrowsException()
24+
{
25+
Action act = () => new CName
26+
{
27+
Name = null
28+
};
29+
act.Should().Throw<ArgumentNullException>();
30+
}
31+
32+
[Theory]
33+
[InlineData("Foo")]
34+
[InlineData("")]
35+
public void SetNameWithoutPrefixThrowsException(string name)
36+
{
37+
Action act = () => new CName
38+
{
39+
Name = name
40+
};
41+
act.Should().Throw<ArgumentException>();
42+
}
43+
}
44+
}

PdfSharpCore/Pdf.Content.Objects/CObjects.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region PDFsharp - A .NET library for processing PDF
2+
23
//
34
// Authors:
45
// Stefan Lange
@@ -25,6 +26,7 @@
2526
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2627
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2728
// DEALINGS IN THE SOFTWARE.
29+
2830
#endregion
2931

3032
using System;
@@ -35,7 +37,7 @@
3537
using System.IO;
3638
using System.Text;
3739

38-
namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
40+
namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
3941
{
4042
/// <summary>
4143
/// Base class for all PDF content stream objects.
@@ -46,7 +48,8 @@ public abstract class CObject : ICloneable
4648
/// Initializes a new instance of the <see cref="CObject"/> class.
4749
/// </summary>
4850
protected CObject()
49-
{ }
51+
{
52+
}
5053

5154
/// <summary>
5255
/// Creates a new object that is a copy of the current instance.
@@ -109,6 +112,7 @@ public string Text
109112
get { return _text; }
110113
set { _text = value; }
111114
}
115+
112116
string _text;
113117

114118
/// <summary>
@@ -129,7 +133,7 @@ internal override void WriteObject(ContentWriter writer)
129133
/// Represents a sequence of objects in a PDF content stream.
130134
/// </summary>
131135
[DebuggerDisplay("(count={Count})")]
132-
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
136+
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
133137
{
134138
/// <summary>
135139
/// Creates a new object that is a copy of the current instance.
@@ -250,6 +254,7 @@ public CObject this[int index]
250254
get { return (CObject)_items[index]; }
251255
set { _items[index] = value; }
252256
}
257+
253258
#endregion
254259

255260
#region ICollection Members
@@ -362,14 +367,8 @@ void IList<CObject>.RemoveAt(int index)
362367

363368
CObject IList<CObject>.this[int index]
364369
{
365-
get
366-
{
367-
throw new NotImplementedException();
368-
}
369-
set
370-
{
371-
throw new NotImplementedException();
372-
}
370+
get { throw new NotImplementedException(); }
371+
set { throw new NotImplementedException(); }
373372
}
374373

375374
#endregion
@@ -484,6 +483,7 @@ public int Value
484483
get { return _value; }
485484
set { _value = value; }
486485
}
486+
487487
int _value;
488488

489489
/// <summary>
@@ -531,6 +531,7 @@ public double Value
531531
get { return _value; }
532532
set { _value = value; }
533533
}
534+
534535
double _value;
535536

536537
/// <summary>
@@ -611,6 +612,7 @@ public string Value
611612
get { return _value; }
612613
set { _value = value; }
613614
}
615+
614616
string _value;
615617

616618
/// <summary>
@@ -621,6 +623,7 @@ public CStringType CStringType
621623
get { return _cStringType; }
622624
set { _cStringType = value; }
623625
}
626+
624627
CStringType _cStringType;
625628

626629
/// <summary>
@@ -687,6 +690,7 @@ public override string ToString()
687690
break;
688691
}
689692
}
693+
690694
s.Append(')');
691695
break;
692696

@@ -710,6 +714,7 @@ public override string ToString()
710714
default:
711715
throw new ArgumentOutOfRangeException();
712716
}
717+
713718
return s.ToString();
714719
}
715720

@@ -725,12 +730,14 @@ internal override void WriteObject(ContentWriter writer)
725730
[DebuggerDisplay("({Name})")]
726731
public class CName : CObject
727732
{
733+
private const string NamePrefix = "/";
734+
728735
/// <summary>
729736
/// Initializes a new instance of the <see cref="CName"/> class.
730737
/// </summary>
731738
public CName()
732739
{
733-
_name = "/";
740+
_name = NamePrefix;
734741
}
735742

736743
/// <summary>
@@ -760,21 +767,24 @@ protected override CObject Copy()
760767
}
761768

762769
/// <summary>
763-
/// Gets or sets the name. Names must start with a slash.
770+
/// Gets or sets the content stream name. Names must start with a slash.
764771
/// </summary>
772+
/// <exception cref="ArgumentNullException"></exception>
773+
/// <exception cref="ArgumentException">If <paramref name="value"/> does not start with a forward slash</exception>
765774
public string Name
766775
{
767-
get { return _name; }
776+
get => _name;
768777
set
769778
{
770-
if (String.IsNullOrEmpty(_name))
771-
throw new ArgumentNullException("name");
772-
if (_name[0] != '/')
773-
throw new ArgumentException(PSSR.NameMustStartWithSlash);
779+
if (string.IsNullOrEmpty(value))
780+
throw new ArgumentNullException(nameof(value));
781+
if (!value.StartsWith(NamePrefix))
782+
throw new ArgumentException(PSSR.NameMustStartWithSlash, nameof(value));
774783
_name = value;
775784
}
776785
}
777-
string _name;
786+
787+
private string _name;
778788

779789
/// <summary>
780790
/// Returns a string that represents the current value.
@@ -837,7 +847,8 @@ public class COperator : CObject
837847
/// Initializes a new instance of the <see cref="COperator"/> class.
838848
/// </summary>
839849
protected COperator()
840-
{ }
850+
{
851+
}
841852

842853
internal COperator(OpCode opcode)
843854
{
@@ -878,6 +889,7 @@ public CSequence Operands
878889
{
879890
get { return _seqence ?? (_seqence = new CSequence()); }
880891
}
892+
881893
CSequence _seqence;
882894

883895
/// <summary>
@@ -887,6 +899,7 @@ public OpCode OpCode
887899
{
888900
get { return _opcode; }
889901
}
902+
890903
readonly OpCode _opcode;
891904

892905

@@ -903,13 +916,14 @@ public override string ToString()
903916

904917
internal override void WriteObject(ContentWriter writer)
905918
{
906-
int count = _seqence != null ? _seqence.Count : 0;
919+
int count = _seqence?.Count ?? 0;
907920
for (int idx = 0; idx < count; idx++)
908921
{
909922
// ReSharper disable once PossibleNullReferenceException because the loop is not entered if _sequence is null
910923
_seqence[idx].WriteObject(writer);
911924
}
925+
912926
writer.WriteLineRaw(ToString());
913927
}
914928
}
915-
}
929+
}

0 commit comments

Comments
 (0)