Skip to content

Commit

Permalink
Handle empty xliff files (#52)
Browse files Browse the repository at this point in the history
* Handle empty xliff files

The lib should not break when we parse this xliff file:

```xliff
<?xml version="1.0" encoding="UTF-8"?>                                      
<xliff 
  xmlns="urn:oasis:names:tc:xliff:document:1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  version="1.2" 
  xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd"></xliff>                                                                          ```

* Add test xliff 1.2 empty file

Co-authored-by: dhoko <[email protected]>
  • Loading branch information
dhoko and dhoko committed Feb 7, 2022
1 parent 074364a commit c3a59e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
46 changes: 25 additions & 21 deletions lib/xliff12ToJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@ const xliff12ToJsClb = (str, options, cb) => {

const xliffRoot = xmlObj.elements.find((ele) => ele.name === 'xliff')

const srcLang = xliffRoot.elements[0].attributes['source-language']
const trgLang = xliffRoot.elements[0].attributes['target-language']

result.sourceLanguage = srcLang
result.targetLanguage = trgLang
if (!result.targetLanguage) delete result.targetLanguage

result.resources = xliffRoot.elements.reduce((resources, file) => {
const namespace = options.namespace || file.attributes.original

const body = file.elements.find((e) => e.name === 'body')
body.elements = body.elements || []
const bodyChildren = body.elements.filter(
(child) => child.type !== 'comment'
)

// namespace
resources[namespace] = createUnits(bodyChildren)

return resources
}, {})
if (xliffRoot.elements && xliffRoot.elements.length) {
const srcLang = xliffRoot.elements[0].attributes['source-language']
const trgLang = xliffRoot.elements[0].attributes['target-language']

result.sourceLanguage = srcLang
result.targetLanguage = trgLang
if (!result.targetLanguage) delete result.targetLanguage

result.resources = xliffRoot.elements.reduce((resources, file) => {
const namespace = options.namespace || file.attributes.original

const body = file.elements.find((e) => e.name === 'body')
body.elements = body.elements || []
const bodyChildren = body.elements.filter(
(child) => child.type !== 'comment'
)

// namespace
resources[namespace] = createUnits(bodyChildren)

return resources
}, {})
} else {
result.resources = {}
}

if (cb) return cb(null, result)
return result
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/example_empty12.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"></xliff>
3 changes: 3 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@ module.exports = {
example_ignorable: {
js: require('./example_ignorable'),
xliff: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_ignorable.xliff')).toString())
},
example_empty:{
xliff12: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_empty12.xliff')).toString())
}
}
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ describe('xliff 1.2 source/target attributes', () => {
})
})

describe('xliff 1.2 empty file', () => {
test('xliff12ToJs', (fn) => (done) => {
fn(fixtures.example_empty.xliff12, (err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql({ resources: {} })
done()
})
})
})


describe('multi', () => {
test('xliff2js', (fn) => (done) => {
fn(fixtures.example_multi.xliff, (err, res) => {
Expand Down

0 comments on commit c3a59e0

Please sign in to comment.