2

I have the following line of coffeescript code (both dCnt and sDesc are jQuery objects), which does some basic cleaning up of a block of HTML while moving its location:

dCnt.append(sDesc.html().replace( /<div/gi, '<p' ).replace( /<\/div>/gi, '</p>' ).replace /\sstyle="text-align: center;"/gi, '')

This outputs the following JavaScript:

dCnt.append(sDesc.html().replace(/<div/gi, '<p').replace(/<\/div>/gi, '</p>').replace(/\sstyle="text-align: center;"/gi, ''));

The first two replaces cause no problems, but the third has taken a bit of work to come up with something the coffeescript compiler can cope with, and the result is too limiting and fragile for my liking.

So far I've discovered that:

1) If I have parenthesis () around the final replace then coffeescript interprets the double quotes as a string and gets confused (it ends up interpreting one of the forward slashes as a division).

2) If I remove the parenthesis that stops the MATH error, then it any white space in the regular expression will confuse the compiler - the output with white space instead of \s for the code above is (note the hideously broken parenthesis on the final replace):

dCnt.append(sDesc.html().replace(/<div/gi, '<p').replace(/<\/div>/gi, '</p>').replace / (style = "text-align: center;" / gi), '');

If the replace is not in a containing set of parenthesis (ie. remove the dCnt.append() from the code and handle that on a separate line) then white space in the regular expression will cause the compiler to throw an Unexpected ',' error (which is at least preferable to generating garbage JavaScript...).

Due to the need to remove the parenthesis (optional in coffeescript, although I find leaving them out seriously reduces code readability), it's not possible to chain two function calls which are afflicted by this issue.

Can anyone come up with a less fragile solution to this problem that still leaves the ability to use jQuery chaining intact?

1 Answers1

1

The problem appears to boil down to these:

s.replace(/ /, '')
s.replace / /, ''

being seen as ambiguous by the CoffeeScript compiler and it resolves the ambiguity incorrectly. Right? The source of the problem is the space following the slash, not the double quotes. CS sees this:

/ /

as an attempted division rather than a regex literal. However, this:

/\ /

is interpreted as a regex which matches a single space.

I can think of a couple work arounds:

  1. Use \s or even [ \t]. I think these are more correct than just looking for a single space (putting aside the whole "mangling HTML with regexes" issue of course).
  2. Escape the leading space: /\ style.../. I actually prefer this over raw spaces in regexes as it makes them easier to count.

So not much of an answer really (sorry) but maybe a confirmation that you're not losing your mind.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • I had a suspicion there wasn't going to be a 'magic' fix I'd missed. Seems to be par for the course with coffeescript - this is the first project I've used it on, and the jury is still very much out on whether it solves more problems and inefficiencies than it introduces. – Geoff Beaumont Dec 29 '12 at 20:42
  • CS is mostly nice (except for debugging) but the not nice parts are rather nasty, sort of like Rails (except that the list of "not nice parts" gets bigger every time I work with Rails while CS's has pretty much levelled off). – mu is too short Dec 29 '12 at 20:58