2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @stylesheetParser plugin.
12 // We want to extract only the elements with classes defined in the stylesheets:
13 function parseClasses( aRules
, skipSelectors
, validSelectors
)
15 // aRules are just the different rules in the style sheets
16 // We want to merge them and them split them by commas, so we end up with only
18 var s
= aRules
.join(' ');
19 // Remove selectors splitting the elements, leave only the class selector (.)
20 s
= s
.replace( /(,|>|\+|~)/g, ' ' );
21 // Remove attribute selectors: table[border="0"]
22 s
= s
.replace( /\[[^\]]*/g, '' );
23 // Remove Ids: div#main
24 s
= s
.replace( /#[^\s]*/g, '' );
25 // Remove pseudo-selectors and pseudo-elements: :hover :nth-child(2n+1) ::before
26 s
= s
.replace( /\:{1,2}[^\s]*/g, '' );
28 s
= s
.replace( /\s+/g, ' ' );
30 var aSelectors
= s
.split( ' ' ),
33 for ( var i
= 0; i
< aSelectors
.length
; i
++ )
35 var selector
= aSelectors
[ i
];
37 if ( validSelectors
.test( selector
) && !skipSelectors
.test( selector
) )
39 // If we still don't know about this one, add it
40 if ( CKEDITOR
.tools
.indexOf( aClasses
, selector
) == -1 )
41 aClasses
.push( selector
);
48 function LoadStylesCSS( theDoc
, skipSelectors
, validSelectors
)
51 // It will hold all the rules of the applied stylesheets (except those internal to CKEditor)
55 for ( i
= 0; i
< theDoc
.styleSheets
.length
; i
++ )
57 var sheet
= theDoc
.styleSheets
[ i
],
58 node
= sheet
.ownerNode
|| sheet
.owningElement
;
60 // Skip the internal stylesheets
61 if ( node
.getAttribute( 'data-cke-temp' ) )
64 // Exclude stylesheets injected by extensions
65 if ( sheet
.href
&& sheet
.href
.substr(0, 9) == 'chrome://' )
68 var sheetRules
= sheet
.cssRules
|| sheet
.rules
;
69 for ( var j
= 0; j
< sheetRules
.length
; j
++ )
70 aRules
.push( sheetRules
[ j
].selectorText
);
73 var aClasses
= parseClasses( aRules
, skipSelectors
, validSelectors
);
75 // Add each style to our "Styles" collection.
76 for ( i
= 0; i
< aClasses
.length
; i
++ )
78 var oElement
= aClasses
[ i
].split( '.' ),
79 element
= oElement
[ 0 ].toLowerCase(),
80 sClassName
= oElement
[ 1 ];
83 name
: element
+ '.' + sClassName
,
85 attributes
: {'class' : sClassName
}
92 // Register a plugin named "stylesheetparser".
93 CKEDITOR
.plugins
.add( 'stylesheetparser',
95 requires
: [ 'styles' ],
98 var obj
= CKEDITOR
.editor
.prototype;
99 obj
.getStylesSet
= CKEDITOR
.tools
.override( obj
.getStylesSet
, function( org
)
101 return function( callback
)
104 org
.call( this, function( definitions
)
106 // Rules that must be skipped
107 var skipSelectors
= self
.config
.stylesheetParser_skipSelectors
|| ( /(^body\.|^\.)/i ),
108 // Rules that are valid
109 validSelectors
= self
.config
.stylesheetParser_validSelectors
|| ( /\w+\.\w+/ );
111 callback( ( self
._
.stylesDefinitions
= definitions
.concat( LoadStylesCSS( self
.document
.$, skipSelectors
, validSelectors
) ) ) );
122 * A regular expression that defines whether a CSS rule will be
123 * skipped by the Stylesheet Parser plugin. A CSS rule matching
124 * the regular expression will be ignored and will not be available
125 * in the Styles drop-down list.
126 * @name CKEDITOR.config.stylesheetParser_skipSelectors
128 * @default /(^body\.|^\.)/i
130 * @see CKEDITOR.config.stylesheetParser_validSelectors
132 * // Ignore rules for body and caption elements, classes starting with "high", and any class defined for no specific element.
133 * config.stylesheetParser_skipSelectors = /(^body\.|^caption\.|\.high|^\.)/i;
137 * A regular expression that defines which CSS rules will be used
138 * by the Stylesheet Parser plugin. A CSS rule matching the regular
139 * expression will be available in the Styles drop-down list.
140 * @name CKEDITOR.config.stylesheetParser_validSelectors
142 * @default /\w+\.\w+/
144 * @see CKEDITOR.config.stylesheetParser_skipSelectors
146 * // Only add rules for p and span elements.
147 * config.stylesheetParser_validSelectors = /\^(p|span)\.\w+/;