mirror of https://github.com/getmango/Mango.git
Use `myhtml` in plugin helper and add tests (#320)
This commit is contained in:
parent
5daeac72cb
commit
75a30a88e0
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"id": "test",
|
||||
"title": "Test Plugin",
|
||||
"placeholder": "placeholder",
|
||||
"wait_seconds": 1
|
||||
}
|
|
@ -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('<a href="https://github.com">Click Me<a>');
|
||||
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('<img src="https://github.com" />');
|
||||
JS
|
||||
res.should eq ""
|
||||
end
|
||||
end
|
||||
|
||||
it "mango.css" do
|
||||
with_plugin do |plugin|
|
||||
res = plugin.eval <<-JS
|
||||
mango.css('<ul><li class="test">A</li><li class="test">B</li><li>C</li></ul>', 'li.test');
|
||||
|
||||
JS
|
||||
res.should eq ["<li class=\"test\">A</li>", "<li class=\"test\">B</li>"]
|
||||
end
|
||||
end
|
||||
|
||||
it "mango.css returns empty array when no match" do
|
||||
with_plugin do |plugin|
|
||||
res = plugin.eval <<-JS
|
||||
mango.css('<ul><li class="test">A</li><li class="test">B</li><li>C</li></ul>', 'li.noclass');
|
||||
JS
|
||||
res.should eq [] of String
|
||||
end
|
||||
end
|
||||
|
||||
it "mango.attribute" do
|
||||
with_plugin do |plugin|
|
||||
res = plugin.eval <<-JS
|
||||
mango.attribute('<a href="https://github.com">Click Me<a>', '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('<div />', '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('<div data-a="<img />" data-b="test" />', 'data-b');
|
||||
JS
|
||||
res.should eq "test"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue