mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
Merge pull request #321 from hkalexling/fix/plugin-use-html-parser
Use html parser in plugin helper functions
This commit is contained in:
commit
47af6ee284
@ -12,3 +12,4 @@ Layout/LineLength:
|
|||||||
MaxLength: 80
|
MaxLength: 80
|
||||||
Excluded:
|
Excluded:
|
||||||
- src/routes/api.cr
|
- src/routes/api.cr
|
||||||
|
- spec/plugin_spec.cr
|
||||||
|
0
spec/asset/plugins/plugin/index.js
Normal file
0
spec/asset/plugins/plugin/index.js
Normal file
6
spec/asset/plugins/plugin/info.json
Normal file
6
spec/asset/plugins/plugin/info.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"id": "test",
|
||||||
|
"title": "Test Plugin",
|
||||||
|
"placeholder": "placeholder",
|
||||||
|
"wait_seconds": 1
|
||||||
|
}
|
70
spec/plugin_spec.cr
Normal file
70
spec/plugin_spec.cr
Normal file
@ -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/server"
|
||||||
require "../src/config"
|
require "../src/config"
|
||||||
require "../src/main_fiber"
|
require "../src/main_fiber"
|
||||||
|
require "../src/plugin/plugin"
|
||||||
|
|
||||||
class State
|
class State
|
||||||
@@hash = {} of String => String
|
@@hash = {} of String => String
|
||||||
@ -54,3 +55,10 @@ def with_storage
|
|||||||
end
|
end
|
||||||
end
|
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 js_path = ""
|
||||||
getter storage_path = ""
|
getter storage_path = ""
|
||||||
|
|
||||||
def self.build_info_ary
|
def self.build_info_ary(dir : String? = nil)
|
||||||
@@info_ary.clear
|
@@info_ary.clear
|
||||||
dir = Config.current.plugin_path
|
dir ||= Config.current.plugin_path
|
||||||
|
|
||||||
Dir.mkdir_p dir unless Dir.exists? dir
|
Dir.mkdir_p dir unless Dir.exists? dir
|
||||||
|
|
||||||
Dir.each_child dir do |f|
|
Dir.each_child dir do |f|
|
||||||
@ -160,8 +161,8 @@ class Plugin
|
|||||||
list.save
|
list.save
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(id : String)
|
def initialize(id : String, dir : String? = nil)
|
||||||
Plugin.build_info_ary
|
Plugin.build_info_ary dir
|
||||||
|
|
||||||
@info = @@info_ary.find &.id.== id
|
@info = @@info_ary.find &.id.== id
|
||||||
if @info.nil?
|
if @info.nil?
|
||||||
@ -315,7 +316,7 @@ class Plugin
|
|||||||
json
|
json
|
||||||
end
|
end
|
||||||
|
|
||||||
private def eval(str)
|
def eval(str)
|
||||||
@rt.eval str
|
@rt.eval str
|
||||||
rescue e : Duktape::SyntaxError
|
rescue e : Duktape::SyntaxError
|
||||||
raise SyntaxError.new e.message
|
raise SyntaxError.new e.message
|
||||||
@ -435,9 +436,15 @@ class Plugin
|
|||||||
env = Duktape::Sandbox.new ptr
|
env = Duktape::Sandbox.new ptr
|
||||||
html = env.require_string 0
|
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
|
env.push_string str
|
||||||
|
rescue
|
||||||
|
env.push_string ""
|
||||||
|
end
|
||||||
|
|
||||||
env.call_success
|
env.call_success
|
||||||
end
|
end
|
||||||
sbx.put_prop_string -2, "text"
|
sbx.put_prop_string -2, "text"
|
||||||
@ -448,8 +455,9 @@ class Plugin
|
|||||||
name = env.require_string 1
|
name = env.require_string 1
|
||||||
|
|
||||||
begin
|
begin
|
||||||
attr = XML.parse(html).first_element_child.not_nil![name]
|
parser = Myhtml::Parser.new html
|
||||||
env.push_string attr
|
attr = parser.body!.children.first.attribute_by name
|
||||||
|
env.push_string attr.not_nil!
|
||||||
rescue
|
rescue
|
||||||
env.push_undefined
|
env.push_undefined
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user