Changeset 1193 for trunk/plugins


Ignore:
Timestamp:
11/08/09 00:46:04 (10 years ago)
Author:
gogo
Message:

Ticket #1227

New boolean parameter to stylistLoadStyles, stylistLoadStylesheet, ripStylesFromCSSFile and ripStylesFromCSSString which allows to disable the reading of imports.

ripStylesFromCSSString can now handle imports also, import processing moved to this function.

Multi-level imports (imports from within imports) now also processed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/Stylist/Stylist.js

    r1192 r1193  
    88/** 
    99 * This method loads an external stylesheet and uses it in the stylist 
     10 * 
     11 * @param string URL to the stylesheet 
     12 * @param hash Alternate descriptive names for your classes  
     13 *              { '.fooclass': 'Foo Description' } 
     14 * @param bool If set true then @import rules in the stylesheet are skipped, 
     15 *   otherwise they will be incorporated if possible. 
    1016 */ 
    11 Xinha.Config.prototype.stylistLoadStylesheet = function(url, altnames) 
     17  
     18Xinha.Config.prototype.stylistLoadStylesheet = function(url, altnames, skip_imports) 
    1219{ 
    1320  if(!altnames) altnames = { }; 
    14   var newStyles = Xinha.ripStylesFromCSSFile(url); 
     21  var newStyles = Xinha.ripStylesFromCSSFile(url, skip_imports); 
    1522  for(var i in newStyles) 
    1623  { 
     
    3441/** 
    3542 * This method takes raw style definitions and uses them in the stylist 
     43 * 
     44 * @param string CSS 
     45 * 
     46 * @param hash Alternate descriptive names for your classes  
     47 *              { '.fooclass': 'Foo Description' } 
     48 * 
     49 * @param bool If set true then @import rules in the stylesheet are skipped, 
     50 *   otherwise they will be incorporated if possible. 
     51 * 
     52 * @param string If skip_imports is false, this string should contain 
     53 *   the "URL" of the stylesheet these styles came from (doesn't matter 
     54 *   if it exists or not), it is used when resolving relative URLs etc.   
     55 *   If not provided, it defaults to Xinha.css in the Xinha root. 
    3656 */ 
    37 Xinha.Config.prototype.stylistLoadStyles = function(styles, altnames) 
     57  
     58Xinha.Config.prototype.stylistLoadStyles = function(styles, altnames, skip_imports, imports_relative_to) 
    3859{ 
    3960  if(!altnames) altnames = { }; 
    40   var newStyles = Xinha.ripStylesFromCSSString(styles); 
     61  var newStyles = Xinha.ripStylesFromCSSString(styles, skip_imports); 
    4162  for(var i in newStyles) 
    4263  { 
     
    463484 
    464485 
    465 Xinha.ripStylesFromCSSFile = function(URL) 
     486Xinha.ripStylesFromCSSFile = function(URL, skip_imports) 
    466487{ 
    467488  var css = Xinha._geturlcontent(URL); 
    468   var RE_atimport = '@import\\s*(url\\()?["\'](.*)["\'].*'; 
    469   var imports = css.match(new RegExp(RE_atimport,'ig')); 
    470   var m, file, re = new RegExp(RE_atimport,'i'); 
    471  
    472   if (imports) 
    473   { 
    474     var path = URL.replace(/\?.*$/,'').split("/"); 
    475     path.pop(); 
    476     path = path.join('/'); 
    477     for (var i=0;i<imports.length;i++) 
    478     { 
    479       m = imports[i].match(re); 
    480       file = m[2]; 
    481       if (!file.match(/^([^:]+\:)?\//)) 
    482       { 
    483         file = Xinha._resolveRelativeUrl(path,file); 
    484       } 
    485       css += Xinha._geturlcontent(file); 
    486     } 
    487   } 
    488  
    489   return Xinha.ripStylesFromCSSString(css); 
    490 }; 
    491  
    492 Xinha.ripStylesFromCSSString = function(css) 
    493 { 
     489  
     490  return Xinha.ripStylesFromCSSString(css, skip_imports, URL); 
     491}; 
     492 
     493Xinha.ripStylesFromCSSString = function(css, skip_imports, imports_relative_to) 
     494{ 
     495  if(!skip_imports) 
     496  {     
     497    if(!imports_relative_to)  
     498    { 
     499      imports_relative_to = _editor_url + 'Xinha.css' 
     500    } 
     501     
     502    var seen = { }; 
     503     
     504    function resolve_imports(css, url) 
     505    { 
     506      seen[url] = true; // protects against infinite recursion 
     507       
     508      var RE_atimport = '@import\\s*(url\\()?["\'](.*)["\'].*'; 
     509      var imports = css.match(new RegExp(RE_atimport,'ig')); 
     510      var m, file, re = new RegExp(RE_atimport,'i'); 
     511 
     512      if (imports) 
     513      { 
     514        var path = url.replace(/\?.*$/,'').split("/"); 
     515        path.pop(); 
     516        path = path.join('/'); 
     517        for (var i=0;i<imports.length;i++) 
     518        { 
     519          m = imports[i].match(re); 
     520          file = m[2]; 
     521          if (!file.match(/^([^:]+\:)?\//)) 
     522          { 
     523            file = Xinha._resolveRelativeUrl(path,file); 
     524          } 
     525                     
     526          if(seen[file]) continue; 
     527           
     528          css += resolve_imports(Xinha._geturlcontent(file), file); 
     529        } 
     530      } 
     531       
     532      return css; 
     533    } 
     534     
     535    css = resolve_imports(css, imports_relative_to); 
     536  } 
     537 
    494538  // We are only interested in the selectors, the rules are not important 
    495539  //  so we'll drop out all coments and rules 
Note: See TracChangeset for help on using the changeset viewer.