Skip to content

Commit

Permalink
Preserve Indent/Whitespace from the original XML
Browse files Browse the repository at this point in the history
Use the XMLNode.Options.nodePreserveAll while creating the XMLDocument object.

This resolves #2
  • Loading branch information
remuslazar committed Oct 31, 2016
1 parent f4cebd1 commit e531ab0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xliff-tool/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Document: NSDocument {

class func getXMLDocument(from data: Data) throws -> XMLDocument {
do {
return try XMLDocument(data: data, options: Int(XMLNode.Options.documentTidyXML.rawValue))
return try XMLDocument(data: data, options: Int(XMLNode.Options.nodePreserveAll.rawValue))
} catch (let error as NSError) {
throw NSError(domain: NSCocoaErrorDomain, code: NSFileReadCorruptFileError, userInfo: [
NSLocalizedDescriptionKey: NSLocalizedString("Could not read file.", comment: "Read error description"),
Expand Down
21 changes: 21 additions & 0 deletions xliff-toolTests/xliff_toolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
//

import XCTest
@testable import XLIFFTool

extension String {
var lines:[String] {
var result:[String] = []
enumerateLines{ (line, _) in result.append(line) }
return result
}
}

class xliff_toolTests: XCTestCase {

Expand Down Expand Up @@ -43,6 +52,18 @@ class xliff_toolTests: XCTestCase {
XCTAssertEqual(xliffFile.files[0].items.first!.elements(forName: "source").first!.stringValue, "Text Cell")
}

// check of the xml file is saved while preserving all whitespace/line breaks from the original
func testSaveFormatting() {
let data = xliffDocument.xmlData
if let content = String(data: data, encoding: .utf8),
let originalContent = String(data: xliffData, encoding: .utf8) {
// check if the 3. last line is equal
XCTAssertEqual(
content.lines[content.lines.count - 3],
originalContent.lines[originalContent.lines.count - 3] )
}
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
Expand Down

0 comments on commit e531ab0

Please sign in to comment.