Skip to content

Commit d603441

Browse files
committed
Make sure to use start number when explicit stated.
When using reversed the start number by default should be the number of items in the list.
1 parent ebe9a95 commit d603441

5 files changed

Lines changed: 60 additions & 11 deletions

File tree

Aztec/Classes/Extensions/NSAttributedString+Lists.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ extension NSAttributedString {
6868
return resultRange
6969
}
7070

71+
/// Returns the number of items of a list at the given location.
72+
///
73+
/// - Parameters:
74+
/// - list: The list.
75+
/// - location: The location of the item.
76+
///
77+
/// - Returns: Returns the total number of items within the list.
78+
///
79+
func numberOfItems(in list: TextList, at location: Int) -> Int {
80+
guard
81+
let paragraphStyle = attribute(.paragraphStyle, at: location, effectiveRange: nil) as? ParagraphStyle
82+
else {
83+
return NSNotFound
84+
}
85+
let listDepth = paragraphStyle.lists.count
86+
guard let rangeOfList = range(of:list, at: location) else {
87+
return NSNotFound
88+
}
89+
var numberInList = 0
90+
let paragraphRanges = self.paragraphRanges(intersecting: rangeOfList)
91+
for (_, enclosingRange) in paragraphRanges {
92+
if let paragraphStyle = attribute(.paragraphStyle, at: enclosingRange.location, effectiveRange: nil) as? ParagraphStyle,
93+
listDepth == paragraphStyle.lists.count {
94+
numberInList += 1
95+
}
96+
}
97+
return numberInList
98+
}
7199
/// Returns the index of the item at the given location within the list.
72100
///
73101
/// - Parameters:

Aztec/Classes/NSAttributedString/Conversions/AttributedStringParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@ private extension AttributedStringParser {
770770
listElement.updateAttribute(named: "reversed", value: .none)
771771
}
772772

773-
if list.start != 1 {
774-
listElement.updateAttribute(named: "start", value: .string("\(list.start)"))
773+
if let start = list.start {
774+
listElement.updateAttribute(named: "start", value: .string("\(start)"))
775775
}
776776
}
777777
}

Aztec/Classes/TextKit/LayoutManager.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ private extension LayoutManager {
211211
let glyphRange = self.glyphRange(forCharacterRange: enclosingRange, actualCharacterRange: nil)
212212
let markerRect = rectForItem(range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
213213
var markerNumber = textStorage.itemNumber(in: list, at: enclosingRange.location)
214+
var start = list.start ?? 1
214215
if list.reversed {
215216
markerNumber = -markerNumber
217+
if list.start == nil {
218+
start = textStorage.numberOfItems(in: list, at: enclosingRange.location)
219+
}
216220
}
217-
markerNumber += list.start
221+
markerNumber += start
218222

219223
drawItem(number: markerNumber, in: markerRect, from: list, using: paragraphStyle, at: enclosingRange.location)
220224
}

Aztec/Classes/TextKit/ParagraphProperty/TextList.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,27 @@ open class TextList: ParagraphProperty {
2424

2525
public let reversed: Bool
2626

27-
public let start: Int
27+
public let start: Int?
2828

2929
// MARK: - Properties
3030

3131
/// Kind of List: Ordered / Unordered
3232
///
3333
let style: Style
3434

35-
init(style: Style, start: Int = 1, reversed: Bool = false, with representation: HTMLRepresentation? = nil) {
35+
init(style: Style, start: Int? = nil, reversed: Bool = false, with representation: HTMLRepresentation? = nil) {
3636
self.style = style
3737

3838
if let representation = representation, case let .element( html ) = representation.kind {
3939
self.reversed = html.attribute(ofType: .reversed) != nil
4040

41-
if let startAttribute = html.attribute(ofType: .start), case let .string( value ) = startAttribute.value, let start = Int(value) {
41+
if let startAttribute = html.attribute(ofType: .start),
42+
case let .string( value ) = startAttribute.value,
43+
let start = Int(value)
44+
{
4245
self.start = start
4346
} else {
44-
self.start = 1
47+
self.start = nil
4548
}
4649
} else {
4750
self.start = start
@@ -61,7 +64,7 @@ open class TextList: ParagraphProperty {
6164
let decodedStart = aDecoder.decodeInteger(forKey: AttributeType.start.rawValue)
6265
start = decodedStart
6366
} else {
64-
start = 1
67+
start = nil
6568
}
6669

6770
if aDecoder.containsValue(forKey: AttributeType.reversed.rawValue) {

Example/Example/SampleContent/content.html

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@ <h4>Ordered List:</h4>
3737
<li>Three</li>
3838
</ol>
3939

40+
<h4>Start in 10 List:</h4>
41+
<ol start="10">
42+
<li>Ten</li>
43+
<li>Eleven</li>
44+
<li>Twelve</li>
45+
</ol>
46+
4047
<h4>Reversed List:</h4>
41-
<ol reversed start="10">
42-
<li>One</li>
43-
<li>Two</li>
48+
<ol reversed>
4449
<li>Three</li>
50+
<li>Two</li>
51+
<li>One</li>
52+
</ol>
53+
54+
<h4>Reversed Start in 10 List:</h4>
55+
<ol reversed start="10">
56+
<li>Ten</li>
57+
<li>Nine</li>
58+
<li>Eight</li>
4559
</ol>
4660

4761
<h4>Nested lists:</h4>

0 commit comments

Comments
 (0)