Completed
Push — master ( 991ce7...64aac0 )
by greg
01:57
created

src/cli/cms/editor/handlebars/compileAbe.js   A

Complexity

Total Complexity 19
Complexity/F 19

Size

Lines of Code 61
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 19
c 2
b 0
f 0
nc 1
mnd 3
bc 8
fnc 1
dl 0
loc 61
rs 10
bpm 8
cpm 19
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
C compileAbe.js ➔ compileAbe 0 49 19
1
import Handlebars from 'handlebars'
2
import abeEngine from './abeEngine'
3
import xss from 'xss'
4
5
import {
6
  config
7
} from '../../../'
8
9
/**
10
 * Abe handlebar helper, that retrieve text to add to handlebars templating engine
11
 * @return {String} the string to replace {{ handlebars_key }}
12
 */
13
export default function compileAbe(){
14
  var content = abeEngine.instance.content
15
  if(typeof arguments[0].hash['key'] === 'undefined' || arguments[0].hash['key'] === null) return ''
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
  var key
17
  var hash
18
  var value
19
  var testXSS
20
  
21
  if(arguments[0].hash['key'].indexOf('}-') > 0){
22
    key = arguments[0].hash['key'].split('-')
23
    key = key[key.length - 1]
24
    hash = arguments[0].hash
25
    hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
26
    try{
27
      value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
28
    }
29
    catch(e){
30
      value = ''
31
    }
32
    if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
33
      value = ''
34
    }
35
    if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
36
      testXSS = xss(value.replace(/"/g, '"'), {
37
        'whiteList': config.htmlWhiteList,
38
        stripIgnoreTag: true
39
      })
40
      return new Handlebars.SafeString(testXSS)
41
    }
42
    return value.replace(/%27/, '\'')
43
  }
44
45
  key = arguments[0].hash['key'].replace('.', '-')
46
47
  hash = arguments[0].hash
48
  value = ((content) ? content[hash.key.replace('.', '-')] : hash.key)
49
  if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
50
    value = ''
51
  }
52
  
53
  if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
54
    testXSS = xss(value.replace(/"/g, '"'), {
55
      'whiteList': config.htmlWhiteList,
56
      stripIgnoreTag: true
57
    })
58
    return new Handlebars.SafeString(testXSS)
59
  }
60
  return value.replace(/%27/, '\'')
61
}
62