Просмотр исходного кода

also check iframes in inDoc (fix #2831)

Evan You 10 лет назад
Родитель
Сommit
bd8c46109e
2 измененных файлов с 27 добавлено и 3 удалено
  1. 15 3
      src/util/dom.js
  2. 12 0
      test/unit/specs/util/dom_spec.js

+ 15 - 3
src/util/dom.js

@@ -36,12 +36,24 @@ export function query (el) {
  * @return {Boolean}
  */
 
-export function inDoc (node) {
-  var doc = document.documentElement
+export function inDoc (node, win) {
+  win = win || window
+  var doc = win.document.documentElement
   var parent = node && node.parentNode
-  return doc === node ||
+  var isInDoc = doc === node ||
     doc === parent ||
     !!(parent && parent.nodeType === 1 && (doc.contains(parent)))
+  if (!isInDoc) {
+    var frames = win.frames
+    if (frames) {
+      for (var i = 0; i < frames.length; i++) {
+        if (inDoc(node, frames[i])) {
+          return true
+        }
+      }
+    }
+  }
+  return isInDoc
 }
 
 /**

+ 12 - 0
test/unit/specs/util/dom_spec.js

@@ -22,6 +22,18 @@ describe('Util - DOM', function () {
     expect(_.inDoc(target)).toBe(false)
   })
 
+  it('inDoc (iframe)', function (done) {
+    var f = document.createElement('iframe')
+    f.onload = function () {
+      f.contentWindow.document.body.appendChild(target)
+      expect(_.inDoc(target)).toBe(true)
+      document.body.removeChild(f)
+      done()
+    }
+    document.body.appendChild(f)
+    f.src = "about:blank"
+  })
+
   it('getAttr', function () {
     target.setAttribute('v-test', 'ok')
     var val = _.getAttr(target, 'v-test')