dominate 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dominate.rb +3 -3
- data/lib/dominate/dom.rb +28 -15
- data/lib/dominate/scope.rb +0 -1
- data/lib/dominate/version.rb +1 -1
- data/test/dominate_test.rb +8 -0
- data/test/dummy/app.slim +2 -1
- data/test/dummy/index.html +30 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93f5b699571918d5a7f4a599d84f7d324ff88ef1
|
4
|
+
data.tar.gz: a3bdcd9fd5ac1dd6145888d03958ebd4334166f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 543d051c51e00fd274bc4cc4c331737aa22dc26889963e63763fde5ac7c9ba3609d2e8bb02ead32275b187c5954d8732ede7ad2d3b42de469f1d00cde6d23a7a
|
7
|
+
data.tar.gz: c7809e76967b223dc8f1d0fb78ae2ec2b2178e3cce14ca6962606ece132feafaa4a732f4624cfa9e5a23e81b4eff77592505d29c07086378ed3000fc77df852b
|
data/lib/dominate.rb
CHANGED
@@ -8,10 +8,10 @@ require "dominate/dom"
|
|
8
8
|
module Dominate
|
9
9
|
extend self
|
10
10
|
|
11
|
-
attr_accessor :config, :reset_config
|
11
|
+
attr_accessor :config, :reset_config
|
12
12
|
|
13
|
-
def HTML html, data = {}
|
14
|
-
Dom.new html, data
|
13
|
+
def HTML html, instance = false, data = {}
|
14
|
+
Dom.new html, instance, data
|
15
15
|
end
|
16
16
|
|
17
17
|
def setup
|
data/lib/dominate/dom.rb
CHANGED
@@ -1,33 +1,31 @@
|
|
1
1
|
module Dominate
|
2
2
|
class Dom
|
3
|
-
attr_accessor :raw_html, :instance, :doc
|
3
|
+
attr_accessor :raw_html, :instance, :options, :config, :doc
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
YIELD_REGEX = /<!--(?:\s*|)@yield(?:\s*|)-->/
|
6
|
+
PARTIAL_REGEX = /<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/
|
7
|
+
PARTIAL_REGEX_WITHIN = /<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->(.*?)<!--(?:\s*|)\/partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/m
|
8
|
+
VIEW_TYPES = %w(html slim haml erb md markdown mkd mab)
|
8
9
|
|
9
|
-
def initialize raw_html, instance = false
|
10
|
+
def initialize raw_html, instance = false, config = {}
|
10
11
|
@raw_html = raw_html
|
11
12
|
@instance = instance
|
13
|
+
@config = (Dominate.config.to_h.merge config).to_deep_ostruct
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
else
|
16
|
-
# need to wrap it in a div to make sure it has a root
|
17
|
-
@doc = Nokogiri::HTML.fragment "<div>#{raw_html}</div>"
|
18
|
-
end
|
19
|
-
|
20
|
-
load_partials if Dominate.config.view_path
|
15
|
+
set_doc raw_html
|
16
|
+
load_html if Dominate.config.view_path
|
21
17
|
end
|
22
18
|
|
23
|
-
def
|
19
|
+
def load_html
|
20
|
+
load_layout if config.layout
|
21
|
+
|
24
22
|
updated_html = doc.inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m|
|
25
23
|
match = m.strip.match(PARTIAL_REGEX)
|
26
24
|
partial = match[1]
|
27
25
|
load_view "#{view_path}/#{partial}"
|
28
26
|
end
|
29
27
|
|
30
|
-
|
28
|
+
set_doc updated_html if updated_html
|
31
29
|
|
32
30
|
doc.traverse do |e|
|
33
31
|
if match = e.to_html.strip.match(PARTIAL_REGEX)
|
@@ -39,6 +37,12 @@ module Dominate
|
|
39
37
|
end
|
40
38
|
end
|
41
39
|
|
40
|
+
def load_layout
|
41
|
+
html = load_view config.layout
|
42
|
+
inner_html = doc.inner_html
|
43
|
+
set_doc html.gsub YIELD_REGEX, inner_html
|
44
|
+
end
|
45
|
+
|
42
46
|
def scope name
|
43
47
|
reset_html
|
44
48
|
Scope.new instance, doc.search("[data-scope='#{name}']")
|
@@ -58,6 +62,15 @@ module Dominate
|
|
58
62
|
|
59
63
|
private
|
60
64
|
|
65
|
+
def set_doc html
|
66
|
+
if html.match(/<html.*>/)
|
67
|
+
@doc = Nokogiri::HTML::Document.parse html
|
68
|
+
else
|
69
|
+
# need to wrap it in a div to make sure it has a root
|
70
|
+
@doc = Nokogiri::HTML.fragment html
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
61
74
|
def reset_html
|
62
75
|
@html = false
|
63
76
|
end
|
data/lib/dominate/scope.rb
CHANGED
data/lib/dominate/version.rb
CHANGED
data/test/dominate_test.rb
CHANGED
@@ -6,6 +6,7 @@ setup do
|
|
6
6
|
Dominate.reset_config!
|
7
7
|
Dominate.setup do |c|
|
8
8
|
c.view_path = './test/dummy'
|
9
|
+
c.layout = "#{c.view_path}/app"
|
9
10
|
end
|
10
11
|
|
11
12
|
inline_html = File.read './test/dummy/index.html'
|
@@ -80,4 +81,11 @@ scope 'dominate' do
|
|
80
81
|
assert a.dom.html['Test Company']
|
81
82
|
assert a.dom.html['This should not show'] == nil
|
82
83
|
end
|
84
|
+
|
85
|
+
test 'layout' do |a|
|
86
|
+
assert a.dom.html['<head>'] == nil
|
87
|
+
assert a.dom.html['body']
|
88
|
+
assert a.dom.html['app layout']
|
89
|
+
assert a.dom.html.scan(/body/).length == 2
|
90
|
+
end
|
83
91
|
end
|
data/test/dummy/app.slim
CHANGED
data/test/dummy/index.html
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
-
|
2
|
-
<
|
3
|
-
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
</head>
|
4
|
+
<body>
|
5
|
+
<!-- @partial header -->
|
6
|
+
<span>This should not show</span>
|
7
|
+
<!-- /partial header -->
|
4
8
|
|
5
|
-
<div>
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
</div>
|
12
|
-
<ul data-scope="list">
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
</ul>
|
9
|
+
<div>
|
10
|
+
<h1 data-scope='admin_only'>Admin</h1>
|
11
|
+
<span>current_user:
|
12
|
+
<span data-instance="current_user.first_name">John</span>
|
13
|
+
<span data-instance="current_user.last_name">Doe</span>
|
14
|
+
</span>
|
15
|
+
</div>
|
16
|
+
<ul data-scope="list">
|
17
|
+
<li>
|
18
|
+
<a href="#" data-prop="todo">test</a>
|
19
|
+
</li>
|
20
|
+
<li>
|
21
|
+
<a href="#" data-prop="todo">testing</a>
|
22
|
+
</li>
|
23
|
+
<li>
|
24
|
+
<ul data-scope="list-nested">
|
25
|
+
<li data-prop="todo"></li>
|
26
|
+
</ul>
|
27
|
+
</li>
|
28
|
+
</ul>
|
25
29
|
|
26
|
-
<!-- @partial footer -->
|
30
|
+
<!-- @partial footer -->
|
31
|
+
</body>
|
32
|
+
</html>
|