Skip to main content

Bug Tracker

Side navigation

#10868 closed feature (wontfix)

Opened November 22, 2011 11:09PM UTC

Closed January 18, 2012 03:26AM UTC

feature request: .preimage

Reported by: gibson042 Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s).

Sample usage:

function childDelegate( $element, selClickTarget, strEvents, fnHandler ) {
  return $( $element ).on( strEvents, selClickTarget, function() {
    if ( $( this ).preimage( "> " + selClickTarget, $element ) ) {
      return fn.apply(this, arguments);
    }
  });
}

childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) {
  alert( "child dd clicked" );
});

Proposed signature: .preimage( selector [, candidates] )

The one-argument version returns a jQuery object containing all elements from which the results of .find(selector) would include a member of the context set.

The two-argument version is a short-circuiting alias for .preimage( selector ).is( candidates ), returning a boolean indicating intersection between the full results and a candidate set.

Implementation would add helper functions on the Sizzle relative operators (e.g., $.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them.

A link to a working demonstration will be posted shortly.

Attachments (0)
Change History (15)

Changed November 22, 2011 11:38PM UTC by ajpiano comment:1

keywords: → 1.8-discuss

If you could give us a few details about the usecase for such a function as well, we'd really appreciate it. Is this meant to be an internal improvement to Sizzle? A user-facing API? Thanks!

Changed November 23, 2011 05:28AM UTC by gibson042 comment:2

It'd be a documented external API, but might ultimately have the potential to shrink Sizzle and/or jQuery codebases through internal use. Mostly, I want it to provide intuitive event handling for potentially-nesting widgets (#6696; #10762; etc.). It'll be valuable if we ever decide to support .on(events, ">...", fn), and will let jQuery users simulate support for it now.

The closure provided by reverse lookups shouldn't add much code weight, and has the potential to enable a lot of currently-unpredictable creative applications.

Changed November 23, 2011 04:59PM UTC by rwaldron comment:3

-1, Feature creep

Changed November 24, 2011 08:58PM UTC by dmethvin comment:4

-1, I have no idea what this is, which makes me think I am against it.

Changed November 24, 2011 08:58PM UTC by dmethvin comment:5

status: newopen

Changed November 27, 2011 07:28PM UTC by gibson042 comment:6

Plugin implementing request: http://plugins.jquery.com/project/preimage

Working example: http://jsbin.com/usabam

Uncompressed, and including unit tests and functionality backporting to support jQuery 1.4+ instead of 1.6+, the plugin is 5.88 KiB. If brought into jQuery proper, access to local variables and functions would eliminate redundancy and result in no more than a 100-200 byte initial increase in jquery.min.js, decreased if .preimage can be used to optimize existing functionality.

Changed December 13, 2011 02:51PM UTC by jzaefferer comment:7

description: I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s). \ \ Sample usage: \ {{{ \ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) { \ return $( $element ).on( strEvents, selClickTarget, function() { \ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) { \ return fn.apply(this, arguments); \ } \ }); \ } \ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) { \ alert( "child dd clicked" ); \ }); \ }}} \ \ Proposed signature: `.preimage( selector [, candidates] )` \ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set. \ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set. \ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them. \ \ A link to a working demonstration will be posted shortly.I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s).\ \ Sample usage:\ {{{\ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) {\ return $( $element ).on( strEvents, selClickTarget, function() {\ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) {\ return fn.apply(this, arguments);\ }\ });\ }\ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) {\ alert( "child dd clicked" );\ });\ }}}\ \ Proposed signature: `.preimage( selector [, candidates] )`\ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set.\ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set.\ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them.\ \ A link to a working demonstration will be posted shortly.

-1, Too much special-case.

Changed December 13, 2011 04:23PM UTC by jaubourg comment:8

-1, I'd like to see a plugin first in order to assess usefulness.

Changed December 13, 2011 05:37PM UTC by dmethvin comment:9

description: I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s).\ \ Sample usage:\ {{{\ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) {\ return $( $element ).on( strEvents, selClickTarget, function() {\ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) {\ return fn.apply(this, arguments);\ }\ });\ }\ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) {\ alert( "child dd clicked" );\ });\ }}}\ \ Proposed signature: `.preimage( selector [, candidates] )`\ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set.\ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set.\ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them.\ \ A link to a working demonstration will be posted shortly.I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s). \ \ Sample usage: \ {{{ \ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) { \ return $( $element ).on( strEvents, selClickTarget, function() { \ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) { \ return fn.apply(this, arguments); \ } \ }); \ } \ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) { \ alert( "child dd clicked" ); \ }); \ }}} \ \ Proposed signature: `.preimage( selector [, candidates] )` \ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set. \ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set. \ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them. \ \ A link to a working demonstration will be posted shortly.

-1, The code was lost in the Great Depluginization Purge; I'd still like to see it but if it's 100-200 bytes it doesn't seem like it would land. Of course, you worked miracles with the undefined patch so maybe I should wait to see it.

Changed December 14, 2011 01:30PM UTC by mikesherov comment:10

description: I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s). \ \ Sample usage: \ {{{ \ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) { \ return $( $element ).on( strEvents, selClickTarget, function() { \ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) { \ return fn.apply(this, arguments); \ } \ }); \ } \ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) { \ alert( "child dd clicked" ); \ }); \ }}} \ \ Proposed signature: `.preimage( selector [, candidates] )` \ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set. \ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set. \ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them. \ \ A link to a working demonstration will be posted shortly.I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s).\ \ Sample usage:\ {{{\ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) {\ return $( $element ).on( strEvents, selClickTarget, function() {\ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) {\ return fn.apply(this, arguments);\ }\ });\ }\ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) {\ alert( "child dd clicked" );\ });\ }}}\ \ Proposed signature: `.preimage( selector [, candidates] )`\ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set.\ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set.\ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them.\ \ A link to a working demonstration will be posted shortly.

-1, gets us further away from qSa, when we should be moving more towards it.

Changed December 14, 2011 04:11PM UTC by timmywil comment:11

-1

Changed December 19, 2011 05:34PM UTC by rwaldron comment:12

description: I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s).\ \ Sample usage:\ {{{\ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) {\ return $( $element ).on( strEvents, selClickTarget, function() {\ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) {\ return fn.apply(this, arguments);\ }\ });\ }\ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) {\ alert( "child dd clicked" );\ });\ }}}\ \ Proposed signature: `.preimage( selector [, candidates] )`\ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set.\ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set.\ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them.\ \ A link to a working demonstration will be posted shortly.I'd like a method that reverses the normal direction of selectors, used to find elements for which a .find with given selector would return the context element(s). \ \ Sample usage: \ {{{ \ function childDelegate( $element, selClickTarget, strEvents, fnHandler ) { \ return $( $element ).on( strEvents, selClickTarget, function() { \ if ( $( this ).preimage( "> " + selClickTarget, $element ) ) { \ return fn.apply(this, arguments); \ } \ }); \ } \ \ childDelegate( $dl, "dt ~ dd > a:first-child", "click", function( evt ) { \ alert( "child dd clicked" ); \ }); \ }}} \ \ Proposed signature: `.preimage( selector [, candidates] )` \ \ The one-argument version returns a jQuery object containing all elements from which the results of `.find(selector)` would include a member of the context set. \ \ The two-argument version is a short-circuiting alias for `.preimage( selector ).is( candidates )`, returning a boolean indicating intersection between the full results and a candidate set. \ \ Implementation would add helper functions on the Sizzle relative operators (e.g., `$.each({'>': $.fn.parent, '+': $.fn.prev, '~': $.fn.prevAll}, function(operator,fn){ try{$.expr.relative[operator].preimage=fn;}catch(x){} });`) and a relatively simple function to parse/chunk selectors and incrementally build up the result set by working backwards through them. \ \ A link to a working demonstration will be posted shortly.

-1, Feature creep

Changed December 20, 2011 10:02PM UTC by gibson042 comment:13

I was pointed to http://lists.w3.org/Archives/Public/public-webapps/2011OctDec/1454.html by timmywil, and in light of the similarities between this feature request and Element.matches(selector, reference), I'd like to redirect this one towards jQuery.fn.is(selector, reference) (as noted by Lachlan in the message, there is a basic equivalence between the two methods except for ours missing the latter signature).

I still think preimage functionality is useful, and is probably a dependency anyway, but the description of this ticket is no longer completely accurate. Should it be updated, or replaced?

Changed January 02, 2012 07:12AM UTC by gibson042 comment:14

#11124 resolves the bulk of this ticket. The remainder is essentially blocked by #11109.

Changed January 18, 2012 03:26AM UTC by dmethvin comment:15

keywords: 1.8-discuss
resolution: → wontfix
status: openclosed

Voted off the island.