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

src/server/public/abejs/scripts/utils/iframe.js   A

Complexity

Total Complexity 19
Complexity/F 3.8

Size

Lines of Code 48
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
nc 1
dl 0
loc 48
cc 0
rs 10
noi 1
wmc 19
mnd 4
bc 11
fnc 5
bpm 2.2
cpm 3.8

3 Functions

Rating   Name   Duplication   Size   Complexity  
C iframe.js ➔ IframeGetComment 0 23 11
A iframe.js ➔ IframeNode 0 7 2
A iframe.js ➔ IframeCommentNode 0 10 1
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
}