diff --git a/spec/asset/plugins/plugin/index.js b/spec/asset/plugins/plugin/index.js
new file mode 100644
index 0000000..e69de29
diff --git a/spec/asset/plugins/plugin/info.json b/spec/asset/plugins/plugin/info.json
new file mode 100644
index 0000000..7329147
--- /dev/null
+++ b/spec/asset/plugins/plugin/info.json
@@ -0,0 +1,6 @@
+{
+ "id": "test",
+ "title": "Test Plugin",
+ "placeholder": "placeholder",
+ "wait_seconds": 1
+}
diff --git a/spec/plugin_spec.cr b/spec/plugin_spec.cr
new file mode 100644
index 0000000..c0535ed
--- /dev/null
+++ b/spec/plugin_spec.cr
@@ -0,0 +1,70 @@
+require "./spec_helper"
+
+describe Plugin do
+ describe "helper functions" do
+ it "mango.text" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.text('Click Me');
+ JS
+ res.should eq "Click Me"
+ end
+ end
+
+ it "mango.text returns empty string when no text" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.text('');
+ JS
+ res.should eq ""
+ end
+ end
+
+ it "mango.css" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.css('', 'li.test');
+
+ JS
+ res.should eq ["A", "B"]
+ end
+ end
+
+ it "mango.css returns empty array when no match" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.css('', 'li.noclass');
+ JS
+ res.should eq [] of String
+ end
+ end
+
+ it "mango.attribute" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.attribute('Click Me', 'href');
+ JS
+ res.should eq "https://github.com"
+ end
+ end
+
+ it "mango.attribute returns undefined when no match" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.attribute('', 'href') === undefined;
+ JS
+ res.should be_true
+ end
+ end
+
+ # https://github.com/hkalexling/Mango/issues/320
+ it "mango.attribute handles tags in attribute values" do
+ with_plugin do |plugin|
+ res = plugin.eval <<-JS
+ mango.attribute('', 'data-b');
+ JS
+ res.should eq "test"
+ end
+ end
+ end
+end
diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr
index 1bbd287..dc8c2a3 100644
--- a/spec/spec_helper.cr
+++ b/spec/spec_helper.cr
@@ -3,6 +3,7 @@ require "../src/queue"
require "../src/server"
require "../src/config"
require "../src/main_fiber"
+require "../src/plugin/plugin"
class State
@@hash = {} of String => String
@@ -54,3 +55,10 @@ def with_storage
end
end
end
+
+def with_plugin
+ with_default_config do
+ plugin = Plugin.new "test", "spec/asset/plugins"
+ yield plugin
+ end
+end
diff --git a/src/plugin/plugin.cr b/src/plugin/plugin.cr
index 5175b3a..ef7c369 100644
--- a/src/plugin/plugin.cr
+++ b/src/plugin/plugin.cr
@@ -105,9 +105,10 @@ class Plugin
getter js_path = ""
getter storage_path = ""
- def self.build_info_ary
+ def self.build_info_ary(dir : String? = nil)
@@info_ary.clear
- dir = Config.current.plugin_path
+ dir ||= Config.current.plugin_path
+
Dir.mkdir_p dir unless Dir.exists? dir
Dir.each_child dir do |f|
@@ -160,8 +161,8 @@ class Plugin
list.save
end
- def initialize(id : String)
- Plugin.build_info_ary
+ def initialize(id : String, dir : String? = nil)
+ Plugin.build_info_ary dir
@info = @@info_ary.find &.id.== id
if @info.nil?
@@ -315,7 +316,7 @@ class Plugin
json
end
- private def eval(str)
+ def eval(str)
@rt.eval str
rescue e : Duktape::SyntaxError
raise SyntaxError.new e.message
@@ -435,9 +436,15 @@ class Plugin
env = Duktape::Sandbox.new ptr
html = env.require_string 0
- str = XML.parse(html).inner_text
+ begin
+ parser = Myhtml::Parser.new html
+ str = parser.body!.children.first.inner_text
+
+ env.push_string str
+ rescue
+ env.push_string ""
+ end
- env.push_string str
env.call_success
end
sbx.put_prop_string -2, "text"
@@ -448,8 +455,9 @@ class Plugin
name = env.require_string 1
begin
- attr = XML.parse(html).first_element_child.not_nil![name]
- env.push_string attr
+ parser = Myhtml::Parser.new html
+ attr = parser.body!.children.first.attribute_by name
+ env.push_string attr.not_nil!
rescue
env.push_undefined
end