Skip to content

Commit

Permalink
Fix complex pattern hanging forever (SimonFairbairn#139).
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPartzsch committed Mar 7, 2024
1 parent e72b393 commit 26e5c8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
12 changes: 10 additions & 2 deletions Sources/SwiftyMarkdown/SwiftyScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,28 @@ class SwiftyScanner {
} else {
var remainingTags = min(openRange.upperBound - openRange.lowerBound, closeRange.upperBound - closeRange.lowerBound) + 1
while remainingTags > 0 {
if remainingTags >= self.rule.maxTags {
let shouldAppendStyle = remainingTags >= self.rule.maxTags
if shouldAppendStyle {
remainingTags -= self.rule.maxTags
if let style = self.rule.styles[ self.rule.maxTags ] {
if !styles.contains(where: { $0.isEqualTo(style)}) {
styles.append(style)
}
}
}
if let style = self.rule.styles[remainingTags] {


let remainingTagsStyle = self.rule.styles[remainingTags]
if let style = remainingTagsStyle {
remainingTags -= remainingTags
if !styles.contains(where: { $0.isEqualTo(style)}) {
styles.append(style)
}
}

if !shouldAppendStyle && remainingTagsStyle == nil {
break
}
}

for idx in (openRange.upperBound)...(closeRange.lowerBound) {
Expand Down
55 changes: 30 additions & 25 deletions Tests/SwiftyMarkdownTests/SwiftyMarkdownAttributedStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,37 @@
// Copyright © 2019 Voyage Travel Apps. All rights reserved.
//

import XCTest
@testable import SwiftyMarkdown
import XCTest

class SwiftyMarkdownAttributedStringTests: XCTestCase {

func testThatAttributesAreAppliedCorrectly() {

let string = """
# Heading 1
A more *complicated* example. This one has **it all**. Here is a [link](http://voyagetravelapps.com/).
## Heading 2
## Heading 3
> This one is a blockquote
"""
let md = SwiftyMarkdown(string: string)
let attributedString = md.attributedString()

XCTAssertNotNil(attributedString)

XCTAssertEqual(attributedString.string, "Heading 1\n\nA more complicated example. This one has it all. Here is a link.\n\nHeading 2\n\nHeading 3\n\nThis one is a blockquote")


}

func testThatAttributesAreAppliedCorrectly() {
let string = """
# Heading 1
A more *complicated* example. This one has **it all**. Here is a [link](http://voyagetravelapps.com/).
## Heading 2
## Heading 3
> This one is a blockquote
"""

let md = SwiftyMarkdown(string: string)
let attributedString = md.attributedString()

XCTAssertNotNil(attributedString)
XCTAssertEqual(attributedString.string, "Heading 1\n\nA more complicated example. This one has it all. Here is a link.\n\nHeading 2\n\nHeading 3\n\nThis one is a blockquote")
}

func testComplexPatternDoesNotHangForever() {
let string = "**~*~~~*~*~**~*~* h e a r d ***~*~*~**~*~~~*"

let md = SwiftyMarkdown(string: string)
let attributedString = md.attributedString()

XCTAssertNotNil(attributedString)
XCTAssertEqual(attributedString.string, "**~~*~**~~ h e a r d ***~~~**~")
}
}

0 comments on commit 26e5c8a

Please sign in to comment.