aboutsummaryrefslogtreecommitdiffstats
path: root/copy.js
diff options
context:
space:
mode:
authorteramako2008-11-28 14:29:27 +0000
committerteramako2008-11-28 14:29:27 +0000
commit996dedeaa1cd67a4611cc641b0ceabf70101ed6c (patch)
tree6888843487ee5749a22574daaab1e3a317f352f3 /copy.js
parent27950f81a7a21d8a94747ebe454aac478b95843e (diff)
downloadvimperator-plugins-996dedeaa1cd67a4611cc641b0ceabf70101ed6c.tar.bz2
change algorithm(replaceVariable) and fix bug
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@25278 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to 'copy.js')
-rw-r--r--copy.js45
1 files changed, 33 insertions, 12 deletions
diff --git a/copy.js b/copy.js
index e8b642e..6b0906d 100644
--- a/copy.js
+++ b/copy.js
@@ -117,17 +117,38 @@ function replaceVariable(str){
if (!str) return '';
var win = new XPCNativeWrapper(window.content.window);
var sel = '',htmlsel = '';
- if (str.indexOf('%SEL%') >= 0 || str.indexOf('%HTMLSEL%') >= 0){
- sel = win.getSelection().rangeCount()>0? win.getSelection().getRangeAt(0): '';
- }
- if (str.indexOf('%HTMLSEL%') >= 0){
- var serializer = new XMLSerializer();
- htmlsel = serializer.serializeToString(sel.cloneContents());
- }
- return str.replace(/%TITLE%/g,buffer.title)
- .replace(/%URL%/g,buffer.URL)
- .replace(/%SEL%/g,sel.toString())
- .replace(/%HTMLSEL%/g,htmlsel);
+ var selection = win.getSelection();
+ function replacer(value){ //{{{
+ switch(value){
+ case '%TITLE%':
+ return buffer.title;
+ case '%URL%':
+ return buffer.URL;
+ case '%SEL%':
+ if (sel)
+ return sel;
+ else if (selection.rangeCount < 1)
+ return '';
+
+ for (var i=0, c=selection.rangeCount; i<c; i++){
+ sel += selection.getRangeAt(i).toString();
+ }
+ return sel;
+ case '%HTMLSEL%':
+ if (htmlsel)
+ return sel;
+ else if (selection.rangeCount < 1)
+ return '';
+
+ var serializer = new XMLSerializer();
+ for (var i=0, c=selection.rangeCount; i<c; i++){
+ htmlsel += serializer.serializeToString(selection.getRangeAt(i).cloneContents());
+ }
+ return htmlsel;
+ }
+ return '';
+ } //}}}
+ return str.replace(/%(TITLE|URL|SEL|HTMLSEL)%/g, replacer);
}
var exCopyManager = {
@@ -168,7 +189,7 @@ var exCopyManager = {
}
} else {
if (!arg) arg = liberator.globalVariables.copy_templates[0];
- var template = getCopyTemplate(arg) || arg;
+ var template = getCopyTemplate(arg) || {value: arg};
if (typeof template.custom == 'function'){
copyString = template.custom.call(this, template.value);
} else if (template.custom instanceof Array){