{"id":71,"date":"2009-06-06T14:34:44","date_gmt":"2009-06-06T12:34:44","guid":{"rendered":"http:\/\/www.angeredbrackets.com\/?p=71"},"modified":"2009-06-06T14:36:15","modified_gmt":"2009-06-06T12:36:15","slug":"inserting-bidi-support-tags-in-online-textareas","status":"publish","type":"post","link":"https:\/\/www.angeredbrackets.com\/wordpress\/2009\/06\/inserting-bidi-support-tags-in-online-textareas\/","title":{"rendered":"Inserting BiDi support tags in online Textareas"},"content":{"rendered":"<p>This post will only be relevant to readers of right-to-left languages, and to developers who develop for such languages. As such, my next post will be a translation of this one into Hebrew.<br \/>\nA bit over a year ago, a friend of mine <a href=\"http:\/\/eesh.net\/blog\/?p=234\">wrote about embedded unicode right-to-left and left-to-right characters in a flow of text<\/a>. In a nutshell, when writing text that contains both LTR characters (like English) and RTL characters (like Hebrew), the flow of the text can wind up messed up. Here&#8217;s an example:<br \/>\n<input style=\"direction: rtl; width: 200px;\" type=\"text\" value=\"The flow of this \u05d8\u05e7\u05e1\u05d8 is broken\" \/><\/p>\n<p>The (proper) solution to this conundrum is this: wrap your text, which you want flowing in the opposite direction from it&#8217;s encapsulating element, in the unicode characters for <a href=\"http:\/\/www.fileformat.info\/info\/unicode\/char\/202a\/index.htm\">left-to-right embedding<\/a> (or <a href=\"http:\/\/www.fileformat.info\/info\/unicode\/char\/202b\/index.htm\">right-to-left embedding<\/a>), and the <a href=\"http:\/\/www.fileformat.info\/info\/unicode\/char\/202c\/index.htm\">Pop Directional Formatting<\/a> character. For further reading, go <a href=\"http:\/\/www.w3.org\/International\/questions\/qa-bidi-controls.en.php\">here<\/a>.<\/p>\n<p>Back when Liron wrote that post of his, I thought it would be a nice little project to tackle &#8211; write a Firefox plugin or Greasemonkey user-script that allows you to encapsulate your text in these characters, for the sake of all those who will follow and read it. I have since then become familiar &#8211; and very fond of &#8211; bookmarklets. I think a bookmarklet is the best way to distribute the solution to this problem, because it&#8217;s lightweight, easy to install and modify, and (at least in theory) cross browser.<!--more--><\/p>\n<p>Coming to tackle this issue, I knew I needed to insert a string of characters in front of and after the selected text in a textarea. Where have I seen this done before?&#8230; Oh, right, right here in WordPress, when I want to make text I&#8217;m writing bold or italicized.<br \/>\nSo I went about finding the Javascript behind that functionality (it&#8217;s the function edInsertTag), and I pulled out of it the bits that are relevant to this situation, and to Firefox.<\/p>\n<p>With no further ado, I present to you <a href=\"javascript:function B(){var s=document.activeElement.selectionStart;var e=document.activeElement.selectionEnd;var v=document.activeElement.value;document.activeElement.value=v.substring(0,s)+'\\u202B'+v.substring(s,e)+'\\u202C'+v.substring(e,v.length);document.activeElement.selectionStart=s;document.activeElement.selectionEnd=e+2};B();\">RTL<\/a> and <a href=\"javascript:function B(){var s=document.activeElement.selectionStart;var e=document.activeElement.selectionEnd;var v=document.activeElement.value;document.activeElement.value=v.substring(0,s)+'\\u202A'+v.substring(s,e)+'\\u202C'+v.substring(e,v.length);document.activeElement.selectionStart=s;document.activeElement.selectionEnd=e+2};B();\">LTR<\/a>.<\/p>\n<p>Simply drag those two links to your bookmark toolbar (I actually have a bookmark folder just for bookmarklets), and next time you&#8217;re writing bidirectional text, select the wrongly-flowing part, and align it properly.<br \/>\nYou can try them out here:<br \/>\n<textarea style=\"direction: ltr; rows:1; width: 300px;\">\u05dc\u05d7\u05e5 \u05e2\u05dc RTL \u05e2\u05dc \u05de\u05e0\u05ea \u05dc\u05ea\u05e7\u05df<\/textarea><br \/>\n<textarea style=\"direction: rtl; rows:1; width: 300px;\">use the LTR \u05db\u05d3\u05d9 \u05dc\u05ea\u05e7\u05df this text<\/textarea><\/p>\n<p>I&#8217;d be happy to get feedback on these bookmarklets. They&#8217;ve only been tested in Firefox, and I think they will not work in other browsers, seeing how I only used one of the three cross-browser-compatibility parts of the wordpress JS I cannibalized. If they do work in other browsers, or do not work as expected in Firefox, please let me know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to properly write bidirectional text that flows against the direction of the textarea in which you&#8217;re typing, you should wrap your text in the unicode characters for the correct embedded directionality.<br \/>\nI wrote a bookmarklet that makes doing so a piece of cake.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[23,24,25],"_links":{"self":[{"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":9,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/posts\/71\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angeredbrackets.com\/wordpress\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}