Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Right-to-left detector (proof-of-concept)</h1>
<section class="intro">
   <div class="description">
<p>The goal of this exercise is to detect the correct writing direction, LTR or RTL, based on the <a href="https://en.wikipedia.org/wiki/Writing_system" target="_blank">writing system</a>. It uses a sample of the text and finds the Unicode code point of each letter. If there are more characters from an RTL system, RTL wins and vice-versa. </p>     
   <p>This is a proof-of-concept and should not be used for production. If you intend to develop this further and are looking for larger text bodies, <a href="https://github.com/patrickschur/language-detection" target="_blank">this repository</a> contains copies of <a href="https://www.ohchr.org/EN/UDHR/Pages/UDHRIndex.aspx" target="_blank">The Universal Declaration of Human Rights</a> in a number of languages in text format.</p>
   </div>
<div class="buttons">
     <button onclick="setExpectedDirection()">Set correct writing direction</button>
     <button onclick="setInitialDirection()">Reset</button>
   </div>
</section>
<main>
   
  <section>
     <dl>
        <dt>Latin</dt>
        <!-- data-expected-dir is used for the red/green boxes -->
        <dd data-expected-dir="ltr">Lorem ipsum dolor sit amet, consectetur pharetra hendrerit adipiscing elit. Duis viverra, eros et porta convallis, felis urna luctus eros, pharetra hendrerit diam velit sed tortor. Sed viverra, augue eum luctus. </dd>
        <dt>Cyrillic</dt>
        <dd data-expected-dir="ltr">Лорем ипсум долор сит амет, ан аетерно салутатус нец, ет ферри ирацундиа сцрипторем вим. Ад сеа деленити сенсерит, ет фугит денияуе фиерент цум опортере интеллегебат. </dd>
        <dt>Devanagari</dt>
        <dd data-expected-dir="ltr">गयेगया यन्त्रालय पुष्टिकर्ता बनाने होभर मानसिक मुश्किल सार्वजनिक प्राण बदले समजते संभव शुरुआत पहोचाना समूह संस्था दर्शाता बनाना संदेश अधिक तकरीबन गटकउसि विषय मुख्यतह स्वतंत्र विवरन रखति देखने </dd>
        
     <dl>
   </section>
  <section>
     <dl>
        <dt>Arabic</dt>
        <dd data-expected-dir="rtl">كان ان الأول معارضة معزّزة, معقل الأثنان مع بحق, دارت معقل دنو ٣٠. جورج وبعد وتنامت عدد عن. تم تعد فبعد سبتمبر, و ولم أراض مدينة وقامت. ترتيب إتفاقية والنرويج عن تحت, بـ وصل وكسبت قبضتهم, ضرب مرجع بخطوط بمباركة قد.</dd>
        <dt>Hebrew</dt>
        <dd data-expected-dir="rtl">מוסיקה המקושרים סוציולוגיה את כדי, מה מדע ביוני המלחמה. שער לכאן באגים הבקשה אל, דת רקטות גרמנית צרפתית עזה, ואלקטרוניקה ביוטכנולוגיה שכל של. צרפתית תקשורת שימושיים ויש אם, שתפו למתחילים על זאת.
</dd>
        <dt>Hanzi</dt>
        <dd data-expected-dir="ltr">岸取耳作日断述事子使加読。写土験類紙王討完感医測線針月。主最一面詳収能御体高重歳生。辺来選房想能辛本語婦日寄月市付。断肌岩達厳層景済余来栗紹充。口関話大乙</dd>
     <dl>
  </section>
</main>
   
   
              
            
!

CSS

              
                // Note: The demo also uses `normalize.css`
// and https://codepen.io/draber/pen/BayEGwb

body {
   padding: 1rem 2rem;
}

p {
   margin: 0 0 2rem 0;
}

dt {
   font-weight: bold;
   margin: 0 0 0.5rem 0;
}

dd {
   margin: 0 0 2rem 0;
   padding: 1.5rem;
   border: 0.1rem var(--border-color) solid;
   position: relative;
   &::before {
      content: "Direction should be " attr(data-expected-dir) ", is " attr(dir);
      position: absolute;
      display: inline-block;
      top: -2.8rem;
      left: 10rem;
      background: darkGreen;
      padding: 0.2rem 1rem;
   }
}

[data-expected-dir="rtl"][dir="ltr"]::before {
   background: darkRed;
}

button {
   margin: 0.2rem;
}

main, .intro {
   display:flex;
}

main {
   justify-content: space-between;
   section {      
      width: 48%;
   }
}

.intro {
   margin-bottom: 1rem;
   //align-items: center;
}

.description {
   width: 60%;
   margin-right: 5rem;
}

              
            
!

JS

              
                // `range()` code copied from https://dev.to/ycmjason/how-to-create-range-in-javascript-539i
const range = (start, end) => {
    const length = end - start;
    return Array.from({ length }, (_, i) => start + i);
}

// Unicode blocks for RTL scripts, everything else is assumed to be LTR
const blocks = {
   arabic: [1536, 1791],
   aramaic: [67648, 67679],
   hebrew: [1424, 1535],
   syriac: [1792, 1871],
   thaana: [1920, 1983]
}
let codePoints = [];
for(let [key, block] of Object.entries(blocks)) {
   codePoints = codePoints.concat(range(block[0], block[1]));
}

// the text blocks
const lorems = document.querySelectorAll("dd");

// Initially no dir attribute is set, so all should default to LTR
const setInitialDirection = () => {
   for(const entry of lorems) {      
      // only relevant for reset
      entry.removeAttribute("dir");
      // find out what the browser thinks
      entry.setAttribute(
         "dir",         window.getComputedStyle(entry).getPropertyValue("direction")
      );
   }
};

// Calculate the actually required direction based on a sample.
// I have seen latin digits in other writing systems, which is 
// why they are taken out of the equation.
// The size of the unfiltered sample is set to 50. This is maybe
// too little and requires more testing.
const setExpectedDirection = () => {
   let codePoint;
   const sampleSize = 50;
   for(const entry of lorems) {
      const sample = Array.from(
         entry.textContent.substr(0, sampleSize)
      ).filter( value => !!value && isNaN(value));

      let rtlMarker = 0;
      let ltrMarker = 0;

      sample.forEach(function(char) {
         if (codePoints.includes(char.codePointAt(0))) {
            rtlMarker++;
         } else {
            ltrMarker++;
         }
      });
      // Overly optimistic formula, some functionality 
      // using a threshold might be the right direction.
      if (rtlMarker > ltrMarker) {
         entry.setAttribute("dir", "rtl");
      }
   }      
};

setInitialDirection();

              
            
!
999px

Console