You are here

static function RTLMaker::fix_mixed_path in PersianTools 8

Fix mixed-up paths in rtl blocks. Logic: Gets triggered once a starting '.' or '/' is detected after a whitespace. The correcting symbol is inserted once an english char is seen inside the path.

1 call to RTLMaker::fix_mixed_path()
RTLMaker::convert_sm in src/RTLMaker.php
Main function for multiple features and fixes of persiantools module.

File

src/RTLMaker.php, line 202
Contains \Drupal\persiantools\RTLMaker.

Class

RTLMaker

Namespace

Drupal\persiantools

Code

static function fix_mixed_path($str, $ch, $type, $i, $len) {
  static $maybe_path = TRUE;
  static $is_path = FALSE;
  static $path_pos = -1;
  $changed = FALSE;
  if ($i == 0) {
    $maybe_path = TRUE;
  }
  if ($type == RTLMaker::WS) {
    $maybe_path = TRUE;
  }
  elseif ($is_path) {
    if ($type == RTLMaker::EN) {
      $str = RTLMaker::insert_str($str, '‎', $path_pos);
      $changed = TRUE;
    }
    $path_pos = -1;
    $is_path = FALSE;
  }
  elseif ($maybe_path) {
    if ($type == RTLMaker::SLASH) {
      $is_path = TRUE;
      if ($path_pos < 0) {
        $path_pos = $i;
      }
    }
    elseif ($ch == '.') {
      if ($path_pos < 0) {
        $path_pos = $i;
      }
    }
    else {
      $maybe_path = FALSE;
      $is_path = FALSE;
      $path_pos = -1;
    }
  }

  // Detect trailing slashes in paths.
  if ($type == RTLMaker::SLASH && $i > 0) {
    $prev_ch = Unicode::substr($str, $i - 1, 1);
    $prev_ch_type = RTLMaker::get_char_type($prev_ch);
    $is_last_char = $i == $len - 1;
    if ($prev_ch_type == RTLMaker::EN && ($is_last_char || Unicode::substr($str, $i + 1, 1) == ' ')) {
      $str = RTLMaker::insert_str($str, '&lrm;', $i + 1);
      $changed = TRUE;
    }
  }
  return array(
    $str,
    $changed,
  );
}