Skip to content

change inDoc function to work properly in phantom.js #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var config = require('../config')

/**
* Check if a node is in the document.
* Note: document.documentElement.contains should work here but doesn't seem to
* work properly in phantom.js making unit testing difficult.
*
* @param {Node} node
* @return {Boolean}
Expand All @@ -12,7 +14,13 @@ var doc =
document.documentElement

exports.inDoc = function (node) {
return doc && doc.contains(node)
var adown = doc.nodeType === 9 ? doc.documentElement : doc
var bup = node && node.parentNode
return doc === bup || !!( bup && bup.nodeType === 1 && (
adown.contains
? adown.contains( bup )
: doc.compareDocumentPosition && doc.compareDocumentPosition( bup ) & 16
));
}

/**
Expand All @@ -35,7 +43,7 @@ exports.attr = function (node, attr) {
* Insert el before target
*
* @param {Element} el
* @param {Element} target
* @param {Element} target
*/

exports.before = function (el, target) {
Expand All @@ -46,7 +54,7 @@ exports.before = function (el, target) {
* Insert el after target
*
* @param {Element} el
* @param {Element} target
* @param {Element} target
*/

exports.after = function (el, target) {
Expand All @@ -71,7 +79,7 @@ exports.remove = function (el) {
* Prepend el to target
*
* @param {Element} el
* @param {Element} target
* @param {Element} target
*/

exports.prepend = function (el, target) {
Expand Down Expand Up @@ -194,4 +202,4 @@ exports.extractContent = function (el) {
}
}
return rawContent
}
}