diamonds 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +36 -0
  3. data/lib/diamonds.rb +74 -0
  4. data/lib/diamonds/Gemfile +28 -0
  5. data/lib/diamonds/README.md +254 -0
  6. data/lib/diamonds/Rakefile +5 -0
  7. data/lib/diamonds/apps/application.rb +15 -0
  8. data/lib/diamonds/apps/library.rb +321 -0
  9. data/lib/diamonds/apps/user_app.rb +5 -0
  10. data/lib/diamonds/config.ru +98 -0
  11. data/lib/diamonds/db/mysiteDB Back.sqlite3 +0 -0
  12. data/lib/diamonds/db/mysiteDB.sqlite3 +0 -0
  13. data/lib/diamonds/gems/auto_launch_browser.rb +22 -0
  14. data/lib/diamonds/gems/io.rb +146 -0
  15. data/lib/diamonds/gems/ruby_to_js.rb +173 -0
  16. data/lib/diamonds/gems/server_utils.rb +96 -0
  17. data/lib/diamonds/lib/opal-browser.rb +1 -0
  18. data/lib/diamonds/lib/opal/browser.rb +4 -0
  19. data/lib/diamonds/opal-browser.gemspec +21 -0
  20. data/lib/diamonds/opal/browser.rb +11 -0
  21. data/lib/diamonds/opal/browser/animation_frame.rb +111 -0
  22. data/lib/diamonds/opal/browser/canvas.rb +321 -0
  23. data/lib/diamonds/opal/browser/canvas/data.rb +63 -0
  24. data/lib/diamonds/opal/browser/canvas/gradient.rb +27 -0
  25. data/lib/diamonds/opal/browser/canvas/style.rb +113 -0
  26. data/lib/diamonds/opal/browser/canvas/text.rb +45 -0
  27. data/lib/diamonds/opal/browser/console.rb +103 -0
  28. data/lib/diamonds/opal/browser/cookies.rb +133 -0
  29. data/lib/diamonds/opal/browser/css.rb +24 -0
  30. data/lib/diamonds/opal/browser/css/declaration.rb +83 -0
  31. data/lib/diamonds/opal/browser/css/rule.rb +48 -0
  32. data/lib/diamonds/opal/browser/css/rule/style.rb +16 -0
  33. data/lib/diamonds/opal/browser/css/style_sheet.rb +83 -0
  34. data/lib/diamonds/opal/browser/css/unit.rb +188 -0
  35. data/lib/diamonds/opal/browser/database/sql.rb +194 -0
  36. data/lib/diamonds/opal/browser/delay.rb +78 -0
  37. data/lib/diamonds/opal/browser/dom.rb +84 -0
  38. data/lib/diamonds/opal/browser/dom/attribute.rb +26 -0
  39. data/lib/diamonds/opal/browser/dom/builder.rb +88 -0
  40. data/lib/diamonds/opal/browser/dom/cdata.rb +9 -0
  41. data/lib/diamonds/opal/browser/dom/character_data.rb +73 -0
  42. data/lib/diamonds/opal/browser/dom/comment.rb +9 -0
  43. data/lib/diamonds/opal/browser/dom/document.rb +149 -0
  44. data/lib/diamonds/opal/browser/dom/document_fragment.rb +7 -0
  45. data/lib/diamonds/opal/browser/dom/element.rb +474 -0
  46. data/lib/diamonds/opal/browser/dom/element/attributes.rb +87 -0
  47. data/lib/diamonds/opal/browser/dom/element/data.rb +67 -0
  48. data/lib/diamonds/opal/browser/dom/element/image.rb +23 -0
  49. data/lib/diamonds/opal/browser/dom/element/input.rb +28 -0
  50. data/lib/diamonds/opal/browser/dom/element/offset.rb +89 -0
  51. data/lib/diamonds/opal/browser/dom/element/position.rb +46 -0
  52. data/lib/diamonds/opal/browser/dom/element/scroll.rb +136 -0
  53. data/lib/diamonds/opal/browser/dom/element/select.rb +36 -0
  54. data/lib/diamonds/opal/browser/dom/element/size.rb +34 -0
  55. data/lib/diamonds/opal/browser/dom/element/template.rb +9 -0
  56. data/lib/diamonds/opal/browser/dom/element/textarea.rb +24 -0
  57. data/lib/diamonds/opal/browser/dom/mutation_observer.rb +178 -0
  58. data/lib/diamonds/opal/browser/dom/node.rb +464 -0
  59. data/lib/diamonds/opal/browser/dom/node_set.rb +110 -0
  60. data/lib/diamonds/opal/browser/dom/text.rb +36 -0
  61. data/lib/diamonds/opal/browser/effects.rb +50 -0
  62. data/lib/diamonds/opal/browser/event.rb +253 -0
  63. data/lib/diamonds/opal/browser/event/animation.rb +38 -0
  64. data/lib/diamonds/opal/browser/event/audio_processing.rb +33 -0
  65. data/lib/diamonds/opal/browser/event/base.rb +430 -0
  66. data/lib/diamonds/opal/browser/event/before_unload.rb +15 -0
  67. data/lib/diamonds/opal/browser/event/clipboard.rb +28 -0
  68. data/lib/diamonds/opal/browser/event/close.rb +47 -0
  69. data/lib/diamonds/opal/browser/event/composition.rb +50 -0
  70. data/lib/diamonds/opal/browser/event/custom.rb +65 -0
  71. data/lib/diamonds/opal/browser/event/device_light.rb +23 -0
  72. data/lib/diamonds/opal/browser/event/device_motion.rb +51 -0
  73. data/lib/diamonds/opal/browser/event/device_orientation.rb +48 -0
  74. data/lib/diamonds/opal/browser/event/device_proximity.rb +33 -0
  75. data/lib/diamonds/opal/browser/event/drag.rb +119 -0
  76. data/lib/diamonds/opal/browser/event/focus.rb +39 -0
  77. data/lib/diamonds/opal/browser/event/gamepad.rb +60 -0
  78. data/lib/diamonds/opal/browser/event/hash_change.rb +28 -0
  79. data/lib/diamonds/opal/browser/event/keyboard.rb +115 -0
  80. data/lib/diamonds/opal/browser/event/message.rb +70 -0
  81. data/lib/diamonds/opal/browser/event/mouse.rb +254 -0
  82. data/lib/diamonds/opal/browser/event/page_transition.rb +23 -0
  83. data/lib/diamonds/opal/browser/event/pop_state.rb +33 -0
  84. data/lib/diamonds/opal/browser/event/progress.rb +43 -0
  85. data/lib/diamonds/opal/browser/event/sensor.rb +15 -0
  86. data/lib/diamonds/opal/browser/event/storage.rb +43 -0
  87. data/lib/diamonds/opal/browser/event/touch.rb +60 -0
  88. data/lib/diamonds/opal/browser/event/ui.rb +38 -0
  89. data/lib/diamonds/opal/browser/event/wheel.rb +49 -0
  90. data/lib/diamonds/opal/browser/event_source.rb +70 -0
  91. data/lib/diamonds/opal/browser/history.rb +90 -0
  92. data/lib/diamonds/opal/browser/http.rb +169 -0
  93. data/lib/diamonds/opal/browser/http/binary.rb +58 -0
  94. data/lib/diamonds/opal/browser/http/headers.rb +109 -0
  95. data/lib/diamonds/opal/browser/http/request.rb +344 -0
  96. data/lib/diamonds/opal/browser/http/response.rb +115 -0
  97. data/lib/diamonds/opal/browser/immediate.rb +163 -0
  98. data/lib/diamonds/opal/browser/interval.rb +111 -0
  99. data/lib/diamonds/opal/browser/location.rb +87 -0
  100. data/lib/diamonds/opal/browser/navigator.rb +173 -0
  101. data/lib/diamonds/opal/browser/screen.rb +66 -0
  102. data/lib/diamonds/opal/browser/socket.rb +123 -0
  103. data/lib/diamonds/opal/browser/storage.rb +252 -0
  104. data/lib/diamonds/opal/browser/support.rb +287 -0
  105. data/lib/diamonds/opal/browser/utils.rb +74 -0
  106. data/lib/diamonds/opal/browser/version.rb +3 -0
  107. data/lib/diamonds/opal/browser/window.rb +120 -0
  108. data/lib/diamonds/opal/browser/window/scroll.rb +59 -0
  109. data/lib/diamonds/opal/browser/window/size.rb +59 -0
  110. data/lib/diamonds/opal/browser/window/view.rb +36 -0
  111. data/lib/diamonds/opal/opal-browser.rb +1 -0
  112. data/lib/diamonds/public/__OPAL_SOURCE_MAPS__/application.map +0 -0
  113. data/lib/diamonds/public/javascripts/font-awesome/HELP-US-OUT.txt +7 -0
  114. data/lib/diamonds/public/javascripts/font-awesome/css/font-awesome.css +2026 -0
  115. data/lib/diamonds/public/javascripts/font-awesome/css/font-awesome.min.css +1 -0
  116. data/lib/diamonds/public/javascripts/font-awesome/fonts/FontAwesome.otf +0 -0
  117. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.eot +0 -0
  118. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.svg +640 -0
  119. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.ttf +0 -0
  120. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.woff +0 -0
  121. data/lib/diamonds/public/javascripts/font-awesome/fonts/fontawesome-webfont.woff2 +0 -0
  122. data/lib/diamonds/public/javascripts/font-awesome/less/animated.less +34 -0
  123. data/lib/diamonds/public/javascripts/font-awesome/less/bordered-pulled.less +25 -0
  124. data/lib/diamonds/public/javascripts/font-awesome/less/core.less +12 -0
  125. data/lib/diamonds/public/javascripts/font-awesome/less/fixed-width.less +6 -0
  126. data/lib/diamonds/public/javascripts/font-awesome/less/font-awesome.less +17 -0
  127. data/lib/diamonds/public/javascripts/font-awesome/less/icons.less +677 -0
  128. data/lib/diamonds/public/javascripts/font-awesome/less/larger.less +13 -0
  129. data/lib/diamonds/public/javascripts/font-awesome/less/list.less +19 -0
  130. data/lib/diamonds/public/javascripts/font-awesome/less/mixins.less +26 -0
  131. data/lib/diamonds/public/javascripts/font-awesome/less/path.less +15 -0
  132. data/lib/diamonds/public/javascripts/font-awesome/less/rotated-flipped.less +20 -0
  133. data/lib/diamonds/public/javascripts/font-awesome/less/stacked.less +20 -0
  134. data/lib/diamonds/public/javascripts/font-awesome/less/variables.less +688 -0
  135. data/lib/diamonds/public/javascripts/font-awesome/scss/_animated.scss +34 -0
  136. data/lib/diamonds/public/javascripts/font-awesome/scss/_bordered-pulled.scss +25 -0
  137. data/lib/diamonds/public/javascripts/font-awesome/scss/_core.scss +12 -0
  138. data/lib/diamonds/public/javascripts/font-awesome/scss/_fixed-width.scss +6 -0
  139. data/lib/diamonds/public/javascripts/font-awesome/scss/_icons.scss +677 -0
  140. data/lib/diamonds/public/javascripts/font-awesome/scss/_larger.scss +13 -0
  141. data/lib/diamonds/public/javascripts/font-awesome/scss/_list.scss +19 -0
  142. data/lib/diamonds/public/javascripts/font-awesome/scss/_mixins.scss +26 -0
  143. data/lib/diamonds/public/javascripts/font-awesome/scss/_path.scss +15 -0
  144. data/lib/diamonds/public/javascripts/font-awesome/scss/_rotated-flipped.scss +20 -0
  145. data/lib/diamonds/public/javascripts/font-awesome/scss/_stacked.scss +20 -0
  146. data/lib/diamonds/public/javascripts/font-awesome/scss/_variables.scss +688 -0
  147. data/lib/diamonds/public/javascripts/font-awesome/scss/font-awesome.scss +17 -0
  148. data/lib/diamonds/public/javascripts/jquery-2.1.4.min.js +4 -0
  149. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  150. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  151. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_flat_10_000000_40x100.png +0 -0
  152. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  153. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  154. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  155. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  156. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  157. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  158. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_222222_256x240.png +0 -0
  159. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_228ef1_256x240.png +0 -0
  160. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ef8c08_256x240.png +0 -0
  161. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ffd27a_256x240.png +0 -0
  162. data/lib/diamonds/public/javascripts/jquery-ui/images/ui-icons_ffffff_256x240.png +0 -0
  163. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.min.css +7 -0
  164. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.min.js +13 -0
  165. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.structure.min.css +5 -0
  166. data/lib/diamonds/public/javascripts/jquery-ui/jquery-ui.theme.min.css +5 -0
  167. data/lib/diamonds/public/javascripts/jquery.js +4 -0
  168. data/lib/diamonds/public/javascripts/jquery.ui.touch-punch.min.js +11 -0
  169. data/lib/diamonds/public/javascripts/ne pas utiliser version en retard jeezs_editor.js b/data/lib/diamonds/public/javascripts/ne pas utiliser version en retard → jeezs_editor.js +0 -0
  170. data/lib/diamonds/public/javascripts/standalone.js +2316 -0
  171. data/lib/diamonds/public/javascripts/standalone.min.js +1 -0
  172. data/lib/diamonds/public/javascripts/wysiwyg-editor.min.css +1 -0
  173. data/lib/diamonds/public/javascripts/wysiwyg-editor.min.js +1 -0
  174. data/lib/diamonds/public/javascripts/wysiwyg.min.js +1 -0
  175. data/lib/diamonds/user_app/main.rb +45 -0
  176. data/lib/diamonds/version.rb +3 -0
  177. data/lib/diamonds/views/index.erb +35 -0
  178. data/lib/diamonds/views/javascripts/jeezs_editor.js.erb +365 -0
  179. data/lib/diamonds/views/stylesheets/main.css.erb +244 -0
  180. metadata +490 -0
@@ -0,0 +1,24 @@
1
+ require 'browser/css/declaration'
2
+ require 'browser/css/style_sheet'
3
+ require 'browser/css/rule'
4
+ require 'browser/css/rule/style'
5
+
6
+ module Kernel
7
+ # Create a <style> element from a string or a block using the
8
+ # {Browser::CSS::Builder} DSL.
9
+ #
10
+ # @param text [String] the CSS text
11
+ # @return [Browser::DOM::Element] the create <style> element
12
+ def CSS(text = nil, &block)
13
+ style = $document.create_element(:style)
14
+ style[:type] = 'text/css'
15
+
16
+ if block
17
+ style.inner_text = Paggio.css(&block)
18
+ else
19
+ style.inner_text = text
20
+ end
21
+
22
+ style
23
+ end
24
+ end
@@ -0,0 +1,83 @@
1
+ module Browser; module CSS
2
+
3
+ class Declaration
4
+ include Native
5
+ include Enumerable
6
+
7
+ def rule
8
+ Rule.new(`#@native.parentRule`) if defined?(`#@native.parentRule`)
9
+ end
10
+
11
+ def assign(data)
12
+ data.each {|name, value|
13
+ self[name] = value
14
+ }
15
+
16
+ self
17
+ end
18
+
19
+ def replace(string)
20
+ `#@native.cssText = #{string}`
21
+ end
22
+
23
+ def apply(&block)
24
+ Paggio::CSS::Definition.new(&block).each {|style|
25
+ if style.important
26
+ `#@native.setProperty(#{style.name}, #{style.value}, "important")`
27
+ else
28
+ `#@native.setProperty(#{style.name}, #{style.value}, "")`
29
+ end
30
+ }
31
+ end
32
+
33
+ def delete(name)
34
+ `#@native.removeProperty(#{name})`
35
+ end
36
+
37
+ def [](name)
38
+ %x{
39
+ var result = #@native.getPropertyValue(#{name});
40
+
41
+ if (result == null || result === "") {
42
+ return nil;
43
+ }
44
+
45
+ return result;
46
+ }
47
+ end
48
+
49
+ def []=(name, value)
50
+ `#@native.setProperty(#{name}, #{value.to_s}, "")`
51
+ end
52
+
53
+ def important?(name)
54
+ `#@native.getPropertyPriority(#{name}) == "important"`
55
+ end
56
+
57
+ def each(&block)
58
+ return enum_for :each unless block_given?
59
+
60
+ %x{
61
+ for (var i = 0, length = #@native.length; i < length; i++) {
62
+ var name = #@native.item(i);
63
+
64
+ #{yield `name`, self[`name`]}
65
+ }
66
+ }
67
+
68
+ self
69
+ end
70
+
71
+ alias_native :length
72
+ alias_native :to_s, :cssText
73
+
74
+ def method_missing(name, value = nil)
75
+ if name.end_with? ?=
76
+ self[name[0 .. -2]] = value
77
+ else
78
+ self[name]
79
+ end
80
+ end
81
+ end
82
+
83
+ end; end
@@ -0,0 +1,48 @@
1
+ module Browser; module CSS
2
+
3
+ class Rule
4
+ include Native
5
+
6
+ STYLE_RULE = 1
7
+ CHARSET_RULE = 2
8
+ IMPORT_RULE = 3
9
+ MEDIA_RULE = 4
10
+ FONT_FACE_RULE = 5
11
+ PAGE_RULE = 6
12
+ KEYFRAMES_RULE = 7
13
+ KEYFRAME_RULE = 8
14
+ NAMESPACE_RULE = 10
15
+ COUNTER_STYLE_RULE = 11
16
+ SUPPORTS_RULE = 12
17
+ DOCUMENT_RULE = 13
18
+ FONT_FEATURE_VALUES_RULE = 14
19
+ VIEWPORT_RULE = 15
20
+ REGION_STYLE_RULE = 16
21
+
22
+ def self.new(rule)
23
+ if self == Rule
24
+ @classes ||= [nil, Style]
25
+
26
+ if klass = @classes[`rule.type`]
27
+ klass.new(rule)
28
+ else
29
+ raise ArgumentError, 'cannot instantiate a non derived Rule object'
30
+ end
31
+ else
32
+ super(rule)
33
+ end
34
+ end
35
+
36
+ alias_native :text, :cssText
37
+ alias_native :to_s, :cssText
38
+
39
+ def parent
40
+ Rule.new(`#@native.parentRule`) if `#@native.parentRule != null`
41
+ end
42
+
43
+ def style_sheet
44
+ StyleSheet.new(`#@native.parentStyleSheet`) if `#@native.parentStyleSheet != null`
45
+ end
46
+ end
47
+
48
+ end; end
@@ -0,0 +1,16 @@
1
+ module Browser; module CSS; class Rule
2
+
3
+ class Style < Rule
4
+ alias_native :selector, :selectorText
5
+ alias_native :id, :selectorText
6
+
7
+ def declaration
8
+ Declaration.new(`#@native.style`)
9
+ end
10
+
11
+ def method_missing(*args, &block)
12
+ declaration.__send__(*args, &block)
13
+ end
14
+ end
15
+
16
+ end; end; end
@@ -0,0 +1,83 @@
1
+ module Browser; module CSS
2
+
3
+ class StyleSheet
4
+ include Native
5
+
6
+ def initialize(what)
7
+ if what.is_a? DOM::Element
8
+ super(`#{what.to_n}.sheet`)
9
+ else
10
+ super(what)
11
+ end
12
+ end
13
+
14
+ alias_native :disabled?, :disabled
15
+ alias_native :href
16
+ alias_native :title
17
+ alias_native :type
18
+
19
+ def media
20
+ Media.new(`#@native.media`) if `#@native.media != null`
21
+ end
22
+
23
+ def owner
24
+ DOM(`#@native.ownerNode`)
25
+ end
26
+
27
+ def parent
28
+ Sheet.new(`#@native.parentStyleSheet`) if `#@native.parentStyleSheet != null`
29
+ end
30
+
31
+ def rules
32
+ Native::Array.new(`#@native.cssRules`) { |e|
33
+ Rule.new(e)
34
+ }
35
+ end
36
+
37
+ def delete(index)
38
+ `#@native.deleteRule(index)`
39
+ end
40
+
41
+ def insert(index, rule)
42
+ `#@native.insertRule(#{rule}, #{index})`
43
+ end
44
+
45
+ def rule(selector, body)
46
+ unless String === selector
47
+ selector = selector.join ', '
48
+ end
49
+
50
+ unless String === body
51
+ body = body.map {|name, value|
52
+ "#{name}: #{value};"
53
+ }.join "\n"
54
+ end
55
+
56
+ insert(length, "#{selector} { #{body} }")
57
+ end
58
+
59
+ def [](id)
60
+ rules.find { |r| log r; r.id == id }
61
+ end
62
+
63
+ def method_missing(*args, &block)
64
+ rules.__send__(*args, &block)
65
+ end
66
+
67
+ class Media < Native::Array
68
+ alias_native :text, :mediaText
69
+ alias_native :to_s, :mediaText
70
+
71
+ def push(medium)
72
+ `#@native.appendMedium(#{medium})`
73
+
74
+ self
75
+ end
76
+
77
+ def delete(medium)
78
+ `#@native.deleteMedium(#{medium})`
79
+ end
80
+ end
81
+ end
82
+
83
+ end; end
@@ -0,0 +1,188 @@
1
+ module Browser; module CSS
2
+
3
+ class Unit
4
+ COMPATIBLE = %i[in pt mm cm px pc]
5
+
6
+ attr_reader :type
7
+
8
+ def initialize(number, type)
9
+ @number = number
10
+ @type = type
11
+ end
12
+
13
+ def coerce(other)
14
+ return self, other
15
+ end
16
+
17
+ def ==(other)
18
+ @number == convert(other, @type)
19
+ end
20
+
21
+ def ===(other)
22
+ @type == other.type && @number == other.to_f
23
+ end
24
+
25
+ alias eql? ==
26
+
27
+ def hash
28
+ [@number, @type].hash
29
+ end
30
+
31
+ %i[em ex ch rem vh vw vmin vmax px mm cm in pt pc].each {|name|
32
+ define_method name do
33
+ Unit.new(convert(self, name), name)
34
+ end
35
+ }
36
+
37
+ def +(other)
38
+ return Unit.new(@number + other, @type) unless Unit === other
39
+
40
+ if @type == other.type
41
+ Unit.new(@number + other.to_f, @type)
42
+ elsif compatible?(self) and compatible?(other)
43
+ Unit.new(@number + convert(other, @type), @type)
44
+ else
45
+ raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
46
+ end
47
+ end
48
+
49
+ def -(other)
50
+ return Unit.new(@number - other, @type) unless Unit === other
51
+
52
+ if @type == other.type
53
+ Unit.new(@number - other.to_f, @type)
54
+ elsif compatible?(self) and compatible?(other)
55
+ Unit.new(@number - convert(other, @type), @type)
56
+ else
57
+ raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
58
+ end
59
+ end
60
+
61
+ def *(other)
62
+ return Unit.new(@number * other, @type) unless Unit === other
63
+
64
+ if @type == other.type
65
+ Unit.new(@number * other.to_f, @type)
66
+ elsif compatible?(self) and compatible?(other)
67
+ Unit.new(@number * convert(other, @type), @type)
68
+ else
69
+ raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
70
+ end
71
+ end
72
+
73
+ def /(other)
74
+ return Unit.new(@number / other, @type) unless Unit === other
75
+
76
+ if @type == other.type
77
+ Unit.new(@number / other.to_f, @type)
78
+ elsif compatible?(self) and compatible?(other)
79
+ Unit.new(@number / convert(other, @type), @type)
80
+ else
81
+ raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
82
+ end
83
+ end
84
+
85
+ def -@
86
+ Unit.new(@number * -1, @type)
87
+ end
88
+
89
+ def +@
90
+ Unit.new(@number, @type)
91
+ end
92
+
93
+ def to_i
94
+ @number.to_i
95
+ end
96
+
97
+ def to_f
98
+ @number.to_f
99
+ end
100
+
101
+ def to_u
102
+ self
103
+ end
104
+
105
+ def to_s
106
+ "#{@number}#{@type}"
107
+ end
108
+
109
+ alias to_str to_s
110
+ alias inspect to_s
111
+
112
+ private
113
+ def compatible?(unit)
114
+ COMPATIBLE.include?(unit.type)
115
+ end
116
+
117
+ def convert(unit, type)
118
+ value = unit.to_f
119
+
120
+ return value if unit.type == type
121
+
122
+ px = case unit.type
123
+ when :in then value * 96
124
+ when :pt then value * 4.0 / 3.0
125
+ when :pc then value / 12 * 4.0 / 3.0
126
+ when :mm then value * 3.77953
127
+ when :cm then value * 10 * 3.77953
128
+ when :px then value
129
+ end
130
+
131
+ case type
132
+ when :in then px / 96.0
133
+ when :pt then px / 4.0 / 3.0
134
+ when :pc then px * 12 / 4.0 / 3.0
135
+ when :mm then px / 3.77953
136
+ when :cm then px / 10 / 3.77953
137
+ when :px then px
138
+ end
139
+ end
140
+ end
141
+
142
+ end; end
143
+
144
+ class Numeric
145
+ Unit = Browser::CSS::Unit
146
+
147
+ %i[em ex ch rem vh vw vmin vmax px mm cm in pt pc].each {|name|
148
+ define_method name do
149
+ Unit.new(self, name)
150
+ end
151
+ }
152
+
153
+ alias old_percent %
154
+
155
+ def %(other = nil)
156
+ if other
157
+ old_percent(other)
158
+ else
159
+ Unit.new(self, :%)
160
+ end
161
+ end
162
+
163
+ def to_u
164
+ self
165
+ end
166
+ end
167
+
168
+ class String
169
+ def to_u
170
+ if matches = self.match(/^([\d+.]+)(.+)?$/)
171
+ value = matches[1].to_f
172
+
173
+ if unit = matches[2]
174
+ value.__send__(unit.downcase)
175
+ else
176
+ value
177
+ end
178
+ else
179
+ 0
180
+ end
181
+ end
182
+ end
183
+
184
+ class NilClass
185
+ def to_u
186
+ 0
187
+ end
188
+ end