Last week, an end user came to me with a bug she found in the largest of the software systems I develop at work. The bug was pretty major and to fix it the right way would mean re-designing. Re-designing software with a three person development team and a strict schedule just wouldn't fly for this particular bug.
The Problem
We have a standard format for search screens throughout the software. Each search screen has a set of fields and, using AJAX, the values entered for those fields get sent to a servlet that performs the actual search. When the results come back from the back end the servlet takes those results and iterates over each one. It then uses an Object called SearchResultFormatter. SearchResultFormatter has a set of methods that take parameters to build the HTML (and inline javascript) for the results. This Object provides an easy way to change the format of all search results from one place, not to mention it cleans up the servlets nicely.
For example, doing:
results.addManageValue("/apolo/crm/prepareSoHdr.do?soId=", so.getSoId(), EvalHelper.returnIfNotNull(so.getSoNo()));
<td onclick="javascript:window.location='/apolo/crm/prepareSoHdr.do?soId=1221&mode=view';">S1221</td>
SearchResultFormatter methods that take additional parameters and string these parameters together to create a different onclick event, such as:
results.addLookupValue(caller, customerMaster.getFullName(), customerMaster.getCustomerId(), customerMaster.getFullName());
<td onclick="javascript:choose('customerMaster', 'Fairchild Controls Corp.', '1000');">Fairchild Controls Corp.</td>
customerMaster.getFullName() generates a String with a single quote "'"? Well, when you go and try and click on that td, it is going to break. The generated code may look like:
<td onclick="javascript:choose('customerMaster', 'Fairchild's Corp.', '1000');">Fairchild Controls Corp.</td>
<td onclick="javascript:choose('customerMaster', 'Fairchild's Corp.', '1000');">Fairchild Controls Corp.</td>
XMLHttpRequest javascript Object and I pull the text out and place it inside the DOM, Mozilla seems to catch the ' and replace it with simple ', defeating all of my efforts.results.addLookupValue(caller, customerMaster.getFullName(), customerMaster.getCustomerId(), explode(customerMaster.getFullName()));
<td onclick="javascript:choose('customerMaster', 'F'+'a'+'i'+'r'+'c'+'h'+'i'+'l'+'d'+'&'+'a'+'p'+'o'+'s'+';'+'s'+' '+'C'+'o'+'r'+'p'+'.','1000');">Fairchild Controls Corp.</td>