From 746912e6aaa3897d1da1da331339fe35bd75bb50 Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Wed, 13 Aug 2014 15:55:57 +0300 Subject: [PATCH] Adding Q&D client-side support for RTL posts automatic redirection --- extensions/bidi_tweets.js | 110 ++++++++++++++++++++++++++++++++++++++ inc/header.php | 1 + inc/html.php | 2 +- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 extensions/bidi_tweets.js diff --git a/extensions/bidi_tweets.js b/extensions/bidi_tweets.js new file mode 100644 index 00000000..2a5a6f8f --- /dev/null +++ b/extensions/bidi_tweets.js @@ -0,0 +1,110 @@ +/** + * hebrew4wp: incredibly simple Hebrew and Arabic support for bilingual WordPress installations + * + * Copyright (c) 2008 Nadav Elyada + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Hacked by Ira Abramov on 2013-02-19 to Bring the BiDi magic to Tweetnest + */ + +/** + * Takes two function pointers f1 and f2, and returns a single function executing both f1 and f2. + * If either function is undefined, execution proceeds with the next defined function, if any. + * Inspired by Robert Hahn + */ +function biditweets_chain(f1, f2) { + return function() { + if(f1) { + f1(); + } + + if(f2) { + f2(); + } + } +} + +/** + * Takes the Unicode code of any character, and returns true if this character is an Hebrew character + * according to the Unicode standard; false otherwise. + */ +function biditweets_is_hebrew_char(c) { + //The range (0x05D0 thru 0x05EA) should be enough (?) + if((c >= 0x0590 && c <= 0x05FF) || (c >= 0xFB1D && c <= 0xFB40)) { + return true; + } else { + return false; + } +} + +/** + * Takes the Unicode code of any character, and returns true if this character is an Arabic character + * according to the Unicode standard; false otherwise. + */ +function biditweets_is_arabic_char(c) { + if((c >= 0x0600 && c <= 0x06FF) || (c >= 0x0750 && c <= 0x077F) || + (c >= 0xFB50 && c <= 0xFDFF) || (c >= 0xFE70 && c <= 0xFEFF)) { + return true; + } else { + return false; + } +} + +/** + * Takes a string, and returns true if this string contains RTL text; false otherwise. + * This implementation treats any string containing one or more RTL characters as RTL text. + * This implementation considers all Hebrew and Arabic characters (as defined by the Unicode standard), + * and no other characters, to be RTL characters. + */ +function tweet_is_rtl_text(s) { + for(var i = 0; i < (s.length ); i++) { + var c = s.charCodeAt(i); + + if(biditweets_is_hebrew_char(c) || biditweets_is_arabic_char(c)) { + return true; + } + } + + return false; +} + +/** + * The biditweets window.onload handler. + */ +function biditweets_onload() { + //Get all the elements in the document and walk over them + var elements = document.getElementsByClassName("tweettext"); + for(var i = 0; i < elements.length; i++) { + var el = elements[i]; + var entryHTML = el.innerHTML; + + if(tweet_is_rtl_text(entryHTML)) { + //If any RTL characters are found, set the element's DIR attribute to "right-to-left". + el.dir = "rtl"; + } else { el.dir = "ltr"; } + } +} + +/** + * Chains the biditweets window.onload handler to any existing window.onload handlers. + */ +window.onload = biditweets_chain(window.onload, biditweets_onload); diff --git a/inc/header.php b/inc/header.php index 34b11791..dd0b3183 100644 --- a/inc/header.php +++ b/inc/header.php @@ -18,6 +18,7 @@ +
diff --git a/inc/html.php b/inc/html.php index ae7520df..c96d2b6b 100644 --- a/inc/html.php +++ b/inc/html.php @@ -151,7 +151,7 @@ function tweetHTML($tweet, $tabs = 4){ $d = $t . "
\n" . ($tweet['favorite'] ? $t . "\t
(A personal favorite)
\n" : "") . - $t . "\t

" . + $t . "\t

" . ($rt ? "" . $retweet['screenname'] . " " : "") . nl2br(p(highlightQuery($htmlcontent, $tweet), 3)) . "

\n" .