Passed
Branch master (c2f34b)
by greg
02:22
created

iframe.js ➔ IframeGetComment   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
nc 21
nop 6
dl 0
loc 23
cc 11
rs 5.3929

How to fix   Complexity   

Complexity

Complex classes like iframe.js ➔ IframeGetComment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*global document */
2
3
export function IframeDocument (frameId){
4
  let iframe = document.querySelector(frameId)
5
  let iframeDocument = iframe != null ? iframe.contentDocument || iframe.contentWindow.document : null
6
  return iframeDocument
7
}
8
9
export function IframeNode (frameId, selector){
10
  var iframe = IframeDocument(frameId)
11
  if (iframe) {
12
    return iframe.querySelectorAll(selector.replace(/\[([0-9]*)\]/g, '$1'))
13
  }
14
  return []
15
}
16
17
function IframeGetComment(frameId, prop, val, meth, nd, useSelf ) {
18
  prop = 'nodeType'
19
  val = 8
20
  meth = null
21
  var r=[], any= IframeGetComment[val]===true
22
  var iframe = IframeDocument(frameId)
23
  if (iframe == 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...
24
  nd = nd||IframeDocument(frameId).documentElement
25
26
  if(nd.constructor===Array){
27
    nd = {childNodes:nd}
28
  }
29
  for(var cn = nd.childNodes, i=0, mx=cn.length;i<mx;i++) {
30
    var it=cn[i]
31
    if(it.childNodes.length && !useSelf) {
32
      r = r.concat(IframeGetComment(frameId, prop, val, meth, it, useSelf ))
33
    }
34
    if( any ? it[prop] : (it[prop]!==undefined && (meth ? ''[meth] &&  String(it[prop])[meth](val) : it[prop]==val))) {
35
      r[r.length]=it
36
    }
37
  }
38
  return r
39
}
40
41
export function IframeCommentNode(frameId, key) {
42
  var nodes = IframeGetComment(frameId, 'nodeType', 8, null, null)
43
  var found = []
44
  Array.prototype.forEach.call(nodes, (node) => {
45
    if(node.textContent.indexOf(key) > -1) {
46
      found.push(node)
47
    }
48
  })
49
  return found
50
}