Skip to content

Commit

Permalink
Merge pull request #12 from nationalarchives/fix/ewcop_2024_30
Browse files Browse the repository at this point in the history
CLP-11335 improve number overrides for paragraphs with style numbering
  • Loading branch information
jurisdatum authored Jun 9, 2024
2 parents f7423da + da6cc5e commit d2f6306
Show file tree
Hide file tree
Showing 4 changed files with 1,325 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/docx/Numbering2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,17 @@ internal static int CalculateN(MainDocumentPart main, Paragraph paragraph, int n
// -1 means not set
// -2 meanss trumped, even numbering instance's own start value doesn't matter
// any positive integer is the numbering id of the previous paragraph that set the value of 'start'
int? numIdOfStartOverrideStyle = null;
int? numIdOfStartOverrideWithoutStyle = null;
bool numOverrideShouldntApplyToStyleOnly = false;

bool prevContainsLowerCompound = false; // see setter below

int absStart = GetAbstractStart(main, abstractNumId, ilvl);

var prevAbsStarts = new PrevAbsStartAccumulator();
int count = 0;

foreach (Paragraph prev in paragraph.Root().Descendants<Paragraph>().TakeWhile(p => !object.ReferenceEquals(p, paragraph))) {

if (Paragraphs.IsDeleted(prev))
Expand Down Expand Up @@ -704,6 +708,8 @@ internal static int CalculateN(MainDocumentPart main, Paragraph paragraph, int n
continue;
}

// now prevIlvl == ilvl

if (prevNumIdWithoutStyle.HasValue) {
var prevAbsStart = prevAbsStarts.Get(prevNumIdWithoutStyle.Value, prevIlvl + 2);
if (prevAbsStart.HasValue) {
Expand All @@ -712,13 +718,21 @@ internal static int CalculateN(MainDocumentPart main, Paragraph paragraph, int n
}
}

// prevIlvl == ilvl
if (prevNumId.Value != numIdOfStartOverride && numIdOfStartOverride != -2) { // true whenever start is null
bool numOverrideShouldntApplyToPrev = numOverrideShouldntApplyToStyleOnly && !prevNumIdWithoutStyle.HasValue && prevNumIdOfStyle == numIdOfStartOverrideStyle;

if (!numOverrideShouldntApplyToPrev && prevNumId.Value != numIdOfStartOverride && numIdOfStartOverride != -2) { // true whenever start is null
if (!isHigher || prevNumIdOfStyle.HasValue) { // test68
int? prevOver = GetStartOverride(prevNumbering, ilvl);
if (prevOver.HasValue && StartOverrideIsOperative(main, prev, prevIlvl)) {
start = prevOver.Value;
numIdOfStartOverride = prevNumId.Value;
if (prevNumIdOfStyle == numIdOfStartOverrideStyle && prevNumIdWithoutStyle.HasValue && !numIdOfStartOverrideWithoutStyle.HasValue) {
numOverrideShouldntApplyToStyleOnly = true;
// When the number override comes from a paragraph that has a style but also numbering of its own,
// then the number override shouldn't apply to paragraphs with only that style. See test 86.
}
numIdOfStartOverrideStyle = prevNumIdOfStyle;
numIdOfStartOverrideWithoutStyle = prevNumIdWithoutStyle;
if (!prevContainsLowerCompound) // only test37 needs this condition
count = 0;
}
Expand All @@ -739,7 +753,9 @@ internal static int CalculateN(MainDocumentPart main, Paragraph paragraph, int n
if (isHigher)
prevContainsLowerCompound = true;

if (numberingId != numIdOfStartOverride && numIdOfStartOverride != -2) { // true whenever start is null
bool numOverrideShouldntApply = numOverrideShouldntApplyToStyleOnly && !thisNumIdWithoutStyle.HasValue && thisNumIdOfStyle == numIdOfStartOverrideStyle;

if (!numOverrideShouldntApply && numberingId != numIdOfStartOverride && numIdOfStartOverride != -2) { // true whenever start is null
int? over = GetStartOverride(main, numberingId, ilvl);
if (start is null)
start = GetStart(main, numberingId, ilvl);
Expand Down
2 changes: 1 addition & 1 deletion test/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Tests() {
Transform.Load(xsltReader);
}

static readonly int total = 85;
static readonly int total = 86;

public static readonly IEnumerable<object[]> indices =
Enumerable.Concat(
Expand Down
Binary file added test/judgments/test86.docx
Binary file not shown.
Loading

0 comments on commit d2f6306

Please sign in to comment.