Nested Quotes causing Bug in HTML Reply Signatures for Gmail
In the Gmail HTML Reply Signatures script, the code you are instructed to modify looks like this:
var htmlsig = '<br><p><img src="http://mydomain.com/image.jpg"></p><br>';
Now, consider this line of code, which will not work properly due to errors:
var htmlsig = "<br><p><img src="http://mydomain.com/image.jpg"></p><br>";
Think about the last book you read where the main character quoted another character:
Mark said to Tammy, "What did John tell you?". Tammy replied, "Mark, John said: 'The only dumb question is the one that is never asked!', and I think he really means it.". Mark raised an eyebrow...
Notice how the storyline is told outside of quotes, quotes are used to indicate a character speaking, and apostrophes are nested within quotes to indicate a person speaking about a person speaking. Could this pattern be nested deeper, such as a person speaking about a person speaking about a person speaking? It could, but we only have two delimiting characters: Quotes and apostrophes. We'll come back to this problem later.
Now, what would the reader think of this:
Mark said to Tammy, "Tammy, my writing career is in the toilet, what is this story about?". Tammy looked puzzled. Finally she said, "John said, "This quote is confusing because it is a quote within a quote.", and not to worry about our careers".
Notice the inner quotation uses the same type of quote. Instead of an inner quote, the text would actually represent the narration and not any type of quote.
Now here comes the really interesting part. You can use quotes in whatever order you want. Consider this:
var htmlsig = "<br><p><img src='http://mydomain.com/image.jpg'></p><br>";
Notice that I reversed the (') and the ("). The quotation mark is now the outer string wrapper and the apostrophe now represents the inner string wrapper. Be prepared to see differences, as unlike publishing a novel, there is no rule determining whether an apostrophe is used first or a quote is used first. As long as one type is used for the outer string and the other type is used for the inner string, your result will be the same.
Quote within a Quote within a Quote within a ...
UPDATE: The article, Nested-Nested Quotes, describes the concept of nested quotes even deeper using real code and provides the solution to this problem!