+ /**
+ * Wraps argument with unicode control characters for directionality safety
+ *
+ * This solves the problem where directionality-neutral characters at the edge of
+ * the argument string get interpreted with the wrong directionality from the
+ * enclosing context, giving renderings that look corrupted like "(Ben_(WMF".
+ *
+ * The wrapping is LRE...PDF or RLE...PDF, depending on the detected
+ * directionality of the argument string, using the BIDI algorithm's own "First
+ * strong directional codepoint" rule. Essentially, this works round the fact that
+ * there is no embedding equivalent of U+2068 FSI (isolation with heuristic
+ * direction inference). The latter is cleaner but still not widely supported.
+ *
+ * @param string $text Text to wrap
+ * @return string Text, wrapped in LRE...PDF or RLE...PDF or nothing
+ */
+ public function embedBidi( $text = '' ) {
+ $dir = Language::strongDirFromContent( $text );
+ if ( $dir === 'ltr' ) {
+ // Wrap in LEFT-TO-RIGHT EMBEDDING ... POP DIRECTIONAL FORMATTING
+ return self::$lre . $text . self::$pdf;
+ }
+ if ( $dir === 'rtl' ) {
+ // Wrap in RIGHT-TO-LEFT EMBEDDING ... POP DIRECTIONAL FORMATTING
+ return self::$rle . $text . self::$pdf;
+ }
+ // No strong directionality: do not wrap
+ return $text;
+ }
+