Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d1536e7

Browse files
committed
perf(jqLite): don't recreate the Node.contains polyfill
1 parent a191314 commit d1536e7

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/jqLite.js

+25-23
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,30 @@ wrapMap.optgroup = wrapMap.option;
156156
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
157157
wrapMap.th = wrapMap.td;
158158

159+
160+
var elementContains = document.head.contains ||
161+
document.head.compareDocumentPosition
162+
? function( a, b ) {
163+
// jshint bitwise: false
164+
var adown = a.nodeType === 9 ? a.documentElement : a,
165+
bup = b && b.parentNode;
166+
return a === bup || !!( bup && bup.nodeType === 1 && (
167+
adown.contains ?
168+
adown.contains( bup ) :
169+
a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16
170+
));
171+
}
172+
: function( a, b ) {
173+
if ( b ) {
174+
while ( (b = b.parentNode) ) {
175+
if ( b === a ) {
176+
return true;
177+
}
178+
}
179+
}
180+
return false;
181+
};
182+
159183
function jqLiteIsTextNode(html) {
160184
return !HTML_REGEXP.test(html);
161185
}
@@ -782,28 +806,6 @@ forEach({
782806

783807
if (!eventFns) {
784808
if (type == 'mouseenter' || type == 'mouseleave') {
785-
var contains = document.body.contains || document.body.compareDocumentPosition ?
786-
function( a, b ) {
787-
// jshint bitwise: false
788-
var adown = a.nodeType === 9 ? a.documentElement : a,
789-
bup = b && b.parentNode;
790-
return a === bup || !!( bup && bup.nodeType === 1 && (
791-
adown.contains ?
792-
adown.contains( bup ) :
793-
a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16
794-
));
795-
} :
796-
function( a, b ) {
797-
if ( b ) {
798-
while ( (b = b.parentNode) ) {
799-
if ( b === a ) {
800-
return true;
801-
}
802-
}
803-
}
804-
return false;
805-
};
806-
807809
events[type] = [];
808810

809811
// Refer to jQuery's implementation of mouseenter & mouseleave
@@ -815,7 +817,7 @@ forEach({
815817
var target = this, related = event.relatedTarget;
816818
// For mousenter/leave call the handler if related is outside the target.
817819
// NB: No relatedTarget if the mouse left/entered the browser window
818-
if ( !related || (related !== target && !contains(target, related)) ){
820+
if ( !related || (related !== target && !elementContains(target, related)) ){
819821
handle(event, type);
820822
}
821823
});

0 commit comments

Comments
 (0)