Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use html parser in plugin helper functions #321

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Layout/LineLength:
MaxLength: 80
Excluded:
- src/routes/api.cr
- spec/plugin_spec.cr
Empty file.
6 changes: 6 additions & 0 deletions spec/asset/plugins/plugin/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "test",
"title": "Test Plugin",
"placeholder": "placeholder",
"wait_seconds": 1
}
70 changes: 70 additions & 0 deletions spec/plugin_spec.cr
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
26 changes: 17 additions & 9 deletions src/plugin/plugin.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down