Index: trunk/extensions/MobileFrontend/javascripts/opensearch.js |
— | — | @@ -144,27 +144,38 @@ |
145 | 145 | } |
146 | 146 | } |
147 | 147 | |
148 | | -function htmlEntities( str ) { |
149 | | - return String( str ).replace( /&/g, '&' ).replace( /</g, '<' ).replace( />/g, '>' ).replace( /"/g, '"' ).replace( /'/g, ''' ); |
150 | | -} |
151 | | - |
152 | | -function escapeJsString( str ) { |
153 | | - return String( str ).replace( /\\/g, '\\\\' ).replace( /'/g, "\\'" ).replace( /\n/g, '\\n' ); |
154 | | -} |
155 | | - |
156 | 148 | function writeResults( sections ) { |
157 | 149 | results.style.display = 'block'; |
158 | 150 | if ( !sections || sections.length < 1 ) { |
159 | 151 | results.innerHTML = "No results"; |
160 | 152 | } else { |
161 | | - var html = '<div class="suggestions-results">'; |
| 153 | + if( results.firstChild ) { |
| 154 | + results.removeChild( results.firstChild ); |
| 155 | + } |
| 156 | + var suggestions = document.createElement( 'div' ); |
| 157 | + suggestions.className = 'suggestions-results'; |
| 158 | + results.appendChild( suggestions ); |
162 | 159 | for ( i = 0; i < sections.length; i++ ) { |
163 | | - var section = sections[i]; |
164 | | - var rel = i + 1; |
165 | | - section.value = section.value.replace( /^(?:\/\/|[^\/]+)*\//, '/' ); |
166 | | - html = html + "<div class=\"suggestions-result\" rel=\"" + htmlEntities( rel ) + "\" title=\"" + htmlEntities( section.label ) + "\"><a class=\"sq-val-update\" href=\"javascript:sqValUpdate('" + htmlEntities( escapeJsString( section.label ) ) + "');\">+</a><a class=\"search-result-item\" href='" + htmlEntities( section.value ) + "'>" + htmlEntities( section.label ) + "</a></div>"; |
| 160 | + var section = sections[i], suggestionsResult = document.createElement( 'div' ), |
| 161 | + link = document.createElement( 'a' ), label; |
| 162 | + suggestionsResult.setAttribute( 'title', section.label ); |
| 163 | + suggestionsResult.className = 'suggestions-result'; |
| 164 | + label = document.createTextNode( '+' ); |
| 165 | + link.appendChild(label); |
| 166 | + link.className = 'sq-val-update'; |
| 167 | + link.addEventListener( 'click', function() { |
| 168 | + var title = this.parentNode.getAttribute( 'title' ); |
| 169 | + sqValUpdate( title ); |
| 170 | + }); |
| 171 | + suggestionsResult.appendChild( link ); |
| 172 | + |
| 173 | + link = document.createElement( 'a' ); |
| 174 | + link.setAttribute( 'href', section.value ); |
| 175 | + link.className = 'search-result-item'; |
| 176 | + label = document.createTextNode( section.label ); |
| 177 | + link.appendChild( label ); |
| 178 | + suggestionsResult.appendChild( link ); |
| 179 | + suggestions.appendChild( suggestionsResult ); |
167 | 180 | } |
168 | | - html = html + '</div>'; |
169 | | - results.innerHTML = html; |
170 | 181 | } |
171 | 182 | } |
\ No newline at end of file |
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js |
— | — | @@ -325,6 +325,7 @@ |
326 | 326 | } |
327 | 327 | |
328 | 328 | function writeResults( sections ) { |
| 329 | + var term = htmlEntities( document.getElementById( 'search' ).value ); |
329 | 330 | results.style.display = 'block'; |
330 | 331 | if ( search ) { |
331 | 332 | search.focus(); |
— | — | @@ -332,16 +333,39 @@ |
333 | 334 | if ( !sections || sections.length < 1 ) { |
334 | 335 | results.innerHTML = "<div class=\"suggestions-results\" title=\"No Results\">No Results</div>"; |
335 | 336 | } else { |
336 | | - var html = '<div class="suggestions-results">'; |
| 337 | + if( results.firstChild ) { |
| 338 | + results.removeChild( results.firstChild ); |
| 339 | + } |
| 340 | + var suggestions = document.createElement( 'div' ); |
| 341 | + suggestions.className = 'suggestions-results'; |
| 342 | + results.appendChild( suggestions ); |
337 | 343 | for ( i = 0; i < sections.length; i++ ) { |
338 | | - var section = sections[i]; |
339 | | - var rel = i + 1; |
340 | | - term = htmlEntities( decodeURIComponent( term ) ); |
341 | | - var label = section.label.replace( new RegExp( '(' + term + ')', 'ig' ), '<strong>$1</strong>' ); |
342 | | - section.value = section.value.replace( /^(?:\/\/|[^\/]+)*\//, '/' ); |
343 | | - html = html + "<div class=\"suggestions-result\" rel=\"" + htmlEntities( rel ) + "\" title=\"" + htmlEntities( section.label ) + "\"><a class=\"sq-val-update\" href=\"javascript:sqValUpdate('" + htmlEntities( escapeJsString( section.label ) ) + "');\">+</a><a class=\"search-result-item\" href='" + htmlEntities( section.value ) + "'>" + label + "</a></div>"; |
| 344 | + var section = sections[i], suggestionsResult = document.createElement( 'div' ), |
| 345 | + link = document.createElement( 'a' ), label; |
| 346 | + suggestionsResult.setAttribute( 'title', section.label ); |
| 347 | + suggestionsResult.className = 'suggestions-result'; |
| 348 | + label = document.createTextNode( '+' ); |
| 349 | + link.appendChild(label); |
| 350 | + link.className = 'sq-val-update'; |
| 351 | + link.addEventListener( 'click', function() { |
| 352 | + var title = this.parentNode.getAttribute( 'title' ); |
| 353 | + sqValUpdate( title ); |
| 354 | + }); |
| 355 | + suggestionsResult.appendChild( link ); |
| 356 | + |
| 357 | + link = document.createElement( 'a' ); |
| 358 | + link.setAttribute( 'href', section.value ); |
| 359 | + link.className = 'search-result-item'; |
| 360 | + label = document.createTextNode( section.label ); |
| 361 | + link.appendChild( label ); |
| 362 | + |
| 363 | + suggestionsResult.appendChild( link ); |
| 364 | + suggestions.appendChild( suggestionsResult ); |
| 365 | + // TODO: simplify the highlighting code to not use htmlEntities |
| 366 | + // highlight matched term |
| 367 | + var escapedTerm = escapeJsString( term ); |
| 368 | + link.innerHTML = link.innerHTML.replace( new RegExp( '(' + escapedTerm + ')' , 'ig'), |
| 369 | + '<strong>$1</strong>' ); |
344 | 370 | } |
345 | | - html = html + '</div>'; |
346 | | - results.innerHTML = html; |
347 | 371 | } |
348 | 372 | } |
\ No newline at end of file |