-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ and Troubleshooting
Welcome to the FAQ and Troubleshooting guide for rtl-text-tools. If you are experiencing unexpected behavior, rendering issues, or just have general questions about the library, you will likely find your answer here.
Answer: As of v1.1.0, fixRTL() is strictly precise. Before applying any transformations, it runs a quick hasRTL() check. If your text does not contain any actual RTL characters (Arabic, Persian, Hebrew, etc.), the function will return the original string completely unchanged. This is a deliberate design choice to prevent accidentally modifying English or LTR text.
-
Fix: Ensure your text actually contains RTL characters. If you want to force digit conversion on purely English text, use the specific functions directly (e.g.,
toPersianDigits("123")) instead offixRTL().
Answer: By default, fixRTL() targets Persian. You need to explicitly tell it to use Arabic.
-
Fix: Pass the
langoption:fixRTL("مرحبا 123", { lang: 'arabic' }); // or simply: fixRTL("مرحبا 123", 'arabic');
Answer: Yes! All text processing functions (fixRTL, hasRTL, toPersianDigits, etc.) are pure JavaScript and have zero dependencies. They work perfectly in Node.js, Next.js SSR, and edge environments.
-
Note: The only function that requires the browser DOM is
setDirAttribute(). If you are using Next.js, make sure to callsetDirAttribute()inside auseEffecthook or a client-side event handler so it doesn't throw awindow/document is undefinederror during server rendering.
Answer: No. While setDirAttribute() interacts with the DOM, its TypeScript definition uses structural typing (it just looks for an object with a setAttribute method). This means you can use the library in non-DOM TypeScript projects (like Node.js backend or React Native) without forcing the "dom" lib into your tsconfig.json.
Answer: Yes, fully. The compiled output targets ES5. It uses var instead of const/let, regular functions instead of arrow functions, and standard String.prototype.replace() with global regexes instead of modern methods like String.prototype.replaceAll(). It also avoids the ES6 u (unicode) regex flag to ensure maximum legacy browser compatibility.
The Cause: CSS properties like direction: rtl and unicode-bidi do not work in plain-text environments like <textarea>, <input>, terminal outputs, or the body of HTML emails (where CSS is often stripped).
The Solution: You must inject invisible Unicode bidirectional control characters directly into the text string.
import { fixRTL, wrapRTL } from 'rtl-text-tools';
// Option 1: Use the addBidiMarkers option
const emailText = fixRTL("مرحبا 123", { addBidiMarkers: true });
// Option 2: Wrap specific parts manually
const safeText = wrapRTL("مرحبا") + " " + wrapLTR("https://example.com");The Cause: The Unicode Bidirectional Algorithm often struggles with neutral characters like brackets when they are surrounded by RTL text, causing them to visually reverse. The Solution: Use the bracket fixing utility. It injects an invisible Left-to-Right Mark (LRM) inside the brackets to force the browser to render them correctly.
import { fixBrackets } from 'rtl-text-tools';
// Or just ensure fixBrackets is enabled (it is by default) in fixRTL
const fixedText = fixRTL("هذا نص (مختلط)"); The Cause: In RTL languages, truncation ellipses should visually appear at the start (the right side) of the text, which is the logical beginning of the string.
The Solution: fixRTL() handles this automatically via moveEllipsis(). If you are using individual functions, make sure you are calling moveEllipsis().
import { moveEllipsis } from 'rtl-text-tools';
moveEllipsis("مرحبا..."); // Output: "...مرحبا"The Cause: This usually happens when you are applying RTL styles via JavaScript after the component mounts, rather than rendering it with the correct styles initially.
The Solution: Instead of using setDirAttribute on mount, use the getRTLStyles() helper to apply the styles synchronously during the initial React render.
import { getRTLStyles } from 'rtl-text-tools';
function MyComponent() {
// Applied immediately on first render, no flickering
return <div style={getRTLStyles()}>مرحبا</div>;
}| Framework | Recommendation |
|---|---|
| React | Use getRTLStyles() for inline styles. Use fixRTL() directly inside your JSX {fixRTL(text)}. |
| Next.js | All text functions are SSR-safe. Wrap setDirAttribute() in a useEffect to avoid hydration errors. |
| Vue.js | Create a custom directive (e.g., v-rtl) that applies getRTLStyles() or calls setDirAttribute() in the mounted hook. |
| Angular | Use a custom pipe (e.g., `{{ text |
| React Native | Text functions work perfectly. For styling, use the returned objects from getRTLStyles() directly in your StyleSheet. |
If you've tried the solutions above and are still facing issues, or if you've found a bug in the library:
- Check the API Reference to ensure you are passing the correct parameters.
- Search existing discussions on the GitHub Issues page.
- If it's a new bug, please open a new issue and include:
- The exact string you are passing to the function.
- The options you are using.
- The expected output vs. the actual output.
- Your environment (Browser, Node.js version, React version, etc.).
🏠 Home • 🚀 Getting Started • 📖 API Reference • 🐛 Report Issue
Made with ❤️ by Homayoun Mohammadi
If you find this project useful, please consider ⭐ starring the repo to show your support!
- 🏠 Home
- 🚀 Getting Started
- 🧩 API Overview
- 🛠️ Framework Guides
- 💡 Use Cases
- 🖥️ Browser Compatibility
- ❓ FAQ & Troubleshooting
Homayoun Mohammadi
🔗 GitHub • ✉️ Email
⭐ Love this project? Star it on GitHub to show your support!