android-xml 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74fdc8cf167a00e6fc913368b39b225d0d4d0c1c
4
- data.tar.gz: e50d23184c0a996f2b5d9557d468e97d4e7dd001
3
+ metadata.gz: 4509fa336291a4bd13e096f37d9b23f8b745bd07
4
+ data.tar.gz: 6ddf6a6eba7193d428d49c7c6a7dfe2f8f802596
5
5
  SHA512:
6
- metadata.gz: 3cf9021c429ed0baa13e54e82f4c7f5c973b2ea5954da51a88222d3f8f0151d764593a913443350937a67631c9ff2306ee6d2c8ff554594921741709afddddc9
7
- data.tar.gz: 5230c0bb21226467546661037a8899016fb87fde53b752798ea9462760c5fcda8d72c7ead82d01d6b413188dc513b150a33f18ad9fbfc59861edb8d23bbe7aba
6
+ metadata.gz: 0d50edfc8f5555fe8a8bc8b6c68c80ea3a3c2ad717ddebd90e0d69ccde953d8fff36b44e718facb895d938a8e120de9c64a8e5629ff4c9c448c7f9d4896c4bf2
7
+ data.tar.gz: f655177e4243493085c021fe585abd379b706b08ce5f8b6dd9b56d94ebdd7bc09f0867f92fef286d54879208563bd336fed31ed13c97500d20ad78dc6742456c
@@ -4,6 +4,14 @@ class Numeric
4
4
  "#{self}pt"
5
5
  end
6
6
 
7
+ def mm
8
+ "#{self}mm"
9
+ end
10
+
11
+ def in
12
+ "#{self}in"
13
+ end
14
+
7
15
  def px
8
16
  "#{self}px"
9
17
  end
@@ -12,4 +20,12 @@ class Numeric
12
20
  "#{self}dp"
13
21
  end
14
22
 
23
+ def dip
24
+ "#{self}dip"
25
+ end
26
+
27
+ def sp
28
+ "#{self}sp"
29
+ end
30
+
15
31
  end
@@ -26,6 +26,30 @@ module AndroidXml
26
26
  tag
27
27
  end
28
28
 
29
+ def run_block(&block)
30
+ text = instance_exec(&block)
31
+ if text.is_a?(String)
32
+ text = Tag.format_string(text)
33
+ end
34
+ text
35
+ end
36
+
37
+ # isn't there a method that does this for me!?
38
+ def self.format_string(text)
39
+ text = text.dup
40
+ {
41
+ '\\' => '\\\\',
42
+ "\n" => '\n',
43
+ "\t" => '\t',
44
+ "\b" => '\b',
45
+ '"' => '\"',
46
+ "'" => "\\\\'",
47
+ }.each do |find, replace|
48
+ text.gsub!(find, replace)
49
+ end
50
+ text
51
+ end
52
+
29
53
  end
30
54
 
31
55
  module_function
@@ -34,7 +58,7 @@ module AndroidXml
34
58
  diff = Tag.found_strings - Tag.registered_strings
35
59
  unless diff.empty?
36
60
  diff.each do |name|
37
- puts " string(name='#{name}') { '#{name}' }"
61
+ puts " string(name='#{Tag.format_string(name)}') { '#{Tag.format_string(name)}' }"
38
62
  end
39
63
  end
40
64
  end
@@ -50,15 +50,18 @@ module AndroidXml
50
50
  attrs.merge!(Setup.tags[@raw_tag][:defaults])
51
51
  attrs.merge!(@attrs)
52
52
 
53
+ format_attrs(@tag, attrs, whitespace)
54
+ end
55
+
56
+ def format_attrs(tag, attrs, whitespace, is_first=true)
53
57
  output = ''
54
- is_first = true
55
58
  attrs.each do |key, value|
56
59
  next if value.nil?
57
60
 
58
61
  key = key.to_s
59
62
 
60
- if Setup.tags[@tag][:attrs].key?(key)
61
- xml_key = Setup.tags[@tag][:attrs][key]
63
+ if Setup.tags[tag][:attrs].key?(key)
64
+ xml_key = Setup.tags[tag][:attrs][key]
62
65
  elsif Setup.all_tag[:attrs].key?(key)
63
66
  xml_key = Setup.all_tag[:attrs][key]
64
67
  elsif key.to_s.include?(':')
@@ -71,16 +74,32 @@ module AndroidXml
71
74
  Tag.found_strings << $1
72
75
  end
73
76
 
77
+ quoted_value = Tag.quote_attr_value(value)
74
78
  if is_first
75
- output << " #{xml_key}=\"#{value}\""
79
+ output << " #{xml_key}=\"#{quoted_value}\""
76
80
  is_first = false
77
81
  else
78
- output << "\n#{whitespace}#{xml_key}=\"#{value}\""
82
+ output << "\n#{whitespace}#{xml_key}=\"#{quoted_value}\""
79
83
  end
80
84
  end
85
+
81
86
  output
82
87
  end
83
88
 
89
+ def self.quote_attr_value(value)
90
+ value = value.to_s.dup
91
+ {
92
+ '&' => '&amp;',
93
+ '\'' => '&apos;',
94
+ '"' => '&quot;',
95
+ '<' => '&lt;',
96
+ '>' => '&gt;',
97
+ }.each do |find, replace|
98
+ value.gsub!(find, replace)
99
+ end
100
+ value
101
+ end
102
+
84
103
  def generate(tab='')
85
104
  whitespace = "#{tab} #{' ' * @tag.length}"
86
105
  output = "#{tab}<#{@tag}#{attrs(whitespace)}"
@@ -108,7 +127,7 @@ module AndroidXml
108
127
 
109
128
  output = ''
110
129
  if @generate
111
- text = instance_exec(&@generate)
130
+ text = run_block(&@generate)
112
131
  @buffer.each do |tag|
113
132
  output << tag.generate(tab)
114
133
  end
@@ -124,6 +143,10 @@ module AndroidXml
124
143
  @block_output = output
125
144
  end
126
145
 
146
+ def run_block(&block)
147
+ instance_exec(&block)
148
+ end
149
+
127
150
  def to_s
128
151
  generate
129
152
  end
@@ -1,3 +1,3 @@
1
1
  module AndroidXml
2
- Version = '1.2.3'
2
+ Version = '1.2.4'
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'android-xml'
2
+
3
+
4
+ describe 'tag attributes' do
5
+
6
+ before do
7
+ AndroidXml.reset
8
+ end
9
+
10
+ it 'should generate <any-tag tag="value" />' do
11
+ xml = AndroidXml.file('tmp/test.xml') do
12
+ any_tag(tag: "value")
13
+ end
14
+
15
+ expect(xml.to_s).to match %r{<any-tag android:tag="value" />}
16
+ end
17
+
18
+ it 'should generate <any-tag tag="value-with-quotes" />' do
19
+ xml = AndroidXml.file('tmp/test.xml') do
20
+ any_tag(tag: %q{value-"'<>&-with-quoted})
21
+ end
22
+
23
+ expect(xml.to_s).to match %r{<any-tag android:tag="value-&quot;&apos;&lt;&gt;&amp;-with-quoted" />}
24
+ end
25
+
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'android-xml'
2
+
3
+
4
+ describe AndroidXml do
5
+
6
+ before do
7
+ AndroidXml.reset
8
+ end
9
+
10
+ it 'should quote strings' do
11
+ xml = AndroidXml.file('tmp/test.xml') do
12
+ string(name: 'registered_string') { "newline \nsingle-quote ' double-quote \" tab\t"}
13
+ end
14
+
15
+ expect(xml.to_s).to include('newline \\nsingle-quote \\\' double-quote \\" tab\\t')
16
+ end
17
+
18
+ it 'should register strings' do
19
+ xml = AndroidXml.file('tmp/test.xml') do
20
+ string(name: 'registered_string') { 'registered'}
21
+ end
22
+
23
+ xml.generate
24
+ expect(AndroidXml::Tag.registered_strings).to eql(['registered_string'])
25
+ end
26
+
27
+ it 'should find strings' do
28
+ xml = AndroidXml.file('tmp/test.xml') do
29
+ any_tag(tag: '@string/found_string')
30
+ end
31
+
32
+ xml.generate
33
+ expect(AndroidXml::Tag.found_strings).to eql(['found_string'])
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: android-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin T.A. Gray
@@ -47,11 +47,13 @@ files:
47
47
  - spec/android_manifest_spec.rb
48
48
  - spec/android_menu_spec.rb
49
49
  - spec/android_resources_spec.rb
50
+ - spec/attrs_spec.rb
50
51
  - spec/clone_tag_spec.rb
51
52
  - spec/create_tag_spec.rb
52
53
  - spec/factory_spec.rb
53
54
  - spec/generate_spec.rb
54
55
  - spec/inc_tag_spec.rb
56
+ - spec/string_spec.rb
55
57
  - spec/write_to_file_spec.rb
56
58
  homepage: https://github.com/colinta/android-xml
57
59
  licenses:
@@ -81,9 +83,11 @@ test_files:
81
83
  - spec/android_manifest_spec.rb
82
84
  - spec/android_menu_spec.rb
83
85
  - spec/android_resources_spec.rb
86
+ - spec/attrs_spec.rb
84
87
  - spec/clone_tag_spec.rb
85
88
  - spec/create_tag_spec.rb
86
89
  - spec/factory_spec.rb
87
90
  - spec/generate_spec.rb
88
91
  - spec/inc_tag_spec.rb
92
+ - spec/string_spec.rb
89
93
  - spec/write_to_file_spec.rb