context" );
if ( QUnit.jQuerySelectors ) {
assert.deepEqual( jQuery.find( "* > *", context ), [], "
context (no results)" );
} else {
assert.ok( "skip", "The whole selector not required to be under context in selector-native" );
}
context.removeAttribute( "id" );
assert.deepEqual( jQuery.find( "*", context ), q( "nothiddendivchild" ), "no-id element context" );
if ( QUnit.jQuerySelectors ) {
assert.deepEqual( jQuery.find( "* > *", context ), [], "no-id element context (no results)" );
} else {
assert.ok( "skip", ":contains not supported in selector-native" );
}
assert.strictEqual( context.getAttribute( "id" ) || "", "", "id not added by no-id selection" );
context = document.getElementById( "lengthtest" );
assert.deepEqual( jQuery.find( "input", context ), q( "length", "idTest" ), "
" );
iframeDoc.close();
} );
QUnit.test( "jQuery.escapeSelector", function( assert ) {
assert.expect( 58 );
// Edge cases
assert.equal( jQuery.escapeSelector(), "undefined", "Converts undefined to string" );
assert.equal( jQuery.escapeSelector( "-" ), "\\-", "Escapes standalone dash" );
assert.equal( jQuery.escapeSelector( "-a" ), "-a", "Doesn't escape leading dash followed by non-number" );
assert.equal( jQuery.escapeSelector( "--" ), "--", "Doesn't escape standalone double dash" );
assert.equal( jQuery.escapeSelector( "\uFFFD" ), "\uFFFD",
"Doesn't escape standalone replacement character" );
assert.equal( jQuery.escapeSelector( "a\uFFFD" ), "a\uFFFD",
"Doesn't escape trailing replacement character" );
assert.equal( jQuery.escapeSelector( "\uFFFDb" ), "\uFFFDb",
"Doesn't escape leading replacement character" );
assert.equal( jQuery.escapeSelector( "a\uFFFDb" ), "a\uFFFDb",
"Doesn't escape embedded replacement character" );
// Derived from CSSOM tests
// https://test.csswg.org/harness/test/cssom-1_dev/section/7.1/
// String conversion
assert.equal( jQuery.escapeSelector( true ), "true", "Converts boolean true to string" );
assert.equal( jQuery.escapeSelector( false ), "false", "Converts boolean true to string" );
assert.equal( jQuery.escapeSelector( null ), "null", "Converts null to string" );
assert.equal( jQuery.escapeSelector( "" ), "", "Doesn't modify empty string" );
// Null bytes
assert.equal( jQuery.escapeSelector( "\0" ), "\uFFFD",
"Escapes null-character input as replacement character" );
assert.equal( jQuery.escapeSelector( "a\0" ), "a\uFFFD",
"Escapes trailing-null input as replacement character" );
assert.equal( jQuery.escapeSelector( "\0b" ), "\uFFFDb",
"Escapes leading-null input as replacement character" );
assert.equal( jQuery.escapeSelector( "a\0b" ), "a\uFFFDb",
"Escapes embedded-null input as replacement character" );
// Number prefix
assert.equal( jQuery.escapeSelector( "0a" ), "\\30 a", "Escapes leading 0" );
assert.equal( jQuery.escapeSelector( "1a" ), "\\31 a", "Escapes leading 1" );
assert.equal( jQuery.escapeSelector( "2a" ), "\\32 a", "Escapes leading 2" );
assert.equal( jQuery.escapeSelector( "3a" ), "\\33 a", "Escapes leading 3" );
assert.equal( jQuery.escapeSelector( "4a" ), "\\34 a", "Escapes leading 4" );
assert.equal( jQuery.escapeSelector( "5a" ), "\\35 a", "Escapes leading 5" );
assert.equal( jQuery.escapeSelector( "6a" ), "\\36 a", "Escapes leading 6" );
assert.equal( jQuery.escapeSelector( "7a" ), "\\37 a", "Escapes leading 7" );
assert.equal( jQuery.escapeSelector( "8a" ), "\\38 a", "Escapes leading 8" );
assert.equal( jQuery.escapeSelector( "9a" ), "\\39 a", "Escapes leading 9" );
// Letter-number prefix
assert.equal( jQuery.escapeSelector( "a0b" ), "a0b", "Doesn't escape embedded 0" );
assert.equal( jQuery.escapeSelector( "a1b" ), "a1b", "Doesn't escape embedded 1" );
assert.equal( jQuery.escapeSelector( "a2b" ), "a2b", "Doesn't escape embedded 2" );
assert.equal( jQuery.escapeSelector( "a3b" ), "a3b", "Doesn't escape embedded 3" );
assert.equal( jQuery.escapeSelector( "a4b" ), "a4b", "Doesn't escape embedded 4" );
assert.equal( jQuery.escapeSelector( "a5b" ), "a5b", "Doesn't escape embedded 5" );
assert.equal( jQuery.escapeSelector( "a6b" ), "a6b", "Doesn't escape embedded 6" );
assert.equal( jQuery.escapeSelector( "a7b" ), "a7b", "Doesn't escape embedded 7" );
assert.equal( jQuery.escapeSelector( "a8b" ), "a8b", "Doesn't escape embedded 8" );
assert.equal( jQuery.escapeSelector( "a9b" ), "a9b", "Doesn't escape embedded 9" );
// Dash-number prefix
assert.equal( jQuery.escapeSelector( "-0a" ), "-\\30 a", "Escapes 0 after leading dash" );
assert.equal( jQuery.escapeSelector( "-1a" ), "-\\31 a", "Escapes 1 after leading dash" );
assert.equal( jQuery.escapeSelector( "-2a" ), "-\\32 a", "Escapes 2 after leading dash" );
assert.equal( jQuery.escapeSelector( "-3a" ), "-\\33 a", "Escapes 3 after leading dash" );
assert.equal( jQuery.escapeSelector( "-4a" ), "-\\34 a", "Escapes 4 after leading dash" );
assert.equal( jQuery.escapeSelector( "-5a" ), "-\\35 a", "Escapes 5 after leading dash" );
assert.equal( jQuery.escapeSelector( "-6a" ), "-\\36 a", "Escapes 6 after leading dash" );
assert.equal( jQuery.escapeSelector( "-7a" ), "-\\37 a", "Escapes 7 after leading dash" );
assert.equal( jQuery.escapeSelector( "-8a" ), "-\\38 a", "Escapes 8 after leading dash" );
assert.equal( jQuery.escapeSelector( "-9a" ), "-\\39 a", "Escapes 9 after leading dash" );
// Double dash prefix
assert.equal( jQuery.escapeSelector( "--a" ), "--a", "Doesn't escape leading double dash" );
// Miscellany
assert.equal( jQuery.escapeSelector( "\x01\x02\x1E\x1F" ), "\\1 \\2 \\1e \\1f ",
"Escapes C0 control characters" );
assert.equal( jQuery.escapeSelector( "\x80\x2D\x5F\xA9" ), "\x80\x2D\x5F\xA9",
"Doesn't escape general punctuation or non-ASCII ISO-8859-1 characters" );
assert.equal(
jQuery.escapeSelector( "\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90" +
"\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F" ),
"\\7f \x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90" +
"\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F",
"Escapes DEL control character"
);
assert.equal( jQuery.escapeSelector( "\xA0\xA1\xA2" ), "\xA0\xA1\xA2",
"Doesn't escape non-ASCII ISO-8859-1 characters" );
assert.equal( jQuery.escapeSelector( "a0123456789b" ), "a0123456789b",
"Doesn't escape embedded numbers" );
assert.equal( jQuery.escapeSelector( "abcdefghijklmnopqrstuvwxyz" ), "abcdefghijklmnopqrstuvwxyz",
"Doesn't escape lowercase ASCII letters" );
assert.equal( jQuery.escapeSelector( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ), "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"Doesn't escape uppercase ASCII letters" );
assert.equal( jQuery.escapeSelector( "\x20\x21\x78\x79" ), "\\ \\!xy",
"Escapes non-word ASCII characters" );
// Astral symbol (U+1D306 TETRAGRAM FOR CENTRE)
assert.equal( jQuery.escapeSelector( "\uD834\uDF06" ), "\uD834\uDF06",
"Doesn't escape astral characters" );
// Lone surrogates
assert.equal( jQuery.escapeSelector( "\uDF06" ), "\uDF06", "Doesn't escape lone low surrogate" );
assert.equal( jQuery.escapeSelector( "\uD834" ), "\uD834", "Doesn't escape lone high surrogate" );
} );
function testAttr( doc, assert ) {
assert.expect( 9 );
var el;
if ( doc ) {
// XML
el = doc.createElement( "input" );
el.setAttribute( "type", "checkbox" );
} else {
// Set checked on creation by creating with a fragment
// See https://jsfiddle.net/8sVgA/1/show/light in oldIE
el = jQuery( "
" )[ 0 ];
}
// Set it again for good measure
el.setAttribute( "checked", "checked" );
el.setAttribute( "id", "id" );
el.setAttribute( "value", "on" );
assert.strictEqual( jQuery.find.attr( el, "nonexistent" ), null, "nonexistent" );
assert.strictEqual( jQuery.find.attr( el, "id" ), "id", "existent" );
assert.strictEqual( jQuery.find.attr( el, "value" ), "on", "value" );
assert.strictEqual( jQuery.find.attr( el, "checked" ), "checked", "boolean" );
assert.strictEqual( jQuery.find.attr( el, "href" ), null, "interpolation risk" );
assert.strictEqual( jQuery.find.attr( el, "constructor" ), null,
"Object.prototype property \"constructor\" (negative)" );
assert.strictEqual( jQuery.find.attr( el, "watch" ), null,
"Gecko Object.prototype property \"watch\" (negative)" );
el.setAttribute( "constructor", "foo" );
el.setAttribute( "watch", "bar" );
assert.strictEqual( jQuery.find.attr( el, "constructor" ), "foo",
"Object.prototype property \"constructor\"" );
assert.strictEqual( jQuery.find.attr( el, "watch" ), "bar",
"Gecko Object.prototype property \"watch\"" );
}
// jQuery.find.attr is a private API but we're testing it on `3.x-stable` to
// make sure backwards compatibility with Sizzle is maintained on that branch.
QUnit.test( "jQuery.find.attr (HTML)", function( assert ) {
testAttr( null, assert );
} );
QUnit.test( "jQuery.find.attr (XML)", function( assert ) {
testAttr( jQuery.parseXML( "
" ), assert );
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom pseudos", function( assert ) {
assert.expect( 6 );
try {
jQuery.expr.filters.foundation = jQuery.expr.filters.root;
assert.deepEqual( jQuery.find( ":foundation" ), [ document.documentElement ], "Copy element filter with new name" );
} finally {
delete jQuery.expr.filters.foundation;
}
try {
jQuery.expr.setFilters.primary = jQuery.expr.setFilters.first;
assert.t( "Copy set filter with new name", "div#qunit-fixture :primary", [ "firstp" ] );
} finally {
delete jQuery.expr.setFilters.primary;
}
try {
jQuery.expr.filters.aristotlean = jQuery.expr.createPseudo( function() {
return function( elem ) {
return !!elem.id;
};
} );
assert.t( "Custom element filter", "#foo :aristotlean", [ "sndp", "en", "yahoo", "sap", "anchor2", "simon" ] );
} finally {
delete jQuery.expr.filters.aristotlean;
}
try {
jQuery.expr.filters.endswith = jQuery.expr.createPseudo( function( text ) {
return function( elem ) {
return jQuery.text( elem ).slice( -text.length ) === text;
};
} );
assert.t( "Custom element filter with argument", "a:endswith(ogle)", [ "google" ] );
} finally {
delete jQuery.expr.filters.endswith;
}
try {
jQuery.expr.setFilters.second = jQuery.expr.createPseudo( function() {
return jQuery.expr.createPseudo( function( seed, matches ) {
if ( seed[ 1 ] ) {
matches[ 1 ] = seed[ 1 ];
seed[ 1 ] = false;
}
} );
} );
assert.t( "Custom set filter", "#qunit-fixture p:second", [ "ap" ] );
} finally {
delete jQuery.expr.filters.second;
}
try {
jQuery.expr.setFilters.slice = jQuery.expr.createPseudo( function( argument ) {
var bounds = argument.split( ":" );
return jQuery.expr.createPseudo( function( seed, matches ) {
var i = bounds[ 1 ];
// Match elements found at the specified indexes
while ( --i >= bounds[ 0 ] ) {
if ( seed[ i ] ) {
matches[ i ] = seed[ i ];
seed[ i ] = false;
}
}
} );
} );
assert.t( "Custom set filter with argument", "#qunit-fixture p:slice(1:3)", [ "ap", "sndp" ] );
} finally {
delete jQuery.expr.filters.slice;
}
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "backwards-compatible custom pseudos", function( assert ) {
assert.expect( 3 );
try {
jQuery.expr.filters.icontains = function( elem, i, match ) {
return jQuery.text( elem ).toLowerCase().indexOf( ( match[ 3 ] || "" ).toLowerCase() ) > -1;
};
assert.t( "Custom element filter with argument", "a:icontains(THIS BLOG ENTRY)", [ "simon1" ] );
} finally {
delete jQuery.expr.filters.icontains;
}
try {
jQuery.expr.setFilters.podium = function( elements, argument ) {
var count = argument == null || argument === "" ? 3 : +argument;
return elements.slice( 0, count );
};
// Using TAG as the first token here forces this setMatcher into a fail state
// Where the descendent combinator was lost
assert.t( "Custom setFilter", "form#form :PODIUM", [ "label-for", "text1", "text2" ] );
assert.t( "Custom setFilter with argument", "#form input:Podium(1)", [ "text1" ] );
} finally {
delete jQuery.expr.setFilters.podium;
}
} );
// The jQuery.expr.attrHandle API is only maintained for backwards compatibility
// on the `3.x-stable` branch.
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom attribute getters", function( assert ) {
assert.expect( 2 );
var original = jQuery.expr.attrHandle.hreflang,
selector = "a:contains('mark')[hreflang='http://diveintomark.org/en']";
try {
jQuery.expr.attrHandle.hreflang = function( elem, name ) {
var href = elem.getAttribute( "href" ),
lang = elem.getAttribute( name );
return lang && ( href + lang );
};
assert.deepEqual( jQuery.find( selector, createWithFriesXML() ), [], "Custom attrHandle (preferred document)" );
assert.t( "Custom attrHandle (preferred document)", selector, [ "mark" ] );
} finally {
jQuery.expr.attrHandle.hreflang = original;
}
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "Ensure no 'undefined' handler is added", function( assert ) {
assert.expect( 1 );
assert.ok( !jQuery.expr.attrHandle.hasOwnProperty( "undefined" ),
"Extra attr handlers are not added to Expr.attrHandle (https://github.com/jquery/sizzle/issues/353)" );
} );