@@ -38,79 +38,29 @@ extension HTMLKitUtilities {
38
38
) -> String {
39
39
var result : String = " "
40
40
result. reserveCapacity ( html. count)
41
- let tagRegex = " [^/>]+ "
42
- let openElementRanges = html. ranges ( of: try ! Regex ( " (< \( tagRegex) >) " ) )
43
- let closeElementRanges = html. ranges ( of: try ! Regex ( " (</ \( tagRegex) >) " ) )
44
-
45
- var openingRangeIndex = 0
46
- var ignoredClosingTags : Set < Range < String . Index > > = [ ]
47
- for openingRange in openElementRanges {
48
- let tag = html [ openingRange]
49
- result += tag
50
- let closure = Self . defaultPreservedWhitespaceTags. contains ( tag) || preservingWhitespaceForTags. contains ( tag) ? minifyAppendAll : minifyAppendIfPermitted
51
- let closestClosingRange = closeElementRanges. first ( where: { $0. lowerBound > openingRange. upperBound } )
52
- if let nextOpeningRange = openElementRanges. getPositive ( openingRangeIndex + 1 ) {
53
- var i = openingRange. upperBound
54
- var lowerBound = nextOpeningRange. lowerBound
55
- if let closestClosingRange {
56
- if closestClosingRange. upperBound < lowerBound {
57
- lowerBound = closestClosingRange. upperBound
58
- }
59
- if closestClosingRange. lowerBound < nextOpeningRange. lowerBound {
60
- ignoredClosingTags. insert ( closestClosingRange)
61
- }
62
- }
63
- // anything after the opening tag, upto the end of the next closing tag
64
- closure ( html, & i, lowerBound, & result)
65
- // anything after the closing tag and before the next opening tag
66
- while i < nextOpeningRange. lowerBound {
67
- let char = html [ i]
68
- if !char. isNewline {
69
- result. append ( char)
41
+ let tagRanges = html. ranges ( of: try ! Regex ( " (<[^>]+>) " ) )
42
+ var tagIndex = 0
43
+ for tagRange in tagRanges {
44
+ let originalTag = html [ tagRange]
45
+ var tag = originalTag. split ( separator: " " ) [ 0 ]
46
+ if tag. last != " > " {
47
+ tag. append ( " > " )
48
+ }
49
+ result += originalTag
50
+ if let next = tagRanges. get ( tagIndex + 1 ) {
51
+ let slice = html [ tagRange. upperBound..< next. lowerBound]
52
+ if !( Self . defaultPreservedWhitespaceTags. contains ( tag) || preservingWhitespaceForTags. contains ( tag) ) {
53
+ for char in slice {
54
+ if !( char. isWhitespace || char. isNewline) {
55
+ result. append ( char)
56
+ }
70
57
}
71
- html. formIndex ( after: & i)
58
+ } else {
59
+ result += slice
72
60
}
73
- } else if let closestClosingRange {
74
- // anything after the opening tag and before the next closing tag
75
- var i = openingRange. upperBound
76
- closure ( html, & i, closestClosingRange. lowerBound, & result)
77
- }
78
- openingRangeIndex += 1
79
- }
80
- for closingRange in closeElementRanges {
81
- if !ignoredClosingTags. contains ( closingRange) {
82
- result += html [ closingRange]
83
61
}
62
+ tagIndex += 1
84
63
}
85
64
return result
86
65
}
87
- }
88
-
89
- // MARK: append
90
- extension HTMLKitUtilities {
91
- @usableFromInline
92
- static func minifyAppendAll(
93
- html: String ,
94
- i: inout String . Index ,
95
- bound: String . Index ,
96
- result: inout String
97
- ) {
98
- result += html [ i..< bound]
99
- i = bound
100
- }
101
- @usableFromInline
102
- static func minifyAppendIfPermitted(
103
- html: String ,
104
- i: inout String . Index ,
105
- bound: String . Index ,
106
- result: inout String
107
- ) {
108
- while i < bound {
109
- let char = html [ i]
110
- if !( char. isWhitespace || char. isNewline) {
111
- result. append ( char)
112
- }
113
- html. formIndex ( after: & i)
114
- }
115
- }
116
66
}
0 commit comments