| 1 | /* |
| 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * @stylesheetParser plugin. |
| 8 | */ |
| 9 | |
| 10 | (function() |
| 11 | { |
| 12 | // Rules that must be skipped |
| 13 | var skipSelectors, |
| 14 | // Rules that are valid |
| 15 | validSelectors; |
| 16 | |
| 17 | // We want to extract only the elements with classes defined in the stylesheets: |
| 18 | function parseClasses( aRules ) |
| 19 | { |
| 20 | // aRules are just the different rules in the style sheets |
| 21 | // We want to merge them and them split them by commas, so we end up with only |
| 22 | // the selectors |
| 23 | var s = aRules.join(' '); |
| 24 | // Remove selectors splitting the elements, leave only the class selector (.) |
| 25 | s = s.replace( /(,|>|\+|~)/g, ' ' ); |
| 26 | // Remove attribute selectors: table[border="0"] |
| 27 | s = s.replace( /\[[^\]]*/g, '' ); |
| 28 | // Remove Ids: div#main |
| 29 | s = s.replace( /#[^\s]*/g, '' ); |
| 30 | // Remove pseudo-selectors and pseudo-elements: :hover :nth-child(2n+1) ::before |
| 31 | s = s.replace( /\:{1,2}[^\s]*/g, '' ); |
| 32 | |
| 33 | s = s.replace( /\s+/g, ' ' ); |
| 34 | var aSelectors = s.split( ' ' ) , |
| 35 | aClasses = []; |
| 36 | |
| 37 | for( var i=0; i<aSelectors.length; i++ ) |
| 38 | { |
| 39 | var selector = aSelectors[ i ]; |
| 40 | |
| 41 | if ( validSelectors.test( selector ) && !skipSelectors.test( selector ) ) |
| 42 | { |
| 43 | // If we still don't know about this one, add it |
| 44 | if ( CKEDITOR.tools.indexOf( aClasses, selector ) == -1 ) |
| 45 | aClasses.push( selector ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return aClasses; |
| 50 | } |
| 51 | |
| 52 | function LoadStylesCSS( theDoc ) |
| 53 | { |
| 54 | var styles = [], |
| 55 | // It will hold all the rules of the applied stylesheets (except those internal to CKEditor) |
| 56 | aRules = [], |
| 57 | i; |
| 58 | for( i = 0; i < theDoc.styleSheets.length; i++ ) |
| 59 | { |
| 60 | var sheet = theDoc.styleSheets[ i ], |
| 61 | node = sheet.ownerNode || sheet.owningElement; |
| 62 | |
| 63 | // Skip the internal stylesheets |
| 64 | if ( node.getAttribute( 'cke_temp' ) ) |
| 65 | continue; |
| 66 | |
| 67 | // Exclude stylesheets injected by extensions |
| 68 | if ( sheet.href && sheet.href.substr(0, 9) == 'chrome://' ) |
| 69 | continue; |
| 70 | |
| 71 | var sheetRules = (sheet.cssRules || sheet.rules); |
| 72 | for( var j = 0; j < sheetRules.length; j++ ) |
| 73 | aRules.push( sheetRules[j].selectorText ); |
| 74 | } |
| 75 | |
| 76 | var aClasses = parseClasses( aRules ); |
| 77 | |
| 78 | // Add each style to our "Styles" collection. |
| 79 | for( i=0; i<aClasses.length; i++ ) |
| 80 | { |
| 81 | var oElement = aClasses[ i ].split( '.' ), |
| 82 | element = oElement[ 0 ].toLowerCase(), |
| 83 | sClassName = oElement[ 1 ]; |
| 84 | |
| 85 | styles.push( { |
| 86 | name : aClasses[ i ], |
| 87 | element : element, |
| 88 | attributes : {'class' : sClassName} |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | return styles; |
| 93 | } |
| 94 | |
| 95 | // Register a plugin named "stylesheetparser". |
| 96 | CKEDITOR.plugins.add( 'stylesheetparser', |
| 97 | { |
| 98 | init : function( editor ) |
| 99 | { |
| 100 | skipSelectors = editor.config.stylesheetParser_skipSelectors || /(^body\.|^\.)/i; |
| 101 | validSelectors = editor.config.stylesheetParser_validSelectors || /\w+\.\w+/; |
| 102 | |
| 103 | editor.on( 'mode', function( e ) |
| 104 | { |
| 105 | if ( editor.mode != 'wysiwyg' ) |
| 106 | return; |
| 107 | |
| 108 | // Do this only once |
| 109 | e.removeListener(); |
| 110 | |
| 111 | editor.getStylesSet( function( stylesDefinitions ) |
| 112 | { |
| 113 | // Add the styles found in the document |
| 114 | editor._.stylesDefinitions = stylesDefinitions.concat( LoadStylesCSS( editor.document.$ ) ); |
| 115 | |
| 116 | // Refresh the styles combo |
| 117 | var combo = editor.ui._.items[ 'Styles' ]; |
| 118 | combo && combo.args[0].reset(); |
| 119 | } ); |
| 120 | }); |
| 121 | |
| 122 | } |
| 123 | }); |
| 124 | })(); |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Regular Expression to check if a css rule must be skipped by |
| 129 | * the stylesheet parser plugin (so it's ignored and not available) |
| 130 | * @name CKEDITOR.config.stylesheetParser_skipSelectors |
| 131 | * @type RegExp |
| 132 | * @default /(^body\.|^\.)/i |
| 133 | * @since 3.5 |
| 134 | * @example |
| 135 | * // Ignore rules for body, caption, only a class or classes starting with "high". |
| 136 | * config.stylesheetParser_skipSelectors = /(^body\.|^caption\.|\.high|^\.)/i; |
| 137 | */ |
| 138 | |
| 139 | /** |
| 140 | * Regular Expression to check if a css rule must be allowed by |
| 141 | * the stylesheet parser plugin |
| 142 | * @name CKEDITOR.config.stylesheetParser_validSelectors |
| 143 | * @type RegExp |
| 144 | * @default /\w+\.\w+/ |
| 145 | * @since 3.5 |
| 146 | * @example |
| 147 | * // Add only rules for p and span elements. |
| 148 | * config.stylesheetParser_validSelectors = /\^(p|span)\.\w+/; |
| 149 | */ |
| 150 | No newline at end of file |