free_dom 0.1.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2955fd654294f2fd1eb821d23dfcda69a78caa02176ca5d32663383b25ad9c1a
4
- data.tar.gz: 4aaa3a36bfb059c1e77fc9d86c06513d10b67d6283dc8157c720168fe6ab5605
3
+ metadata.gz: d2d526647a894ced51a3730b7f1d9c0d92616faf408097e8f333350cd74fb49d
4
+ data.tar.gz: d5474c9bbe9fa6489b546a79fd403a68eb2db4ca094f4867ec7c84c347a2a421
5
5
  SHA512:
6
- metadata.gz: 18c25f4563a48e85a570328e5e8147958eef9634a8d3f7465b6eabd709e77bfd49be1668013e1608a01326c9fa0c9c539f5668cd88b2b47cff8ac4285e088e41
7
- data.tar.gz: 25d1f4e5cde908569da5d20214495c8580a919ce2d22f0b9726fe00ec4efad7f87e57abd91f88b5726f6e891a97d6d8804129c2c05bd835fedd9174d43433b30
6
+ metadata.gz: 8fa72885700f6945a1fd0d44f4a95b2e75beb1ddfcbdcfc1fa27a6e0b738fce41e18126b5cdf3d8cce58b4cfef27cf585e2b3a8b9b641119ac3f101539f19892
7
+ data.tar.gz: 94fbc713022b98dc884d00809cfdbc515200f9adf518a60d497244c8507d31bb2c206a562bea7dbc21da66add5b312c70578e05eb7032e570dd4591d45ea7c7c
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -3,28 +3,84 @@
3
3
  # file: free_dom.rb
4
4
 
5
5
  require 'domle'
6
+ require 'xml_to_sliml'
6
7
 
7
8
 
9
+ class Rexle::Element
10
+ def to_sliml()
11
+
12
+ xml = self.xml(pretty: true)
13
+ XmlToSliml.new(xml.gsub(/ style=''/,"")).to_s
14
+
15
+ end
16
+ end
17
+
8
18
  class FreeDom < Domle
9
19
 
10
- def initialize(xml)
20
+ def initialize(s, debug: false)
11
21
 
12
- doc = Rexle.new xml
22
+ @debug = debug
23
+
24
+ xml = s =~ /^</ ? s : LineTree.new(s).to_xml
25
+ @doc = Rexle.new(xml)
13
26
 
14
27
  h = {}
15
28
 
16
- doc.root.each_recursive do |e|
29
+ @doc.root.each_recursive do |e|
30
+
17
31
  h[e.name.to_sym] ||= {}
32
+
33
+ # if there's a custom attribute, add a default trigger called trigger_change
34
+ a = e.attributes.keys.reject {|x| %i(id name class style).include? x }
35
+ e.attributes.merge!({trigger_change: a.first}) if a.any?
36
+
37
+ # add a trigger attribute for each *on* event attribute
38
+ events = e.attributes.keys.select {|x| x =~ /^on/}\
39
+ .map {|x| 'trigger_' + x.to_s[/(?<=^on)\w+/]}
40
+ e.attributes.merge! events.zip(['']*events.length).to_h
41
+
18
42
  h[e.name.to_sym].merge!(e.attributes)
19
43
  end
20
44
 
21
45
  @defined_elements = {}
22
46
 
23
- h.each do |name, attributes|
47
+ h.each do |name, attributelist|
24
48
 
25
49
  klass = Class.new(Domle::Element) do
26
50
 
27
- attr2_accessor *attributes.keys
51
+ a = attributelist.keys
52
+
53
+ triggers = a.select {|x| x =~ /^trigger_/ }
54
+ attr2_accessor *((a - triggers) + %i(onchange)).uniq
55
+
56
+ triggers.each do |x|
57
+
58
+ trigger = x.to_s[/(?<=^trigger_).*/].to_sym
59
+ puts 'trigger: ' + trigger.inspect if @debug
60
+
61
+ define_method(trigger) do
62
+ statement = method(('on' + trigger.to_s).to_sym).call
63
+ eval statement, $env if statement
64
+ end
65
+
66
+ if trigger == :change then
67
+
68
+ attribute = attributelist[x].to_sym
69
+
70
+ define_method((attribute.to_s + '=').to_sym) do |val|
71
+
72
+ oldval = attributes[attribute]
73
+ attributes[attribute] = val
74
+
75
+ @rexle.refresh if @rexle
76
+ change() unless val == oldval
77
+
78
+ val
79
+ end
80
+
81
+ end
82
+
83
+ end
28
84
 
29
85
  end
30
86
 
@@ -32,9 +88,39 @@ class FreeDom < Domle
32
88
  @defined_elements.merge!({name => custom_class})
33
89
 
34
90
  end
91
+
92
+ # remove the trigger declaration attributes
93
+ @doc.root.each_recursive do |e|
94
+ e.attributes.keys.select {|x| x =~ /^trigger_/ }\
95
+ .each {|x| e.attributes.delete x }
96
+ end
35
97
 
36
- super(xml)
98
+ super(@doc.root.xml, debug: @debug)
99
+ script()
100
+
37
101
  end
102
+
103
+
104
+ # used within the scope of the script tags
105
+ #
106
+ def doc()
107
+ self
108
+ end
109
+
110
+ def script()
111
+ s = @doc.root.xpath('//script').map {|x| x.text.unescape }.join
112
+ eval s
113
+ $env = binding
114
+ end
115
+
116
+ def to_sliml()
117
+
118
+ xml = @doc.root.element('*').xml(pretty: true)
119
+ puts 'xml: ' + xml.inspect if @debug
120
+ XmlToSliml.new(xml.gsub(/ style=''/,"")).to_s
121
+
122
+ end
123
+
38
124
 
39
125
  private
40
126
 
@@ -44,4 +130,3 @@ class FreeDom < Domle
44
130
 
45
131
  end
46
132
 
47
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: free_dom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  VxSuuHhAXplX7wWmd1FzAAH5bk4OPlKGt5fxQbax+FSoEzTsnoDcLTY2i0WOxwRo
36
36
  76nAi+UZoU87S6brj88/rtOL
37
37
  -----END CERTIFICATE-----
38
- date: 2020-04-29 00:00:00.000000000 Z
38
+ date: 2020-05-02 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: domle
@@ -43,20 +43,40 @@ dependencies:
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0.3'
46
+ version: '0.4'
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 0.3.1
49
+ version: 0.4.1
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
- version: '0.3'
56
+ version: '0.4'
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 0.3.1
59
+ version: 0.4.1
60
+ - !ruby/object:Gem::Dependency
61
+ name: xml_to_sliml
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '0.1'
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 0.1.2
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.1'
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.1.2
60
80
  description:
61
81
  email: james@jamesrobertson.eu
62
82
  executables: []
metadata.gz.sig CHANGED
Binary file