line 40 of ajax-responder.js states
url = url.replace("/nojs/", "/ajax/");
However, if I've setup a url where nojs is at the end (i.e. myurl/nojs) then it is not picked up by .replace() and is not changed.

I propose the following change:
url = url.replace(/nojs/g, "ajax");

This would make 'nojs' replaced globally in the url with 'ajax'. There may be edge cases where this is not desirable, but since it is not case insensitive and does handle the case where we have /nojs, I think it's okay. Can anyone refute this? Is there something I've missed?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

Status: Active » Fixed

Works for me. Committed!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Dave Reid’s picture

Project: Chaos Tool Suite (ctools) » Drupal core
Version: 6.x-1.x-dev » 7.x-dev
Component: Code » javascript
Status: Closed (fixed) » Needs review
FileSize
732 bytes

I had problems that paths like 'foo/bar/nojs' didn't work on D7 (had to use 'foo/bar/nojs/' with explicit trailing slash) so this needs to be up-ported as well. Tested and works great.

zroger’s picture

Status: Needs review » Reviewed & tested by the community

Works for me. This simple patch has worked well in CTools and makes just as much sense in 7.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Might want to open a ticket for CTools on D7 -- they can undo that little change. :)

Dave Reid’s picture

Project: Drupal core » Chaos Tool Suite (ctools)
Version: 7.x-dev » 7.x-1.x-dev
Component: javascript » Code
Status: Fixed » Patch (to be ported)

Moving back to CTools 7.x-1.x to see if it needs fixing there. Re-fix if it doesn't need to be.

Damien Tournoud’s picture

Project: Chaos Tool Suite (ctools) » Drupal core
Version: 7.x-1.x-dev » 7.x-dev
Component: Code » base system
Priority: Normal » Critical
Status: Patch (to be ported) » Needs work

Crap. Let's fix this non-sense, please.

/nojs/g will replace *all* occurrences of "nojs", even if they are part of another word.

cha0s’s picture

url = url.replace("/nojs/?$", "/ajax");

?

cha0s’s picture

Status: Needs work » Needs review
FileSize
543 bytes

Aaaand patchie.

Damien Tournoud’s picture

Status: Needs review » Needs work
-  this.url = element_settings.url.replace(/nojs/g, 'ajax');
+  this.url = element_settings.url.replace(/\/nojs\/?$/g, '/ajax');

At first sight, this is removing the / after nojs.

cha0s’s picture

Status: Needs work » Needs review

Yep, it is. So what? That's only if the slash is at the end (aka it's meaningless)

Damien Tournoud’s picture

Status: Needs review » Needs work

/a/nojs/b/c will become /a/nojsb/c, which *is* a problem.

cha0s’s picture

Status: Needs work » Needs review

Excuse me? Test the patch. Thanks in advance.

Or alternatively, run this code in your browser: javascript:(alert('/a/nojs/b/c'.replace(/\/nojs\/?$/g, '/ajax')))

Damien Tournoud’s picture

Status: Needs review » Needs work

Indeed, I read that wrong. But this regexp is still wrong: we want to replace every part of the path containing "nojs", whether it is in the end or in the middle of the path.

Dave Reid’s picture

The JS in #13 didn't replace properly in my browser.

zroger’s picture

this.url = element_settings.url.replace(/\/nojs(\/?)/g, '/ajax$1');

This matches /nojs with an optional trailing slash. The optional trailing slash is captured and re-used in the replacement.

Test with nojs in the middle of a url:
javascript:(alert('/a/nojs/b/c'.replace(/\/nojs(\/?)/g, '/ajax$1')))

Test with nojs at the end of the url, no trailing slash:
javascript:(alert('/a/nojs'.replace(/\/nojs(\/?)/g, '/ajax$1')))

cha0s’s picture

Status: Needs work » Needs review
FileSize
550 bytes

Cool, thank you Roger López. :) For some reason I had assumed that nojs was only occuring at the end of the path. If it can occur everywhere, let's check for the leading slash as well, in the case of paths 'nojs/foo/bar', for completion.

cha0s’s picture

FileSize
551 bytes

Whoops, this one instead.

Dave Reid’s picture

I tested #16 and it worked even with foo/nonojshark/bar and didn't replace as expected.

cha0s’s picture

FileSize
596 bytes

The patch I posted works on that string too, there is a problem however, that isn't addressed in either patch.

My latest patch will fail on 'nojshark/bar', and Roger's will faill on 'nojs/foo/bar'. I added the anchor to the beginning of the regex in my latest patch, but it isn't enough.

So, I see what's happening here, the '$' anchor was removed from my regex without me noticing in the last couple of patches. This has been put back in.

I'm providing test expressions, though I don't think we can simpletest JS. Please run these trough firebug's console before commenting about how effective the regex is...

alert('nojs/foo'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('nojshark/foo'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('nonojshark/foo'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('bar/nojs/foo'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('bar/nonojshark/foo'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('bar/nojs'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('bar/nojs/'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));
alert('bar/nonojs'.replace(/(\/|^)nojs(\/|$){1}/g, '$1ajax$2'));

merlinofchaos’s picture

Drupal cannot handle wildcards in the first position of a URL, so missing ^nojs does not matter in the slightest. We should consider that an invalid place to put your ajax/nojs anyway.

cha0s’s picture

FileSize
588 bytes

Oh ok... if we don't have to handle that case (I was trying to be safe rather than sorry ;) then we cn just use Roger's patch straight up. I created a patch for that one.

cha0s’s picture

Wait no we cant, blah. It messes up 'bar/nojshark/foo'. One sec.

cha0s’s picture

FileSize
594 bytes

Fine, I just removed the line beginning anchor from my last patch. :P

cha0s’s picture

FileSize
591 bytes

Here, I think the {1} was redundant.

cha0s’s picture

FileSize
587 bytes

Speaking of redundant... :)

So, Roger's last one would fail on '/a/nojsb/c', so this patch SHOULD be the one.

cha0s’s picture

FileSize
588 bytes

Meh, how many times can I screw up one patch? Hopefully Damien is counting for me ;)

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

#27 looks ok :)

Alexander N’s picture

FileSize
743 bytes

It should be '/ajax$1' instead of '/ajax$2'.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 565808_8.patch, failed testing.

scor’s picture

Status: Needs work » Needs review

#29: 565808_8.patch queued for re-testing.

cha0s’s picture

Yup, you got it! (Apparently I still had one bad patch left in me ;P)

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Let's get this in, and fix the mess.

rfay’s picture

subscribing.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Dave Reid’s picture

Project: Drupal core » Chaos Tool Suite (ctools)
Version: 7.x-dev » 6.x-1.x-dev
Component: base system » Code
Status: Fixed » Patch (to be ported)

Let's make sure this gets backported to Ctools then.

Dave Reid’s picture

Status: Patch (to be ported) » Needs review
FileSize
1.66 KB

Patch for CTools 6.x-1.x that uses the same new replacement in all places

Dave Reid’s picture

Missed one in modal.js

merlinofchaos’s picture

Status: Needs review » Fixed

Thanks for the backport! Committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Dave Reid’s picture

Project: Chaos Tool Suite (ctools) » Drupal core
Version: 6.x-1.x-dev » 7.x-dev
Component: Code » ajax system
Assigned: Unassigned » Dave Reid
Priority: Critical » Major
Status: Closed (fixed) » Active

I need to re-open this because urls like path/nojs?destination=foobar do not get replaced properly (with both Ctools and D7 core). I'm going to also bet if you have clean URLs disabled the similar ?q=path/nojs&destination=foobar will not get processed as well.

rfay’s picture

subscribing

Dave Reid’s picture

My initial thought is to use /nojs\b/ which will match on word boundries and we don't even have to use $1 for replacement. The only downside is that it matches paths like /foo/nojs-bar, but otherwise it works for *all* possible cases, including query string and query string without clean URLs. Otherwise we'll just have to use /nojs(\/|\?|&|$)/

Dave Reid’s picture

Issue tags: +xmlsitemap, +Needs regular expression review

Tagging...

tnightingale’s picture

Just ran into a similar issue to Dave Reid, wanting to use a URL ending in /nojs?destination...
Are there any cases not covered by /nojs(\/|\?|&|$)/?

Seems pretty reasonable to me

tnightingale’s picture

Patch for CTools 6.x-1.x using /nojs(\/|$\?|&)/g
Throwing in a patch for D7 too though I suspect it is too late for D7 patches eh? (I'm kind of new to all this :P)

Dave Reid’s picture

Status: Active » Needs review
tnightingale’s picture

Changing status...? EDIT - oh thanks Dave, you got in before me :)

sun’s picture

Status: Needs review » Needs work
+++ misc/ajax.js	25 Aug 2010 16:58:37 -0000
@@ -107,7 +107,7 @@ Drupal.ajax = function (base, element, e
   // Replacing 'nojs' with 'ajax' in the URL allows for an easy method to let
   // the server detect when it needs to degrade gracefully.
-  this.url = element_settings.url.replace(/\/nojs(\/|$)/g, '/ajax$1');
+  this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&)/g, '/ajax$1');

Since we are not able to test JS yet, and even if we could, we need to explain the non-trivial parts in comments.

- /nojs? made me think.

- /nojs& made me go nuts, until I noticed the term "destination" somewhere skimming this issue.

Powered by Dreditor.

tnightingale’s picture

Status: Needs work » Needs review
FileSize
1.07 KB
2.16 KB

Added explanation in comments before regex to describe the four scenarios being checked:

// There are four scenarios to check for:
// 1. /nojs/
// 2. /nojs$ - The end of a URL string.
// 3. /nojs? - Followed by a query (with cleanurls enabled).
//      eg: path/nojs?destination=foobar
// 4. /nojs& - Followed by a query (without cleanurls enabled).
//      eg: ?q=path/nojs&destination=foobar
Dave Reid’s picture

Status: Needs review » Needs work

Can we remove the empty comment line (//) and just make that an empty line (up for a quick re-roll thegreat)? Otherwise this looks good to me. Side note, the ctools patch has to fix this in 4 different places. The D7 patch only has to fix it in one place though.

merlinofchaos’s picture

The CTools patch could add a single javascript function and call that, so that the regex only exists in one place. Yay abstraction. =)

tnightingale’s picture

Removed empty comment and rerolled.
Created a function (urlReplaceNojs... not feeling very imaginative today :P) for ctools and replaced the replace() calls in all four places.

tnightingale’s picture

Status: Needs work » Needs review

changing status

tnightingale’s picture

Whoops apparently it pays to review your work *before* you upload... plz ignore last ctools patch

tnightingale’s picture

also uploading the right file is important...

Dave Reid’s picture

Status: Needs review » Needs work

The comments will need to be cleaned up in #54. We use 'clean URLs' and not 'cleanurls'. Should use 'E.g.' and not 'eg'. thegreat, up for a re-roll?

andrewlevine’s picture

Status: Needs work » Needs review
FileSize
2.43 KB
1.07 KB

Made the simple wording changes recommended by Dave Reid. The patches still applied

Status: Needs review » Needs work

The last submitted patch, 565808-ajax-replace-ctools-comments-D6-58.patch, failed testing.

andrewlevine’s picture

Status: Needs work » Needs review

D6 patch was meant for ctools, bot didn't know

tnightingale’s picture

Thanks andrewlevine.
Sorry I was slow to pick up on this, I've been dealing with back to school crazies :P

Dave Reid’s picture

Status: Needs review » Needs work

I think we're missing one other condition with 'mypath/nojs#fragment'. If someone can re-roll I can quickly review today.

merlinofchaos’s picture

Go ahead, though we need to make sure the fragments are even visible. Those aren't transmitted to the server, usually, so I don't think they'll be seen usually?

Dave Reid’s picture

No, they're not transmitted to the server, but we need to make sure they're still included in links in a page, which is when this is executed.

carlos8f’s picture

Status: Needs work » Needs review
FileSize
2.44 KB
1.15 KB

Rerolled with support for a fragment.

Status: Needs review » Needs work

The last submitted patch, 565808-ajax-replace-ctools-comments-D6-65.patch, failed testing.

merlinofchaos’s picture

Status: Needs work » Needs review

CTools patch failing testing is not relevant. Back to needs review.

Dave Reid’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #65 satisfies all the conditions I think we could encounter. Tested manually with JS and it replaces as desired.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Looks good to me. Committed to CVS HEAD.

Should we move this to CTools now?

carlos8f’s picture

Project: Drupal core » Chaos Tool Suite (ctools)
Version: 7.x-dev » 6.x-1.x-dev
Component: ajax system » Code
Status: Fixed » Needs review

Moving to CTools.

rfay’s picture

Project: Chaos Tool Suite (ctools) » Drupal core
Version: 6.x-1.x-dev » 7.x-dev
Component: Code » ajax system
Assigned: Dave Reid » Unassigned
Status: Needs review » Fixed

Please create a new issue in CTools and link it here. It makes our history too difficult to follow when an issue that was committed to core suddenly disappears into another queue. Thanks!

carlos8f’s picture

Status: Fixed » Closed (fixed)
Issue tags: -xmlsitemap, -Needs regular expression review

Automatically closed -- issue fixed for 2 weeks with no activity.