Skip to content

Commit c69ea62

Browse files
committed
Ignore numbering levels that use numStyleLink to refer to themselves
1 parent fbf5e22 commit c69ea62

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Fix: on Windows, when an image's content type includes a backslash in the
44
subpart, files may be written outside of the directory set by --output-dir.
55

6+
* Detect and ignore numbering levels that use numStyleLink to refer to
7+
themselves.
8+
69
# 1.12.0
710

811
* Handle hyperlinked wp:anchor and wp:inline elements.

‎lib/docx/numbering-xml.js‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,29 @@ function Numbering(nums, abstractNums, styles) {
1717
);
1818

1919
function findLevel(numId, level) {
20+
return findLevelWithSeenNumIds(numId, level, {});
21+
}
22+
23+
function findLevelWithSeenNumIds(numId, level, seenNumIds) {
24+
if (seenNumIds[numId]) {
25+
return null;
26+
}
27+
seenNumIds[numId] = true;
28+
2029
var num = nums[numId];
21-
if (num) {
22-
var abstractNum = abstractNums[num.abstractNumId];
23-
if (!abstractNum) {
24-
return null;
25-
} else if (abstractNum.numStyleLink == null) {
26-
return abstractNums[num.abstractNumId].levels[level];
27-
} else {
28-
var style = styles.findNumberingStyleById(abstractNum.numStyleLink);
29-
return findLevel(style.numId, level);
30-
}
31-
} else {
30+
if (!num) {
3231
return null;
3332
}
33+
34+
var abstractNum = abstractNums[num.abstractNumId];
35+
if (!abstractNum) {
36+
return null;
37+
} else if (abstractNum.numStyleLink == null) {
38+
return abstractNums[num.abstractNumId].levels[level];
39+
} else {
40+
var style = styles.findNumberingStyleById(abstractNum.numStyleLink);
41+
return findLevelWithSeenNumIds(style.numId, level, seenNumIds);
42+
}
3443
}
3544

3645
function findLevelByParagraphStyleId(styleId) {

‎test/docx/numbering-xml.tests.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ test('when w:abstractNum has w:numStyleLink then style is used to find w:num', f
152152
});
153153

154154

155+
test('when w:abstractNum has self-recursive w:numStyleLink then level is not found', function() {
156+
var numbering = readNumberingXml(
157+
new XmlElement("w:numbering", {}, [
158+
new XmlElement("w:abstractNum", {"w:abstractNumId": "100"}, [
159+
new XmlElement("w:numStyleLink", {"w:val": "List1"})
160+
]),
161+
new XmlElement("w:num", {"w:numId": "200"}, [
162+
new XmlElement("w:abstractNumId", {"w:val": "100"})
163+
])
164+
]),
165+
{styles: new stylesReader.Styles({}, {}, {}, {"List1": {numId: "200"}})}
166+
);
167+
duck.assertThat(numbering.findLevel("200", "0"), duck.equalTo(null));
168+
});
169+
170+
155171
// See: 17.9.23 pStyle (Paragraph Style's Associated Numbering Level) in ECMA-376, 4th Edition
156172
test('numbering level can be found by paragraph style ID', function() {
157173
var numbering = readNumberingXml(

0 commit comments

Comments
 (0)