Skip to content

Commit cc196ed

Browse files
use different approach to minify html (unit tests now pass)
1 parent 26c72f7 commit cc196ed

File tree

2 files changed

+20
-70
lines changed

2 files changed

+20
-70
lines changed

Sources/HTMLKitUtilities/Minify.swift

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,79 +38,29 @@ extension HTMLKitUtilities {
3838
) -> String {
3939
var result:String = ""
4040
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+
}
7057
}
71-
html.formIndex(after: &i)
58+
} else {
59+
result += slice
7260
}
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]
8361
}
62+
tagIndex += 1
8463
}
8564
return result
8665
}
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-
}
11666
}

Tests/HTMLKitTests/MinifyTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import HTMLKit
1212

1313
struct MinifyTests {
1414
@Test func minifyHTML() {
15-
var expected = "<html><body><p>\ndude&dude </p>r ly<div>what</div></body></html>"
15+
var expected = "<html><body><p>\ndude&dude </p>rly<div>what</div></body></html>"
1616
var result:String = HTMLKitUtilities.minify(html: "\n<html>\n <body><p>\ndude&dude </p>r ly\n<div>\nwh at</div></body>\n</html>")
1717
#expect(expected == result)
1818

0 commit comments

Comments
 (0)