r111757 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111756‎ | r111757 | r111758 >
Date:15:17, 17 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
rewrite writeResults to not use innerHTML

this is a first step towards encapsulating the
opensearch.js code. We cannot rely on href
attributes with javascript:
Modified paths:
  • /trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/opensearch.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/javascripts/opensearch.js
@@ -144,27 +144,38 @@
145145 }
146146 }
147147
148 -function htmlEntities( str ) {
149 - return String( str ).replace( /&/g, '&amp;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' ).replace( /"/g, '&quot;' ).replace( /'/g, '&#39;' );
150 -}
151 -
152 -function escapeJsString( str ) {
153 - return String( str ).replace( /\\/g, '\\\\' ).replace( /'/g, "\\'" ).replace( /\n/g, '\\n' );
154 -}
155 -
156148 function writeResults( sections ) {
157149 results.style.display = 'block';
158150 if ( !sections || sections.length < 1 ) {
159151 results.innerHTML = "No results";
160152 } 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 );
162159 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 );
167180 }
168 - html = html + '</div>';
169 - results.innerHTML = html;
170181 }
171182 }
\ No newline at end of file
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -325,6 +325,7 @@
326326 }
327327
328328 function writeResults( sections ) {
 329+ var term = htmlEntities( document.getElementById( 'search' ).value );
329330 results.style.display = 'block';
330331 if ( search ) {
331332 search.focus();
@@ -332,16 +333,39 @@
333334 if ( !sections || sections.length < 1 ) {
334335 results.innerHTML = "<div class=\"suggestions-results\" title=\"No Results\">No Results</div>";
335336 } 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 );
337343 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>' );
344370 }
345 - html = html + '</div>';
346 - results.innerHTML = html;
347371 }
348372 }
\ No newline at end of file

Status & tagging log