Merge pull request #3808 from evazion/fix-3807

Tag scripts: drop [if ...] and [reset] syntax (fix #3807, #3773)
This commit is contained in:
Albert Yi
2018-08-13 18:21:58 -07:00
committed by GitHub
5 changed files with 7 additions and 72 deletions

View File

@@ -38,5 +38,4 @@ export { default as Note } from '../src/javascripts/notes.js';
export { default as PostModeMenu } from '../src/javascripts/post_mode_menu.js';
export { default as Utility } from '../src/javascripts/utility.js';
export { default as Ugoira } from '../src/javascripts/ugoira.js';
export { default as Shortcuts } from '../src/javascripts/shortcuts.js';
export { default as TagScript } from '../src/javascripts/tag_script.js';

View File

@@ -2,7 +2,6 @@ import Utility from './utility'
import Cookie from './cookie'
import Post from './posts.js.erb'
import Favorite from './favorites'
import TagScript from './tag_script'
let PostModeMenu = {};
@@ -175,7 +174,7 @@ PostModeMenu.click = function(e) {
} else if (s === "tag-script") {
var current_script_id = Cookie.get("current_tag_script_id");
var tag_script = Cookie.get("tag-script-" + current_script_id);
TagScript.run(post_id, tag_script);
Post.tag(post_id, tag_script);
} else {
return;
}

View File

@@ -532,6 +532,11 @@ Post.vote = function(score, id) {
});
}
Post.tag = function(post_id, tags) {
const tag_string = (Array.isArray(tags) ? tags.join(" ") : String(tags));
Post.update(post_id, { "post[old_tag_string]": "", "post[tag_string]": tag_string });
}
Post.update = function(post_id, params) {
Post.notice_update("inc");

View File

@@ -1,58 +0,0 @@
import Utility from './utility'
import Post from './posts.js.erb'
let TagScript = {};
TagScript.parse = function(script) {
return script.match(/\[.+?\]|\S+/g);
}
TagScript.test = function(tags, predicate) {
var split_pred = predicate.match(/\S+/g);
var is_true = true;
$.each(split_pred, function(i, x) {
if (x[0] === "-") {
if ($.inArray(x.substr(1, 100), tags)) {
is_true = false;
}
} else if (!$.inArray(x, tags)) {
is_true = false;
}
});
return is_true;
}
TagScript.process = function(tags, command) {
if (command.match(/^\[if/)) {
var match = command.match(/\[if\s+(.+?)\s*,\s*(.+?)\]/);
if (TagScript.test(tags, match[1])) {
return TagScript.process(tags, match[2]);
} else {
return tags;
}
} else if (command === "[reset]") {
return [];
} else if (command[0] === "-" && !command.match(/^(?:-pool|-parent|-fav|-favgroup):/i)) {
return Utility.reject(tags, function(x) {return x === command.substr(1, 100)});
} else {
tags.push(command);
return tags;
}
}
TagScript.run = function(post_id, tag_script) {
var commands = TagScript.parse(tag_script);
var $post = $("#post_" + post_id);
var old_tags = $post.data("tags");
$.each(commands, function(i, x) {
var array = String($post.data("tags")).match(/\S+/g);
$post.data("tags", TagScript.process(array, x).join(" "));
});
Post.update(post_id, {"post[old_tag_string]": old_tags, "post[tag_string]": $post.data("tags")});
}
export default TagScript

View File

@@ -99,16 +99,6 @@ Utility.without = function(array, element) {
return temp;
}
Utility.reject = function(array, f) {
var filtered = [];
$.each(array, function(i, x) {
if (!f(x)) {
filtered.push(x);
}
});
return filtered;
}
Utility.regexp_escape = function(string) {
return string.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}