deface 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/sandbox +2 -0
- data/bin/sandbox-setup +40 -0
- data/lib/deface/action_view_extensions.rb +16 -12
- data/lib/deface/applicator.rb +49 -37
- data/lib/deface/template_helper.rb +11 -8
- data/lib/deface/version.rb +1 -1
- data/spec/deface/action_view_template_spec.rb +31 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9060884c364b0a4472ddfc9bc76ddc213c67dbe6565eb62592facd1ab6b52183
|
4
|
+
data.tar.gz: dd5c46b0211214d0f2dd703bc5c5fe6f8dc880179050154414ac4ca83b11aa66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11be9726027369b1cdf3aee30d33f5919c9027a900f594569fc001eea61a5f510c714a17b450bbee10da95911dda1c7eb9f9d68adebc54c7461009103d66fd7d
|
7
|
+
data.tar.gz: 349650343bac57b3dbd94033f8cc65c237e1931d857a64e4a415cba6acfd7a03e19f9ef0a8998edd70179566464f573b4d69d3cba01f4d39b0bc072540971748
|
data/bin/sandbox
CHANGED
data/bin/sandbox-setup
CHANGED
@@ -8,6 +8,22 @@ system 'bin/rails db:migrate'
|
|
8
8
|
|
9
9
|
mkdir_p 'sandbox/app/overrides'
|
10
10
|
|
11
|
+
File.write 'sandbox/app/views/posts/_haml_partial.html.haml', <<~HAML
|
12
|
+
%h2 hello from haml
|
13
|
+
HAML
|
14
|
+
|
15
|
+
File.write 'sandbox/app/views/posts/_slim_partial.html.slim', <<~SLIM
|
16
|
+
h2 hello from slim
|
17
|
+
SLIM
|
18
|
+
|
19
|
+
File.write 'sandbox/app/views/posts/_haml_defaced_partial.html.haml', <<~HAML
|
20
|
+
%h3 hello from defaced haml
|
21
|
+
HAML
|
22
|
+
|
23
|
+
File.write 'sandbox/app/views/posts/_slim_defaced_partial.html.slim', <<~SLIM
|
24
|
+
h3 hello from defaced slim
|
25
|
+
SLIM
|
26
|
+
|
11
27
|
File.write 'sandbox/app/overrides/improved_posts.rb', <<~RUBY
|
12
28
|
Deface::Override.new(
|
13
29
|
virtual_path: "posts/show",
|
@@ -36,6 +52,30 @@ File.write 'sandbox/app/overrides/improved_posts.rb', <<~RUBY
|
|
36
52
|
replace: 'tr td:nth-child(2)',
|
37
53
|
text: "<td style='border:2px gray solid; padding: 1rem;'><%= post.body %></d>"
|
38
54
|
)
|
55
|
+
|
56
|
+
Deface::Override.new(
|
57
|
+
virtual_path: "posts/index",
|
58
|
+
name: "haml_and_slim_partials",
|
59
|
+
insert_before: 'table',
|
60
|
+
text: "
|
61
|
+
<header><%= render 'haml_partial' %><%= render 'slim_partial' %></header>
|
62
|
+
<section><%= render 'haml_defaced_partial' %><%= render 'slim_defaced_partial' %></section>
|
63
|
+
"
|
64
|
+
)
|
65
|
+
|
66
|
+
Deface::Override.new(
|
67
|
+
virtual_path: "posts/_haml_defaced_partial",
|
68
|
+
name: "haml_deface",
|
69
|
+
insert_before: 'h3',
|
70
|
+
text: "<h4>HAML subtitle</h4>"
|
71
|
+
)
|
72
|
+
|
73
|
+
Deface::Override.new(
|
74
|
+
virtual_path: "posts/_slim_defaced_partial",
|
75
|
+
name: "slim_deface",
|
76
|
+
insert_before: 'h3',
|
77
|
+
text: "<h4>SLIM subtitle</h4>"
|
78
|
+
)
|
39
79
|
RUBY
|
40
80
|
|
41
81
|
File.write 'sandbox/config/routes.rb', <<~RUBY
|
@@ -19,23 +19,27 @@ module Deface::ActionViewExtensions
|
|
19
19
|
|
20
20
|
# Before Rails 6 encode! returns nil
|
21
21
|
source = Deface.before_rails_6? ? (super; @source) : super
|
22
|
+
syntax = Deface::ActionViewExtensions.determine_syntax(@handler)
|
23
|
+
overrides = Deface::Override.find(
|
24
|
+
locals: @locals,
|
25
|
+
format: @format,
|
26
|
+
variant: @variant,
|
27
|
+
virtual_path: @virtual_path,
|
28
|
+
)
|
29
|
+
|
30
|
+
if syntax && overrides.any?
|
31
|
+
# Prevents any caching by rails in development mode.
|
32
|
+
@updated_at = Time.now if Deface.before_rails_6?
|
33
|
+
@handler = ActionView::Template::Handlers::ERB
|
22
34
|
|
23
|
-
if (syntax = Deface::ActionViewExtensions.determine_syntax(@handler))
|
24
35
|
# Modify the existing string instead of returning a copy
|
25
|
-
source.replace Deface::Override.
|
26
|
-
source,
|
27
|
-
|
28
|
-
format: @format,
|
29
|
-
variant: @variant,
|
30
|
-
virtual_path: @virtual_path,
|
31
|
-
},
|
32
|
-
true,
|
33
|
-
syntax
|
36
|
+
@source.replace Deface::Override.apply_overrides(
|
37
|
+
Deface::Override.convert_source(source, syntax: syntax),
|
38
|
+
overrides: overrides
|
34
39
|
)
|
35
|
-
@handler = ActionView::Template::Handlers::ERB
|
36
40
|
end
|
37
41
|
|
38
|
-
source
|
42
|
+
@source
|
39
43
|
end
|
40
44
|
|
41
45
|
private
|
data/lib/deface/applicator.rb
CHANGED
@@ -6,58 +6,70 @@ module Deface
|
|
6
6
|
def apply(source, details, log=true, syntax=:erb)
|
7
7
|
overrides = find(details)
|
8
8
|
|
9
|
-
|
10
|
-
Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'"
|
11
|
-
end
|
9
|
+
return source if overrides.empty?
|
12
10
|
|
13
|
-
|
14
|
-
case syntax
|
15
|
-
when :haml
|
16
|
-
#convert haml to erb before parsing before
|
17
|
-
source = Deface::HamlConverter.new(source.to_param).result
|
18
|
-
when :slim
|
19
|
-
source = Deface::SlimConverter.new(source.to_param).result
|
20
|
-
end
|
11
|
+
Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'" if log
|
21
12
|
|
22
|
-
|
13
|
+
apply_overrides(
|
14
|
+
convert_source(source, syntax: syntax),
|
15
|
+
overrides: overrides,
|
16
|
+
log: log
|
17
|
+
)
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
|
27
|
-
next
|
28
|
-
end
|
20
|
+
# applies specified overrides to given source
|
21
|
+
def apply_overrides(source, overrides:, log: true)
|
29
22
|
|
30
|
-
|
31
|
-
matches = override.matcher.matches(doc, log)
|
23
|
+
doc = Deface::Parser.convert(source)
|
32
24
|
|
33
|
-
|
34
|
-
|
25
|
+
overrides.each do |override|
|
26
|
+
if override.disabled?
|
27
|
+
Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
|
28
|
+
next
|
29
|
+
end
|
35
30
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
31
|
+
override.parsed_document = doc
|
32
|
+
matches = override.matcher.matches(doc, log)
|
33
|
+
|
34
|
+
if log
|
35
|
+
Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
override.
|
47
|
-
matches.each {|match| override.execute_action match }
|
37
|
+
# temporarily check and notify on use of old selector styles.
|
38
|
+
#
|
39
|
+
if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/)
|
40
|
+
Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>"
|
48
41
|
end
|
49
42
|
end
|
50
43
|
|
51
|
-
|
52
|
-
|
44
|
+
if matches.empty?
|
45
|
+
override.failure = "failed to match :#{override.action} selector '#{override.selector}'"
|
46
|
+
else
|
47
|
+
override.failure = nil
|
48
|
+
matches.each {|match| override.execute_action match }
|
49
|
+
end
|
50
|
+
end
|
53
51
|
|
54
|
-
|
52
|
+
source = doc.to_s
|
55
53
|
|
56
|
-
|
57
|
-
end
|
54
|
+
Deface::Parser.undo_erb_markup!(source)
|
58
55
|
|
59
56
|
source
|
60
57
|
end
|
58
|
+
|
59
|
+
# converts the source to a supported syntax (ERB)
|
60
|
+
def convert_source(source, syntax:)
|
61
|
+
# convert haml/slim to erb before parsing before
|
62
|
+
case syntax
|
63
|
+
when :erb
|
64
|
+
source
|
65
|
+
when :haml
|
66
|
+
Deface::HamlConverter.new(source.to_s).result
|
67
|
+
when :slim
|
68
|
+
Deface::SlimConverter.new(source.to_s).result
|
69
|
+
else
|
70
|
+
raise "unsupported syntax: #{syntax}"
|
71
|
+
end
|
72
|
+
end
|
61
73
|
end
|
62
74
|
|
63
75
|
def execute_action(target_element)
|
@@ -35,14 +35,17 @@ module Deface
|
|
35
35
|
original_enabled = Rails.application.config.deface.enabled
|
36
36
|
Rails.application.config.deface.enabled = apply_overrides
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
syntax = Deface::ActionViewExtensions.determine_syntax(view.handler)
|
39
|
+
overrides = Deface::Override.find(
|
40
|
+
locals: view.instance_variable_get(:@locals),
|
41
|
+
format: view.instance_variable_get(:@format),
|
42
|
+
variant: view.instance_variable_get(:@variant),
|
43
|
+
virtual_path: view.instance_variable_get(:@virtual_path),
|
44
|
+
)
|
45
|
+
|
46
|
+
if syntax && overrides.any?
|
47
|
+
source = Deface::Override.convert_source(source, syntax: syntax)
|
48
|
+
source = Deface::Override.apply_overrides(source, overrides: overrides)
|
46
49
|
end
|
47
50
|
ensure
|
48
51
|
Rails.application.config.deface.enabled = original_enabled
|
data/lib/deface/version.rb
CHANGED
@@ -46,7 +46,7 @@ describe Deface::ActionViewExtensions do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
describe "
|
49
|
+
describe "unsupported template" do
|
50
50
|
let(:source) { "xml.post => :blah" }
|
51
51
|
let(:path) { "/some/path/to/file.erb" }
|
52
52
|
let(:handler) { ActionView::Template::Handlers::Builder }
|
@@ -119,5 +119,35 @@ describe Deface::ActionViewExtensions do
|
|
119
119
|
|
120
120
|
expect(template.render(view, local_assigns)).to eq(%{"some <br> text"})
|
121
121
|
end
|
122
|
+
|
123
|
+
context 'with a haml template' do
|
124
|
+
let(:source) { "%p test\n= raw(text)\n" }
|
125
|
+
let(:handler) { Haml::Plugin }
|
126
|
+
|
127
|
+
it 'renders the template modified by deface using :remove' do
|
128
|
+
Deface::Override.new(
|
129
|
+
virtual_path: virtual_path,
|
130
|
+
name: "Posts#index",
|
131
|
+
remove: "p",
|
132
|
+
)
|
133
|
+
|
134
|
+
expect(template.render(view, local_assigns)).to eq(%{\nsome <br> text})
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'with a slim template' do
|
139
|
+
let(:source) { "p test\n= raw(text)\n" }
|
140
|
+
let(:handler) { Slim::RailsTemplate.new }
|
141
|
+
|
142
|
+
it 'renders the template modified by deface using :remove' do
|
143
|
+
Deface::Override.new(
|
144
|
+
virtual_path: virtual_path,
|
145
|
+
name: "Posts#index",
|
146
|
+
remove: "p",
|
147
|
+
)
|
148
|
+
|
149
|
+
expect(template.render(view, local_assigns)).to eq(%{\nsome <br> text\n})
|
150
|
+
end
|
151
|
+
end
|
122
152
|
end
|
123
153
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian D Quinn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|