Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8282: 0010-patch-with-String-extractStyles-String-insertStyle.patch

File 0010-patch-with-String-extractStyles-String-insertStyle.patch, 2.0 kB (added by kangax, 8 months ago)
  • a/src/prototype.js

    old new  
    2222  }, 
    2323 
    2424  ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', 
     25  StyleFragment: '<style[^>]*>([\u0001-\uFFFF]*?)<\/style>', 
    2526  JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,   
    2627   
    2728  emptyFunction: function() { }, 
  • a/src/string.js

    old new  
    7171    }); 
    7272  }, 
    7373   
     74  extractStyles: function() { 
     75    var matchAll = new RegExp(Prototype.StyleFragment, 'img'); 
     76    var matchOne = new RegExp(Prototype.StyleFragment, 'im'); 
     77    return (this.match(matchAll) || []).map(function(styleTag) { 
     78      return (styleTag.match(matchOne) || ['', ''])[1]; 
     79    }); 
     80  }, 
     81   
     82  insertStyle: function() { 
     83    var style = document.createElement('style'); 
     84    var head = document.getElementsByTagName('head')[0]; 
     85     
     86    style.setAttribute('type', 'text/css'); 
     87     
     88    if (style.styleSheet) { 
     89      if (!style.styleSheet.disabled) 
     90        style.styleSheet.cssText = this; 
     91    } 
     92    else { 
     93      var text = document.createTextNode(this); 
     94      style.appendChild(text); 
     95    } 
     96     
     97    head.appendChild(style); 
     98    return this; 
     99  }, 
     100   
    74101  evalScripts: function() { 
    75102    return this.extractScripts().map(function(script) { return eval(script) }); 
    76103  }, 
     104   
     105  applyStyles: function() { 
     106    return this.extractStyles().invoke('insertStyle'); 
     107  }, 
    77108 
    78109  escapeHTML: function() { 
    79110    var self = arguments.callee;