Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 1.33 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function tryToStripXpathes( xpathes ) {
  2.         var standard_axis_number = [ -1, -1 ], // standard number for each half
  3.                 max_axis_idx = [ [], [] ], // for each half
  4.                 axis_number, half_type, cnt = 0,
  5.                 i, l, j, len, idx;
  6.  
  7.         for ( i = 0, l = xpathes.length; i < l; i += 1 ) {
  8.                 half_type = ( i & 1 ); // Bitwise! will be 0 for even, 1 for odd half
  9.                 axis_number = xpathes[i].split( "/" ).length; // -1 doesn't matter
  10.  
  11.                 if ( -1 === standard_axis_number[half_type] ) {
  12.                         standard_axis_number[half_type] = axis_number;
  13.                         max_axis_idx[half_type].push( i ); // remember xpath with max axis
  14.                 } else if ( standard_axis_number[half_type] < axis_number ) {
  15.                         xpathes[i] = xpathes[i].replace(
  16.                                 /\/(a|span|b|i|em|strong)$/,
  17.                                 ''
  18.                         );
  19.                         i -= 1; // One step back and check number of axis again
  20.                 } else if ( standard_axis_number[half_type] > axis_number ) {
  21.                         // strip last axis from every xpathes with max axis
  22.                         for ( j = 0, len = max_axis_idx[half_type].length; j < len; j += 1 ) {
  23.                                 idx = max_axis_idx[half_type][j];
  24.                                
  25.                                 xpathes[idx] = xpathes[idx].replace(
  26.                                         /\/(a|span|b|i|em|strong)$/,
  27.                                         ''
  28.                                 );
  29.                         }
  30.                         standard_axis_number[half_type] -= 1;
  31.                        
  32.                         i -= 1; // One step back and check number of axis again
  33.                 } else {
  34.                         max_axis_idx[half_type].push( i ); // remember xpath with max axis
  35.                 }
  36.         }
  37.  
  38.         return xpathes;
  39. }