Skip to content

jquery-ui:Base text direction support #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/unit/accordion/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ common.testWidget( "accordion", {
"activeHeader": "ui-icon-triangle-1-s",
"header": "ui-icon-triangle-1-e"
},
textDir: null,

// Callbacks
activate: null,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/autocomplete/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ common.testWidget( "autocomplete", {
collision: "none"
},
source: null,
textDir: null,

// Callbacks
change: null,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/button/common-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ common.testWidget( "button", {
label: null,
showLabel: true,
text: true,
textDir: null,

// Callbacks
create: null
Expand Down
1 change: 1 addition & 0 deletions tests/unit/button/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ common.testWidget( "button", {
iconPosition: "beginning",
label: null,
showLabel: true,
textDir: null,

// Callbacks
create: null
Expand Down
1 change: 1 addition & 0 deletions tests/unit/checkboxradio/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ common.testWidget( "checkboxradio", {
disabled: null,
icon: true,
label: null,
textDir: null,

// Callbacks
create: null
Expand Down
1 change: 1 addition & 0 deletions tests/unit/controlgroup/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ common.testWidget( "controlgroup", {
"controlgroupLabel": ".ui-controlgroup-label"
},
onlyVisible: true,
textDir: null,

// Callbacks
create: null
Expand Down
1 change: 1 addition & 0 deletions tests/unit/menu/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common.testWidget( "menu", {
at: "right top"
},
role: "menu",
textDir: null,

// Callbacks
blur: null,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/selectmenu/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ common.testWidget( "selectmenu", {
at: "left bottom",
collision: "none"
},
textDir: null,
width: false,

// Callbacks
Expand Down
1 change: 1 addition & 0 deletions tests/unit/tabs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ common.testWidget( "tabs", {
heightStyle: "content",
hide: null,
show: null,
textDir: null,

// Callbacks
activate: null,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/tooltip/common-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ common.testWidget( "tooltip", {
collision: "flipfit flip"
},
show: true,
textDir: null,
tooltipClass: null,
track: false,

Expand Down
1 change: 1 addition & 0 deletions tests/unit/tooltip/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ common.testWidget( "tooltip", {
collision: "flipfit flip"
},
show: true,
textDir: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These options are alphabetized. All files will need to be updated for the correct order.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

track: false,

// Callbacks
Expand Down
26 changes: 26 additions & 0 deletions ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,32 @@ $.Widget.prototype = {
return !( $.isFunction( callback ) &&
callback.apply( this.element[ 0 ], [ event ].concat( data ) ) === false ||
event.isDefaultPrevented() );
},

_getTextDir: function( text ) {
if ( this.options.textDir === "auto" ) {

// Look for first strong (either English or Arabic/Hebrew) character.
// Resolve text direction accordingly ("rtl" for Arabic/Hebrew, "ltr" otherwise).
var matcher = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec( text );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about which set of characters this is searching for.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return ( matcher && ( matcher[ 0 ] > "z" ) ) ? "rtl" : "ltr";
}
return this.options.textDir;
},

_applyTextDir: function( param ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line between methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if ( typeof param === "string" ) {
param = param.replace( /[\u202A\u202B\u202C]/g, "" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about what these characters are.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// Unicode directional characters: 202A and 202B used to enforce text direction.
// 202C - POP formatter closing directional segment.
return ( this._getTextDir( param ) === "rtl" ? "\u202B" : "\u202A" ) + param + "\u202C";
} else if ( param.jquery ) {
var isField = param.is( "input" ) || param.is( "textarea" );
param.css( "direction", this._getTextDir( isField ? param.val() : param.text() ) );
} else if ( param.nodeType === 1 ) {
param.style.direction = this._getTextDir( param.textContent );
}
}
};

Expand Down
8 changes: 8 additions & 0 deletions ui/widgets/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ return $.widget( "ui.accordion", {
activeHeader: "ui-icon-triangle-1-s",
header: "ui-icon-triangle-1-e"
},
textDir: null,

// Callbacks
activate: null,
Expand Down Expand Up @@ -305,6 +306,13 @@ return $.widget( "ui.accordion", {
this._addClass( this.active.next(), "ui-accordion-content-active" );
this.active.next().show();

if ( this.options.textDir ) {
var that = this;
this.headers.each( function( i, header ) {
header.textContent = that._applyTextDir( header.textContent );
} );
}

this.headers
.attr( "role", "tab" )
.each( function() {
Expand Down
33 changes: 33 additions & 0 deletions ui/widgets/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ $.widget( "ui.autocomplete", {
collision: "none"
},
source: null,
textDir: null,

// Callbacks
change: null,
Expand Down Expand Up @@ -89,6 +90,26 @@ $.widget( "ui.autocomplete", {

this._addClass( "ui-autocomplete-input" );
this.element.attr( "autocomplete", "off" );
if ( this.options.textDir ) {
var textDir = this._getTextDir( this._value() );
this.element.css( "direction", textDir );
if ( this.options.textDir === "auto" ) {
this.element.css( "text-align", textDir === "rtl" ? "right" : "left" );
this._on( this.element, {
keyup: function( e ) {
var keyCode = $.ui.keyCode;
if ( e.keyCode < keyCode.PAGE_UP || e.keyCode >= keyCode.DELETE ) {
var textDir = this._getTextDir( this._value() );
this.element.css( "direction", textDir )
.css( "text-align", textDir === "rtl" ? "right" : "left" );
}
}
} );
} else {
this.element.css( "text-align",
this.element.css( "direction" ) === "rtl" ? "right" : "left" );
}
}

this._on( this.element, {
keydown: function( event ) {
Expand Down Expand Up @@ -288,6 +309,9 @@ $.widget( "ui.autocomplete", {

if ( false !== this._trigger( "select", event, { item: item } ) ) {
this._value( item.value );
if ( this.options.textDir === "auto" ) {
this.element.css( "direction", this._getTextDir( item.value ) );
}
}

// reset the term after the select event
Expand Down Expand Up @@ -521,6 +545,15 @@ $.widget( "ui.autocomplete", {
_suggest: function( items ) {
var ul = this.menu.element.empty();
this._renderMenu( ul, items );

if ( this.options.textDir ) {
ul.css( "text-align", ul.css( "direction" ) === "rtl" ? "right" : "left" );
var that = this;
ul.children().each( function( i, li ) {
li.style.direction = that._getTextDir( li.textContent );
} );
}

this.isNewMenu = true;
this.menu.refresh();

Expand Down
14 changes: 13 additions & 1 deletion ui/widgets/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ $.widget( "ui.button", {
icon: null,
iconPosition: "beginning",
label: null,
showLabel: true
showLabel: true,
textDir: null
},

_getCreateOptions: function() {
Expand Down Expand Up @@ -96,6 +97,14 @@ $.widget( "ui.button", {
this.element.html( this.options.label );
}
}

if ( this.options.textDir ) {
this._applyTextDir( this.element );
if ( this.hasTitle ) {
this.element.attr( "title", this._applyTextDir( this.element.attr( "title" ) ) );
}
}

this._addClass( "ui-button", "ui-widget" );
this._setOption( "disabled", this.options.disabled );
this._enhance();
Expand Down Expand Up @@ -218,6 +227,9 @@ $.widget( "ui.button", {
options.showLabel = true;
}
this._super( options );
if ( ( this.options.textDir && options.label ) || options.textDir ) {
this._applyTextDir( this.element );
}
},

_setOption: function( key, value ) {
Expand Down
17 changes: 16 additions & 1 deletion ui/widgets/checkboxradio.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
classes: {
"ui-checkboxradio-label": "ui-corner-all",
"ui-checkboxradio-icon": "ui-corner-all"
}
},
textDir: null
},

_getCreateOptions: function() {
Expand Down Expand Up @@ -105,6 +106,13 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
this._addClass( this.label, "ui-checkboxradio-radio-label" );
}

if ( this.options.textDir ) {
var markup = $.parseHTML( this.options.label );
if ( markup && markup.length === 1 && markup[ 0 ].nodeType === 3 ) {
this.options.label = this._applyTextDir( this.options.label );
}
}

if ( this.options.label && this.options.label !== this.originalLabel ) {
this._updateLabel();
} else if ( this.originalLabel ) {
Expand Down Expand Up @@ -208,6 +216,13 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {

this._super( key, value );

if ( this.options.textDir && ( key === "label" || key === "textDir" ) ) {
var markup = $.parseHTML( this.options.label );
if ( markup && markup.length === 1 && markup[ 0 ].nodeType === 3 ) {
this.options.label = this._applyTextDir( this.options.label );
}
}

if ( key === "disabled" ) {
this._toggleClass( this.label, null, "ui-state-disabled", value );
this.element[ 0 ].disabled = value;
Expand Down
13 changes: 10 additions & 3 deletions ui/widgets/controlgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ return $.widget( "ui.controlgroup", {
"checkboxradio": "input[type='checkbox'], input[type='radio']",
"selectmenu": "select",
"spinner": ".ui-spinner-input"
}
},
textDir: null
},

_create: function() {
Expand Down Expand Up @@ -92,8 +93,11 @@ return $.widget( "ui.controlgroup", {
if ( element.children( ".ui-controlgroup-label-contents" ).length ) {
return;
}
element.contents()
.wrapAll( "<span class='ui-controlgroup-label-contents'></span>" );
var dir = that.options.textDir ?
"dir='" + that._getTextDir( element.text() ) + "' " : "";

element.contents().wrapAll(
"<span " + dir + "class='ui-controlgroup-label-contents'></span>" );
} );
that._addClass( labels, null, "ui-widget ui-widget-content ui-state-default" );
childWidgets = childWidgets.concat( labels.get() );
Expand Down Expand Up @@ -138,6 +142,9 @@ return $.widget( "ui.controlgroup", {
instanceOptions.classes =
that._resolveClassesValues( instanceOptions.classes, instance );
}
if ( that.options.textDir ) {
instanceOptions.textDir = that.options.textDir;
}
element[ widget ]( instanceOptions );

// Store an instance of the controlgroup to be able to reference
Expand Down
11 changes: 11 additions & 0 deletions ui/widgets/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ return $.widget( "ui.menu", {
at: "right top"
},
role: "menu",
textDir: null,

// Callbacks
blur: null,
Expand Down Expand Up @@ -370,6 +371,16 @@ return $.widget( "ui.menu", {
if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
this.blur();
}
if ( this.options.textDir ) {
menus.each( function() {
var item = $( this );
item.css( "text-align", item.css( "direction" ) === "rtl" ? "right" : "left" );
} );

items.each( function() {
that._applyTextDir( this );
} );
}
},

_itemRole: function() {
Expand Down
8 changes: 8 additions & 0 deletions ui/widgets/selectmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, {
collision: "none"
},
width: false,
textDir: null,

// Callbacks
change: null,
Expand Down Expand Up @@ -318,6 +319,10 @@ return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, {
var that = this,
currentOptgroup = "";

if ( this.options.textDir ) {
ul.css( "text-align", ul.css( "direction" ) === "rtl" ? "right" : "left" );
}

$.each( items, function( index, item ) {
var li;

Expand Down Expand Up @@ -359,6 +364,9 @@ return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, {

_setText: function( element, value ) {
if ( value ) {
if ( this.options.textDir ) {
value = this._applyTextDir( value );
}
element.text( value );
} else {
element.html( "&#160;" );
Expand Down
8 changes: 8 additions & 0 deletions ui/widgets/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ $.widget( "ui.tabs", {
heightStyle: "content",
hide: null,
show: null,
textDir: null,

// Callbacks
activate: null,
Expand Down Expand Up @@ -371,6 +372,13 @@ $.widget( "ui.tabs", {
"aria-hidden": "true"
} );

if ( this.options.textDir ) {
var that = this;
this.tabs.each( function( i, tab ) {
that._applyTextDir( $( tab ).find( ".ui-tabs-anchor" ) );
} );
}

// Make sure one tab is in the tab order
if ( !this.active.length ) {
this.tabs.eq( 0 ).attr( "tabIndex", 0 );
Expand Down
Loading