Skip to content

Commit 2a07672

Browse files
committed
Merge branch 'develop' into release/1.6.4
2 parents e8f4c95 + a1ad1a9 commit 2a07672

19 files changed

Lines changed: 102 additions & 24 deletions

File tree

Aztec.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
F9982CF621877663001E606B /* TextViewPasteboardDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9982CF521877663001E606B /* TextViewPasteboardDelegate.swift */; };
223223
F99D5CF321B989FF0089314A /* StringRangeMultibyteConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F99D5CF221B989FF0089314A /* StringRangeMultibyteConversionTests.swift */; };
224224
FF0714021EFD78AF00E50713 /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FF0714001EFD78AF00E50713 /* Media.xcassets */; };
225+
FF17B2D2227A589F0022AECE /* LIElementConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF17B2D1227A589F0022AECE /* LIElementConverter.swift */; };
225226
FF20D6401EDC389A00294B78 /* ShortcodeAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63D1EDC389A00294B78 /* ShortcodeAttribute.swift */; };
226227
FF20D6411EDC389A00294B78 /* HTMLProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63E1EDC389A00294B78 /* HTMLProcessor.swift */; };
227228
FF20D6421EDC389A00294B78 /* Processor.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF20D63F1EDC389A00294B78 /* Processor.swift */; };
@@ -493,6 +494,7 @@
493494
F9982CF521877663001E606B /* TextViewPasteboardDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextViewPasteboardDelegate.swift; sourceTree = "<group>"; };
494495
F99D5CF221B989FF0089314A /* StringRangeMultibyteConversionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringRangeMultibyteConversionTests.swift; sourceTree = "<group>"; };
495496
FF0714001EFD78AF00E50713 /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = "<group>"; };
497+
FF17B2D1227A589F0022AECE /* LIElementConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LIElementConverter.swift; sourceTree = "<group>"; };
496498
FF20D63D1EDC389A00294B78 /* ShortcodeAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShortcodeAttribute.swift; sourceTree = "<group>"; };
497499
FF20D63E1EDC389A00294B78 /* HTMLProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLProcessor.swift; sourceTree = "<group>"; };
498500
FF20D63F1EDC389A00294B78 /* Processor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Processor.swift; sourceTree = "<group>"; };
@@ -541,6 +543,7 @@
541543
isa = PBXGroup;
542544
children = (
543545
40359F271FD88A7900B1C1D2 /* BRElementConverter.swift */,
546+
FF17B2D1227A589F0022AECE /* LIElementConverter.swift */,
544547
F1E2323120C17F70008DA49F /* CiteElementConverter.swift */,
545548
B5E94D0F1FE01334000E7C20 /* FigureElementConverter.swift */,
546549
F1FF7DA0201A1D3E007B0B32 /* FigcaptionElementConverter.swift */,
@@ -1564,6 +1567,7 @@
15641567
F177FF751FB6404D00CBBE35 /* UILayoutPriority+Swift4.swift in Sources */,
15651568
F1C05B9D1E37FA77007510EA /* String+CharacterName.swift in Sources */,
15661569
F1FF7D9F201A1D24007B0B32 /* Figcaption.swift in Sources */,
1570+
FF17B2D2227A589F0022AECE /* LIElementConverter.swift in Sources */,
15671571
FF437B6620D7CB83000D9666 /* LiFormatter.swift in Sources */,
15681572
FFFEC7DB1EA7698900F4210F /* VideoAttachment.swift in Sources */,
15691573
F12F58671EF20394008AE298 /* BlockquoteFormatter.swift in Sources */,

Aztec/Classes/Converters/ElementsToAttributedString/Base/ElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import UIKit
55
///
66
public protocol ElementConverter {
77

8-
typealias ContentSerializer = (_ elementNode: ElementNode, _ intrinsicRepresentation: NSAttributedString?, _ inheriting: [NSAttributedString.Key:Any]) -> NSAttributedString
8+
typealias ContentSerializer = (_ elementNode: ElementNode, _ intrinsicRepresentation: NSAttributedString?, _ inheriting: [NSAttributedString.Key:Any], _ implicitRepresentationBeforeChildren: Bool) -> NSAttributedString
99

1010
/// Converts an instance of ElementNode into a NSAttributedString.
1111
///

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/BRElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class BRElementConverter: ElementConverter {
1616

1717
let intrinsicRepresentation = NSAttributedString(.lineSeparator, attributes: attributes)
1818

19-
return serialize(element, intrinsicRepresentation, attributes)
19+
return serialize(element, intrinsicRepresentation, attributes, false)
2020
}
2121
}

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/CiteElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CiteElementConverter: FormatterElementConverter {
1616

1717
let childrenAttributes = attributes(for: element, inheriting: inheritedAttributes)
1818

19-
return serialize(element, nil, childrenAttributes)
19+
return serialize(element, nil, childrenAttributes, false)
2020
}
2121

2222
// MARK: - FormatterElementConverter

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/FigcaptionElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FigcaptionElementConverter: ElementConverter {
1818

1919
let attributes = self.attributes(for: element, inheriting: attributes)
2020

21-
return serialize(element, nil, attributes)
21+
return serialize(element, nil, attributes, false)
2222
}
2323

2424
private func attributes(for element: ElementNode, inheriting attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] {

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/FigureElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FigureElementConverter: ElementConverter {
1616

1717
let attributes = self.attributes(for: element, inheriting: attributes)
1818

19-
return serialize(element, nil, attributes)
19+
return serialize(element, nil, attributes, false)
2020
}
2121

2222
private func attributes(for element: ElementNode, inheriting attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] {

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/GenericElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class GenericElementConverter: ElementConverter {
113113

114114
let childrenAttributes = attributes(for: element, inheriting: inheritedAttributes)
115115

116-
return serialize(element, nil, childrenAttributes)
116+
return serialize(element, nil, childrenAttributes, false)
117117
}
118118
}
119119

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/HRElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HRElementConverter: AttachmentElementConverter {
2323
let attachment = self.attachment(for: element)
2424

2525
let intrinsicRepresentation = NSAttributedString(attachment: attachment, attributes: attributes)
26-
let serialization = serialize(element, intrinsicRepresentation, attributes)
26+
let serialization = serialize(element, intrinsicRepresentation, attributes, false)
2727

2828
return (attachment, serialization)
2929
}

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/ImageElementConverter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ImageElementConverter: AttachmentElementConverter {
1616

1717
let attachment = self.attachment(for: element)
1818
let intrinsicRepresentation = NSAttributedString(attachment: attachment, attributes: attributes)
19-
let serialization = serialize(element, intrinsicRepresentation, attributes)
19+
let serialization = serialize(element, intrinsicRepresentation, attributes, false)
2020

2121
return (attachment, serialization)
2222
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import UIKit
2+
3+
4+
/// Converts `<li>` elements into a `String(.lineSeparator)`.
5+
///
6+
class LIElementConverter: ElementConverter {
7+
8+
// MARK: - ElementConverter
9+
10+
func convert(
11+
_ element: ElementNode,
12+
inheriting attributes: [NSAttributedString.Key: Any],
13+
contentSerializer serialize: ContentSerializer) -> NSAttributedString {
14+
15+
precondition(element.type == .li)
16+
17+
var intrinsicRepresentation: NSAttributedString?
18+
let intrisicRepresentationBeforeChildren = !hasNonEmptyTextChildren(node: element) && hasNestedList(node: element)
19+
20+
let elementRepresentation = HTMLElementRepresentation(element)
21+
let representation = HTMLRepresentation(for: .element(elementRepresentation))
22+
let formatter = LiFormatter(placeholderAttributes: nil)
23+
let finalAttributes = formatter.apply(to: attributes, andStore: representation)
24+
25+
if intrisicRepresentationBeforeChildren {
26+
intrinsicRepresentation = NSAttributedString(string: String(.paragraphSeparator), attributes: finalAttributes)
27+
}
28+
29+
return serialize(element, intrinsicRepresentation, finalAttributes, intrisicRepresentationBeforeChildren)
30+
}
31+
32+
private func hasNonEmptyTextChildren(node: ElementNode) -> Bool {
33+
var result = false
34+
var whiteSpaces = CharacterSet.whitespacesAndNewlines
35+
whiteSpaces.remove(charactersIn: String(.paragraphSeparator))
36+
for node in node.children {
37+
if let text = node as? TextNode, !text.contents.trimmingCharacters(in: whiteSpaces).isEmpty {
38+
return true
39+
}
40+
if let element = node as? ElementNode, !element.isNodeType(.ol) && !element.isNodeType(.ul) {
41+
result = result || hasNonEmptyTextChildren(node: element)
42+
}
43+
}
44+
return result
45+
}
46+
47+
private func hasNestedList(node: ElementNode) -> Bool {
48+
for node in node.children {
49+
if let element = node as? ElementNode, element.isNodeType(.ol) || element.isNodeType(.ul) {
50+
return true
51+
}
52+
}
53+
return false
54+
}
55+
}

0 commit comments

Comments
 (0)