Skip to content

Instantly share code, notes, and snippets.

@Comandeer
Created March 21, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Comandeer/de6c832993a8b9c21b01 to your computer and use it in GitHub Desktop.
Save Comandeer/de6c832993a8b9c21b01 to your computer and use it in GitHub Desktop.
function getContextualizedSelector( element, selector ) {
var escapedId = CKEDITOR.tools.escapeCss( element.$.id ),
selectorParts = selector.split( /,\s*/ ),
splitRegex = /([\.#\[\s\+>:]|[\^*]*=["']*|["']*[\]]$)/,
notEscapeCharacters = [ '', ' ', '+', '>', '[', ']', '.', '#', '=', '"', "'", '^', '*', ':' ],
currentPart,
escapedPart,
i,
j;
for ( i = 0; i < selectorParts.length; i++ ) {
currentPart = CKEDITOR.tools.trim( selectorParts[ i ] ).split( splitRegex );
escapedPart = '';
for ( j = 0; j < currentPart.length; j++ ) {
escapedPart += CKEDITOR.tools.indexOf( notEscapeCharacters, currentPart[ j ].charAt( 0 ) ) === -1 ?
CKEDITOR.tools.escapeCss( currentPart[ j ] ) : currentPart[ j ];
}
selectorParts[ i ] = escapedPart;
}
return '#' + escapedId + ' ' + selectorParts.join( ', #' + escapedId + ' ' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment