]> source.dussan.org Git - jquery.git/commitdiff
Selector: add jQuery.escapeSelector
authorTimmy Willison <timmywillisn@gmail.com>
Wed, 27 Jan 2016 17:57:04 +0000 (12:57 -0500)
committerTimmy Willison <timmywillisn@gmail.com>
Thu, 28 Jan 2016 22:25:41 +0000 (17:25 -0500)
Fixes gh-1761
Close gh-2878

src/selector-native.js
src/selector-sizzle.js
test/unit/selector.js

index ee81483426fa6b1f00b702342d9512a311c94d2c..3b2525d69ea4121d0c73874084b92bc6f64fb9b2 100644 (file)
@@ -37,7 +37,26 @@ var hasDuplicate, sortInput,
                documentElement.webkitMatchesSelector ||
                documentElement.mozMatchesSelector ||
                documentElement.oMatchesSelector ||
-               documentElement.msMatchesSelector;
+               documentElement.msMatchesSelector,
+
+       // CSS string/identifier serialization
+       // https://drafts.csswg.org/cssom/#common-serializing-idioms
+       rcssescape = /([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g,
+       fcssescape = function( ch, asCodePoint ) {
+               if ( asCodePoint ) {
+
+                       // U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER
+                       if ( ch === "\0" ) {
+                               return "\uFFFD";
+                       }
+
+                       // Control characters and (dependent upon position) numbers get escaped as code points
+                       return ch.slice( 0, -1 ) + "\\" + ch.charCodeAt( ch.length - 1 ).toString( 16 ) + " ";
+               }
+
+               // Other potentially-special ASCII characters get backslash-escaped
+               return "\\" + ch;
+       };
 
 function sortOrder( a, b ) {
 
@@ -110,7 +129,14 @@ function uniqueSort( results ) {
        return results;
 }
 
+function escape( sel ) {
+       return ( sel + "" ).replace( rcssescape, fcssescape );
+}
+
 jQuery.extend( {
+       uniqueSort: uniqueSort,
+       unique: uniqueSort,
+       escapeSelector: escape,
        find: function( selector, context, results, seed ) {
                var elem, nodeType,
                        i = 0;
@@ -140,8 +166,6 @@ jQuery.extend( {
 
                return results;
        },
-       uniqueSort: uniqueSort,
-       unique: uniqueSort,
        text: function( elem ) {
                var node,
                        ret = "",
index dcee45f37fd7f33ef187e4c8e945a1ee04a516cc..462cd240a0951dfe26d9c3c2616d008df6979a44 100644 (file)
@@ -10,5 +10,6 @@ jQuery.uniqueSort = jQuery.unique = Sizzle.uniqueSort;
 jQuery.text = Sizzle.getText;
 jQuery.isXMLDoc = Sizzle.isXML;
 jQuery.contains = Sizzle.contains;
+jQuery.escapeSelector = Sizzle.escape;
 
 } );
index 2f6e9affdacbeabebf0618efdb59800679557241..c3394e3805dd89028d0a555d551869e618ef874f 100644 (file)
@@ -536,3 +536,9 @@ QUnit.asyncTest( "Iframe dispatch should not affect jQuery (#13936)", 1, functio
        iframeDoc.write( "<body><form id='navigate' action='?'></form></body>" );
        iframeDoc.close();
 } );
+
+QUnit.test( "Ensure escapeSelector exists (escape tests in Sizzle)", function( assert ) {
+       assert.expect( 1 );
+
+       assert.equal( jQuery.escapeSelector( "#foo.bar" ), "\\#foo\\.bar", "escapeSelector present" );
+} );