Changeset 25278

Show
Ignore:
Timestamp:
11/28/08 23:29:27 (4 years ago)
Author:
teramako
Message:

change algorithm(replaceVariable) and fix bug

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/copy.js

    r25152 r25278  
    118118    var win = new XPCNativeWrapper(window.content.window); 
    119119    var sel = '',htmlsel = ''; 
    120     if (str.indexOf('%SEL%') >= 0 || str.indexOf('%HTMLSEL%') >= 0){ 
    121         sel = win.getSelection().rangeCount()>0? win.getSelection().getRangeAt(0): ''; 
    122     } 
    123     if (str.indexOf('%HTMLSEL%') >= 0){ 
    124         var serializer = new XMLSerializer(); 
    125         htmlsel = serializer.serializeToString(sel.cloneContents()); 
    126     } 
    127     return str.replace(/%TITLE%/g,buffer.title) 
    128               .replace(/%URL%/g,buffer.URL) 
    129               .replace(/%SEL%/g,sel.toString()) 
    130               .replace(/%HTMLSEL%/g,htmlsel); 
     120    var selection =  win.getSelection(); 
     121    function replacer(value){ //{{{ 
     122        switch(value){ 
     123            case '%TITLE%': 
     124                return buffer.title; 
     125            case '%URL%': 
     126                return buffer.URL; 
     127            case '%SEL%': 
     128                if (sel) 
     129                    return sel; 
     130                else if (selection.rangeCount < 1) 
     131                    return ''; 
     132 
     133                for (var i=0, c=selection.rangeCount; i<c; i++){ 
     134                    sel += selection.getRangeAt(i).toString(); 
     135                } 
     136                return sel; 
     137            case '%HTMLSEL%': 
     138                if (htmlsel) 
     139                    return sel; 
     140                else if (selection.rangeCount < 1) 
     141                    return ''; 
     142 
     143                var serializer = new XMLSerializer(); 
     144                for (var i=0, c=selection.rangeCount; i<c; i++){ 
     145                    htmlsel += serializer.serializeToString(selection.getRangeAt(i).cloneContents()); 
     146                } 
     147                return htmlsel; 
     148        } 
     149        return ''; 
     150    } //}}} 
     151    return str.replace(/%(TITLE|URL|SEL|HTMLSEL)%/g, replacer); 
    131152} 
    132153 
     
    169190        } else { 
    170191            if (!arg) arg = liberator.globalVariables.copy_templates[0]; 
    171             var template = getCopyTemplate(arg) || arg; 
     192            var template = getCopyTemplate(arg) || {value: arg}; 
    172193            if (typeof template.custom == 'function'){ 
    173194                copyString = template.custom.call(this, template.value);