Skip to content

Commit

Permalink
Deal with components that don't have children
Browse files Browse the repository at this point in the history
truncate the html content before stripping it. cuts down on the amount of memory needed to run this check with large contents.

add in support for neutral characters in the text

call the isRTL function. Helps that.
  • Loading branch information
blowery committed Dec 7, 2016
1 parent 9cdd457 commit d86f127
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions client/components/auto-direction/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ import { stripHTML } from 'lib/formatting';

const user = userModule();

const RTL_THRESHOLD = 0.5;
const MAX_LENGTH_OF_TEXT_TO_EXAMINE = 100;

/***
* Gets text content from react element in case that's a leaf element
* @param {React.Element} reactElement react element
* @returns {string|null} returns a text content of the react element or null if it's not a leaf element
*/
const getContent = ( reactElement ) => {
if ( ! reactElement ) {
return null;
}
const { props } = reactElement;

if ( ! props ) {
return null;
}

// The child is a text node
if ( typeof props.children === 'string' ) {
return props.children;
Expand All @@ -28,7 +38,7 @@ const getContent = ( reactElement ) => {
if ( typeof props.dangerouslySetInnerHTML === 'object' ) {
// Strip tags because we're only interested in the text, not markup
return props.dangerouslySetInnerHTML.__html
? stripHTML( props.dangerouslySetInnerHTML.__html )
? stripHTML( props.dangerouslySetInnerHTML.__html.substring( 0, Math.floor( MAX_LENGTH_OF_TEXT_TO_EXAMINE * 1.25 ) ) )
: '';
}

Expand Down Expand Up @@ -100,14 +110,18 @@ const rtlCharacterRanges = [
},
];

const RTL_THRESHOLD = 0.5;
const MAX_LENGTH_OF_TEXT_TO_EXAMINE = 100;


const isRTLCharacter = ( character ) => {
const characterCode = character.charCodeAt( 0 );
return rtlCharacterRanges.some( range => range.start <= characterCode && range.end >= characterCode );
};

const whitespaceRe = /[\s\-\_0-9]/u;
const isNeutralCharacter = ( character ) => {
return whitespaceRe.test( character );
}

/***
* Gets the main directionality in a text
* It returns what kind of characters we had the most, RTL or LTR according to some ratio
Expand All @@ -117,12 +131,21 @@ const isRTLCharacter = ( character ) => {
*/
const getTextMainDirection = ( text ) => {
let rtlCount = 0;
let countedCharacters = 0;
const examinedLength = Math.min( MAX_LENGTH_OF_TEXT_TO_EXAMINE, text.length );
for ( let i = 0; i < examinedLength; i++ ) {
if ( isNeutralCharacter( text[ i ] ) ) {
continue;
}
rtlCount += isRTLCharacter( text[ i ] ) ? 1 : 0;
countedCharacters++;
}

return ( rtlCount / examinedLength > RTL_THRESHOLD ) ? 'rtl' : 'ltr';
if ( countedCharacters === 0 ) {
return user.isRTL() ? 'rtl' : 'ltr';
}

return ( rtlCount / countedCharacters > RTL_THRESHOLD ) ? 'rtl' : 'ltr';
};

/***
Expand Down Expand Up @@ -154,9 +177,13 @@ const setChildDirection = ( child ) => {
return child;
}

return React.cloneElement( child, {
children: React.Children.map( child.props.children, setChildDirection )
} );
if ( child && child.props.children ) {
return React.cloneElement( child, {
children: React.Children.map( child.props.children, setChildDirection )
} );
}

return child;
};

/***
Expand Down

0 comments on commit d86f127

Please sign in to comment.