Skip to content

Commit

Permalink
Added custom prefix support for widget getSkinName
Browse files Browse the repository at this point in the history
  • Loading branch information
sdesai committed Dec 22, 2012
1 parent 9c114d6 commit 840e38b
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 267 deletions.
6 changes: 6 additions & 0 deletions src/widget/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Widget Change History
=====================

@VERSION@
-----

* Added custom prefix support to widget.getSkinName,
derived https://github.com/yui/yui3/pull/327

3.8.0
-----

Expand Down
34 changes: 25 additions & 9 deletions src/widget/js/WidgetSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@
* @module widget
* @submodule widget-skin
*/

var BOUNDING_BOX = "boundingBox",
CONTENT_BOX = "contentBox",
SKIN = "skin",
_getClassName = Y.ClassNameManager.getClassName;

/**
* Returns the name of the skin that's currently applied to the widget.
*
* Searches up the Widget's ancestor axis for, by default, a class
* yui3-skin-(name), and returns the (name) portion. Otherwise, returns null.
*
* This is only really useful after the widget's DOM structure is in the
* document, either by render or by progressive enhancement. Searches up
* the Widget's ancestor axis for a class yui3-skin-(name), and returns the
* (name) portion. Otherwise, returns null.
* document, either by render or by progressive enhancement.
*
* @method getSkinName
* @for Widget
* @return {String} the name of the skin, or null (yui3-skin-sam => sam)
* @param {String} [skinPrefix] The prefix which the implementation uses for the skin
* ("yui3-skin-" is the default).
*
* NOTE: skinPrefix will be used as part of a regular expression:
*
* new RegExp('\\b' + skinPrefix + '(\\S+)')
*
* Although an unlikely use case, literal characters which may result in an invalid
* regular expression should be escaped.
*
* @return {String} The name of the skin, or null, if a matching skin class is not found.
*/

Y.Widget.prototype.getSkinName = function () {
Y.Widget.prototype.getSkinName = function (skinPrefix) {

var root = this.get( CONTENT_BOX ) || this.get( BOUNDING_BOX ),
search = new RegExp( '\\b' + _getClassName( SKIN ) + '-(\\S+)' ),
match;
match,
search;

skinPrefix = skinPrefix || _getClassName(SKIN, "");

search = new RegExp( '\\b' + skinPrefix + '(\\S+)' );

if ( root ) {
root.ancestor( function ( node ) {
Expand All @@ -35,4 +51,4 @@ Y.Widget.prototype.getSkinName = function () {
}

return ( match ) ? match[1] : null;
};
}
Loading

0 comments on commit 840e38b

Please sign in to comment.