Skip to content

Commit c4cbbe0

Browse files
Fix incorrect regular expression
1 parent 7ed16a8 commit c4cbbe0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

OpenGraph/OpenGraphParser.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension OpenGraphParser {
2424
options: []
2525
)
2626
let contentRegexp = try! NSRegularExpression(
27-
pattern: "\\scontent=(?:\"|\')(.*?)(?:\"|\')(?:\\s|>)",
27+
pattern: "\\scontent=\"(.*?)\"",
2828
options: []
2929
)
3030

@@ -40,7 +40,14 @@ extension OpenGraphParser {
4040
range: NSMakeRange(0, metaTag.count))
4141
guard let propertyResult = propertyMatches.first else { return nil }
4242

43-
let contentMatches = contentRegexp.matches(in: metaTag, options: [], range: NSMakeRange(0, metaTag.count))
43+
var contentMatches = contentRegexp.matches(in: metaTag, options: [], range: NSMakeRange(0, metaTag.count))
44+
if contentMatches.first == nil {
45+
let contentRegexp = try! NSRegularExpression(
46+
pattern: "\\scontent='(.*?)'",
47+
options: []
48+
)
49+
contentMatches = contentRegexp.matches(in: metaTag, options: [], range: NSMakeRange(0, metaTag.count))
50+
}
4451
guard let contentResult = contentMatches.first else { return nil }
4552

4653
let nsMetaTag = metaTag as NSString

Tests/OpenGraphTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,16 @@ class OpenGraphTests: XCTestCase {
155155
XCTAssert(error! is OpenGraphParseError)
156156
}
157157
}
158+
159+
func testParsing() {
160+
var html = "<meta content=\"It's a description\" property=\"og:description\" />"
161+
XCTAssert(OpenGraph(htmlString: html)[.description] == "It's a description")
162+
html = "<meta property=\"og:title\" content=\"It's a title contains single quote\"/>"
163+
XCTAssert(OpenGraph(htmlString: html)[.title] == "It's a title contains single quote")
164+
165+
html = "<meta content='It&#39;s a description' property='og:description' />"
166+
XCTAssert(OpenGraph(htmlString: html)[.description] == "It&#39;s a description")
167+
html = "<meta content='It is a title contains double quote \"' property='og:title' />"
168+
XCTAssert(OpenGraph(htmlString: html)[.title] == "It is a title contains double quote \"")
169+
}
158170
}

0 commit comments

Comments
 (0)