Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to initialise a DownView using a custom template bundle #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Down.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
8AFAEB071E6E331700E09B68 /* DownViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D41689B21CFFE28200E5802B /* DownViewTests.swift */; };
8AFAEB081E6E331700E09B68 /* NSAttributedStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F948DB1D00A4A800C9C0F6 /* NSAttributedStringTests.swift */; };
8AFAEB091E6E331700E09B68 /* StringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D438696B1D00D27700E95A1F /* StringTests.swift */; };
907C64651EC133780095FEE1 /* TestDownView.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 907C64621EC120530095FEE1 /* TestDownView.bundle */; };
90A40A9C1EC03292004F2E91 /* Down.framework in Copy Bundled Frameworks */ = {isa = PBXBuildFile; fileRef = 8A569F401E6B3E50008BE2AC /* Down.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -94,6 +95,7 @@
/* Begin PBXFileReference section */
8A569F401E6B3E50008BE2AC /* Down.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Down.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8AFAEAFB1E6E32E900E09B68 /* DownTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DownTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
907C64621EC120530095FEE1 /* TestDownView.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = TestDownView.bundle; sourceTree = "<group>"; };
90A40A951EC02FF6004F2E91 /* Down-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Down-Info.plist"; sourceTree = "<group>"; };
90A40A961EC02FF6004F2E91 /* DownTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "DownTests-Info.plist"; sourceTree = "<group>"; };
90A40A981EC0309A004F2E91 /* Deployment-Targets.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Deployment-Targets.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +173,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
907C64611EC120530095FEE1 /* Fixtures */ = {
isa = PBXGroup;
children = (
907C64621EC120530095FEE1 /* TestDownView.bundle */,
);
path = Fixtures;
sourceTree = "<group>";
};
90A40A971EC0309A004F2E91 /* Configurations */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -232,6 +242,7 @@
children = (
D4201EC51CFA59A5008EEC6E /* BindingTests.swift */,
D41689B21CFFE28200E5802B /* DownViewTests.swift */,
907C64611EC120530095FEE1 /* Fixtures */,
D4F948DB1D00A4A800C9C0F6 /* NSAttributedStringTests.swift */,
D438696B1D00D27700E95A1F /* StringTests.swift */,
);
Expand Down Expand Up @@ -377,6 +388,7 @@
buildConfigurationList = 8AFAEB031E6E32E900E09B68 /* Build configuration list for PBXNativeTarget "DownTests" */;
buildPhases = (
8AFAEAF71E6E32E900E09B68 /* Sources */,
907C64641EC133740095FEE1 /* Resources */,
8AFAEAF81E6E32E900E09B68 /* Frameworks */,
90A40A9B1EC03282004F2E91 /* Copy Bundled Frameworks */,
);
Expand Down Expand Up @@ -437,6 +449,14 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
907C64641EC133740095FEE1 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
907C64651EC133780095FEE1 /* TestDownView.bundle in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
18 changes: 12 additions & 6 deletions Source/Views/DownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ open class DownView: WKWebView {
- parameter frame: The frame size of the web view
- parameter markdownString: A string containing CommonMark Markdown
- parameter openLinksInBrowser: Whether or not to open links using an external browser
- parameter templateBundle: Optional custom template bundle. Leaving this as `nil` will use the bundle included with Down.
- parameter didLoadSuccessfully: Optional callback for when the web content has loaded successfully

- returns: An instance of Self
*/
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, didLoadSuccessfully: DownViewClosure? = nil) throws {
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, templateBundle: Bundle? = nil, didLoadSuccessfully: DownViewClosure? = nil) throws {
self.didLoadSuccessfully = didLoadSuccessfully

if let templateBundle = templateBundle {
self.bundle = templateBundle
} else {
let classBundle = Bundle(for: DownView.self)
let url = classBundle.url(forResource: "DownView", withExtension: "bundle")!
self.bundle = Bundle(url: url)!
}

super.init(frame: frame, configuration: WKWebViewConfiguration())

if openLinksInBrowser || didLoadSuccessfully != nil { navigationDelegate = self }
Expand Down Expand Up @@ -58,11 +68,7 @@ open class DownView: WKWebView {

// MARK: - Private Properties

fileprivate let bundle: Bundle = {
let bundle = Bundle(for: DownView.self)
let url = bundle.url(forResource: "DownView", withExtension: "bundle")!
return Bundle(url: url)!
}()
let bundle: Bundle

fileprivate lazy var baseURL: URL = {
return self.bundle.url(forResource: "index", withExtension: "html")!
Expand Down
27 changes: 27 additions & 0 deletions Tests/DownViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ class DownViewTests: XCTestCase {
}
}

func testInstantiationWithCustomTemplateBundle() {
let expect1 = expectation(description: "DownView accepts and uses a custom theme bundle")
guard
let bundle = Bundle(for: type(of: self)).url(forResource: "TestDownView", withExtension: "bundle"),
let templateBundle = Bundle(url: bundle)
else {
XCTFail("Test template bundle not found in test target!")
return
}

var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)", templateBundle: templateBundle, didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))
XCTAssertTrue(htmlString!.contains("But also, custom HTML!"))

expect1.fulfill()
}
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}
}

fileprivate extension DownViewTests {
Expand Down
8 changes: 8 additions & 0 deletions Tests/Fixtures/TestDownView.bundle/css/down.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Tests/Fixtures/TestDownView.bundle/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640"/>
<link charset="utf-8" href="css/down.min.css" rel="stylesheet">
<script charset="utf-8" src="js/highlight.min.js" type="text/javascript"></script>
<script charset="utf-8" src="js/down.js" type="text/javascript"></script>
<title></title>
</head>
<body>
DOWN_HTML
But also, custom HTML!
</body>
</html>
1 change: 1 addition & 0 deletions Tests/Fixtures/TestDownView.bundle/js/down.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hljs.initHighlightingOnLoad();
2 changes: 2 additions & 0 deletions Tests/Fixtures/TestDownView.bundle/js/highlight.min.js

Large diffs are not rendered by default.