vigilem-dom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b56d55d2f86728b678f5e5fbe144190c18a9427f
4
+ data.tar.gz: cff68b533d8098ec170300c5110b6ef892b42165
5
+ SHA512:
6
+ metadata.gz: 4fc80e126ffa25143ed2bf06453a1f67793b332f33a24dbf55c1880db722fe50b4ca90ab6c6862b98f135c6ef408805408777289d7cbbf7cb3adaf7d6e8c7dd1
7
+ data.tar.gz: 312cd3a18461414d87f97c9b2e78d17818cc5d2122deb4905951f89e632f8817fc9d50dd6e68d33e09e577e2507b9bc6660dc92756882bde86dc42ff962dc079
@@ -0,0 +1,27 @@
1
+ require 'w3c/dom'
2
+
3
+ module Vigilem
4
+ module DOM
5
+
6
+ class << self
7
+ attr_writer :strict
8
+ # controls checking of various values
9
+ # @return [TrueClass||FalseClass]
10
+ def strict
11
+ @strict ||= true
12
+ end
13
+ end
14
+
15
+ KeyValues = ::W3C::DOM3::KeyValues
16
+ CodeValues = ::W3C::DOM3::CodeValues
17
+
18
+ Event = ::W3C::DOM4::Event
19
+
20
+ UIEvent = ::W3C::DOM3::UIEvent
21
+
22
+ require 'vigilem/dom/keyboard_event'
23
+
24
+ end
25
+ end
26
+
27
+ require 'vigilem/dom/utils'
@@ -0,0 +1,187 @@
1
+ require 'vigilem/support/core_ext'
2
+
3
+ require 'vigilem/dom'
4
+
5
+ module Vigilem
6
+ module DOM
7
+ #
8
+ # @see W3C::DOM3::KeyboardEvent
9
+ class KeyboardEvent < ::W3C::DOM3::KeyboardEvent
10
+
11
+ attr_reader :os_specific, :modifier_state #@todo, :chr
12
+
13
+ #
14
+ # @see #SharedKeyboardAndMouseEventInit
15
+ # @param [String || Symbol] type
16
+ # @param [Hash] options
17
+ # @option [String] :key
18
+ # @option [String] :code
19
+ # @option [Hash] :location
20
+ # @option :location [Integer] 0x00 DOM_KEY_LOCATION_STANDARD
21
+ # @option :location [Integer] 0x01 DOM_KEY_LOCATION_LEFT
22
+ # @option :location [Integer] 0x02 DOM_KEY_LOCATION_RIGHT
23
+ # @option :location [Integer] 0x03 DOM_KEY_LOCATION_NUMPAD
24
+ # @option [TrueClass || FalseClass] repeat
25
+ # @option[Hash] :modifiers || :modifier_state
26
+ # @option modifiers [TrueClass || FalseClass || NilClass] :alt, :altKey
27
+ # @option modifiers [TrueClass || FalseClass || NilClass] :alt_graph, :AltGraph
28
+ # @option modifiers [TrueClass || FalseClass || NilClass] :caps_lock, :CapsLock
29
+ # @option modifiers [TrueClass || FalseClass || NilClass] :control, :ctrl, :ctrlKey
30
+ # @option modifiers [TrueClass || FalseClass || NilClass] :meta, :metaKey
31
+ # @option modifiers [TrueClass || FalseClass || NilClass] :num_lock, :NumLock
32
+ # @option modifiers [TrueClass || FalseClass || NilClass] :scroll, :scroll_lock
33
+ # @option modifiers [TrueClass || FalseClass || NilClass] :shift, :shiftKey
34
+ # @option modifiers [TrueClass || FalseClass || NilClass] :symbol, :symbol_lock
35
+ # @param [Hash] os_specific
36
+ def initialize(type, options={})
37
+ raise TypeError, "invalid type `#{type}'" if strict = Vigilem::DOM.strict and not self.class.types.include?(type)
38
+ @type = type
39
+ @key = options[:key] ||= ''
40
+ if options[:location] and strict and (kl = KeyLocations).constants.none? {|const| kl.const_get(const) == options[:location] }
41
+ raise ArgumentError, "invalid location `#{options[:location]}'"
42
+ end
43
+ @location = options[:location] ||= 0
44
+ @repeat = options[:repeat] ||= false
45
+ @os_specific = self.class.hash_frozen_clone(options[:os_specific] || {})
46
+
47
+ @modifier_state = self.class.hash_frozen_clone(options[:modifiers] || options[:modifier_state] || {})
48
+
49
+ mod_dict = self.class.shared_keyboard_and_mouse_event_init(@modifier_state)
50
+
51
+ super(type, mod_dict.merge(options))
52
+ end
53
+
54
+ #---
55
+
56
+ @modifier_attrs = ::W3C::DOM3::KeyboardEvent.instance_variable_get(:@modifer_attrs)
57
+
58
+ @modifier_attrs.each do |method_name|
59
+
60
+ snake_case = method_name.to_s.snakecase.to_sym
61
+
62
+ mod_state_name = "keyModifierState#{method_name}".to_sym
63
+ define_method(snake_case) do
64
+ modifier_state[mod_state_name]
65
+ end
66
+ end
67
+
68
+ #---
69
+
70
+ @dom_3_attrs = ::W3C::DOM3::KeyboardEvent.instance_variable_get(:@dom_3_attrs) + ::W3C::DOM4::Event.instance_variable_get(:@attrs)
71
+
72
+ @dom_3_attrs.each do |method_name|
73
+
74
+ snake_case = method_name.to_s.snakecase.to_sym
75
+
76
+ snake_case = "#{snake_case}?" if snake_case[0..1] == 'is'
77
+
78
+ alias_method snake_case, method_name
79
+ end
80
+
81
+ #
82
+ # @param [#to_s] key_type
83
+ # @return [KeyboardEvent]
84
+ def copy_to(key_type)
85
+ copy = Support::Utils.deep_dup(self)
86
+ copy.instance_variable_set(:@type, key_type.to_s)
87
+ copy
88
+ end
89
+
90
+ # has @type @key @location, so the object can be distinguised from other KeyboardEvents
91
+ # @return [String]
92
+ def to_s
93
+ "#{super().chop} @type=#{@type.inspect} @key=#{@key.inspect} @location=#{location.inspect}>"
94
+ end
95
+
96
+ #
97
+ # @return [String]
98
+ def inspect
99
+ "<#{self.class}:0x#{self.object_id} #{instance_variables.map {|var| "#{var}=#{instance_variable_get(var).inspect}" }.sort_by {|name| name[1..2]}.join(' ')}>"
100
+ end
101
+
102
+ class << self
103
+
104
+ #
105
+ # @return [Array<String>]
106
+ def types
107
+ @types ||= %w(keyup keypress keydown)
108
+ end
109
+
110
+ # this is not a hash so that the values can be #joined as a regex
111
+ # the last value of each group is the DOM value without the prefix
112
+ # @return [Array<Array<Symbol>>]
113
+ def alternative_key_names
114
+ @alternative_key_names ||= [[:alt, :menu, :altKey], [:alt_graph, :AltGraph], [:caps_lock, :CapsLock], [:ctrl, :control, :ctrlKey],
115
+ [:Fn], [:fn_lock, :FnLock], [:Hyper], [:meta, :metaKey], [:num_lock, :NumLock], [:OS],
116
+ [:scroll_lock, :ScrollLock], [:shift, :shiftKey], [:Super], [:Symbol], [:symbol_lock, :SymbolLock]]
117
+ end
118
+
119
+ # converts a common key to shared_keyboard_and_mouse_event_init_key
120
+ # dom_3 are not prefixed by keyModifierState
121
+ # @param [#to_s] key_name
122
+ # @return [Symbol || NilClass]
123
+ def shared_keyboard_and_mouse_event_init_key(key_name)
124
+ key_group = alternative_key_names.find {|key_grp| /^(#{key_grp.join('|')})$/i =~ key_name.to_s }
125
+ if key_group
126
+ if @dom_3_attrs.include?(correct_key = key_group.last)
127
+ correct_key
128
+ else
129
+ "keyModifierState#{correct_key}".to_sym
130
+ end
131
+ end
132
+ end
133
+
134
+ # gets the keys for SharedKeyboardAndMouseEventInit
135
+ #
136
+ # @return [Array<Symbol>]
137
+ def shared_keyboard_and_mouse_event_init_keys
138
+ shared_keyboard_and_mouse_event_init_keys ||= alternative_key_names.map do |key_grp|
139
+ if @dom_3_attrs.include?(correct_key = key_grp.last)
140
+ correct_key
141
+ else
142
+ "keyModifierState#{correct_key}".to_sym
143
+ end
144
+ end
145
+ end
146
+
147
+ # converts common names to SharedKeyboardAndMouseEventInit
148
+ # @param [Hash || Array] hsh_or_ary
149
+ # @return [Hash]
150
+ def SharedKeyboardAndMouseEventInit(hsh_or_ary)
151
+
152
+ hsh = if hsh_or_ary.is_a?(Hash)
153
+ hsh_or_ary
154
+ else
155
+ Hash[hsh_or_ary.zip([true] * hsh_or_ary.size)]
156
+ end
157
+
158
+ alternative_key_names.each_with_object({}) do |key_grp, out_hash|
159
+ skamei_name = shared_keyboard_and_mouse_event_init_key(key_grp.last)
160
+ matched_usr_key = hsh.keys.find {|usr_key| usr_key =~ /^(#{key_grp.join('|')})$/i }
161
+ out_hash[skamei_name] = !!hsh[matched_usr_key]
162
+ end
163
+ end
164
+
165
+ alias_method :shared_keyboard_and_mouse_event_init, :SharedKeyboardAndMouseEventInit
166
+ end
167
+
168
+ #
169
+ #
170
+ # @return [Hash]
171
+ def modifier_state
172
+ @modifier_state ||= Hash[DOM::KeyValues::ModifierKeys.zip([false])].tap {|hsh| hsh.default = false }.freeze
173
+ end
174
+
175
+ private
176
+
177
+ #
178
+ # @param [Hash] hsh_obj
179
+ # @return [Hash] cloned obj
180
+ def self.hash_frozen_clone(hsh_obj)
181
+ hsh_obj.map.with_object({}) do |(key, value), hsh|
182
+ hsh[key] = value.clone rescue value
183
+ end.freeze
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,49 @@
1
+ require 'vigilem/dom/keyboard_event'
2
+
3
+ module Vigilem
4
+ module DOM
5
+ # @author jtzero
6
+ #
7
+ # useful dom mapping methods
8
+ module Utils
9
+
10
+ #
11
+ # @return [Hash<String, Integer>]
12
+ def name_codes
13
+ @name_codes ||= Hash.new(KeyboardEvent::DOM_KEY_LOCATION_STANDARD).merge({
14
+ 'L' => KeyboardEvent::DOM_KEY_LOCATION_LEFT,
15
+ 'R' => KeyboardEvent::DOM_KEY_LOCATION_RIGHT,
16
+ 'NUMPAD' => KeyboardEvent::DOM_KEY_LOCATION_NUMPAD,
17
+ 'KP' => KeyboardEvent::DOM_KEY_LOCATION_NUMPAD
18
+ })
19
+ end
20
+
21
+ #
22
+ # @return [Hash<Integer, Regexp>
23
+ def codes_regex
24
+ @codes_regex ||= { KeyboardEvent::DOM_KEY_LOCATION_LEFT => /left/i,
25
+ KeyboardEvent::DOM_KEY_LOCATION_RIGHT => /right/i,
26
+ KeyboardEvent::DOM_KEY_LOCATION_NUMPAD => /numpad/i,
27
+ }
28
+ end
29
+
30
+ # @todo rename dom_location_to_common_str
31
+ # @param [Integer] dom_location_code
32
+ # @return [String]
33
+ def to_dom_location_common_str(dom_location_code)
34
+ (@code_names ||= name_codes.invert.tap {|obj| obj.default = '' })[dom_location_code]
35
+ end
36
+
37
+ #
38
+ # @param [String<'L', 'R', 'NUMPAD', 'KP', Object>] location
39
+ # @return [Integer]
40
+ def common_str_to_dom_location(location)
41
+ name_codes[location]
42
+ end
43
+
44
+ extend self
45
+ end
46
+ end
47
+ end
48
+
49
+ VDOM = ::Vigilem::DOM
@@ -0,0 +1,5 @@
1
+ module Vigilem
2
+ module DOM
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ # http://www.w3.org/TR/DOM-Level-3-Events/
2
+ module W3C
3
+ module DOM3
4
+ end
5
+ module DOM4
6
+ end
7
+ end
8
+
9
+ require 'w3c/dom4/event'
10
+
11
+ require 'w3c/dom3/ui_event'
12
+
13
+ require 'w3c/dom3/key_values'
14
+ require 'w3c/dom3/code_values'
15
+
16
+ require 'w3c/dom3/keyboard_event'
@@ -0,0 +1,33 @@
1
+ module W3C
2
+ module DOM3
3
+ # http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-code-20140612/
4
+ module CodeValues
5
+ WritingSystemKeys = ['Backquote', 'Backslash', 'Backspace', 'BracketLeft', 'BracketRight', 'Comma', 'Digit0', 'Digit1', 'Digit2',
6
+ 'Digit3', 'Digit4', 'Digit5', 'Digit6', 'Digit7', 'Digit8', 'Digit9', 'Equal', 'IntlBackslash', 'IntlHash', 'IntlRo',
7
+ 'IntlYen', 'KeyA', 'KeyB', 'KeyC', 'KeyD', 'KeyE', 'KeyF', 'KeyG', 'KeyH', 'KeyI', 'KeyJ', 'KeyK', 'KeyL', 'KeyM', 'KeyN',
8
+ 'KeyO', 'KeyP', 'KeyQ', 'KeyR', 'KeyS', 'KeyT', 'KeyU', 'KeyV', 'KeyW', 'KeyX', 'KeyY', 'KeyZ', 'Minus', 'Period', 'Quote',
9
+ 'Semicolon', 'Slash']
10
+
11
+ FunctionalKeys = ['AltLeft', 'AltRight', 'CapsLock', 'ContextMenu', 'ControlLeft', 'ControlRight', 'Enter', 'OSLeft', 'OSRight',
12
+ 'ShiftLeft', 'ShiftRight', 'Space', 'Tab', 'Convert', 'KanaMode', 'Lang1', 'Lang2', 'Lang3', 'Lang4', 'Lang5', 'NonConvert']
13
+
14
+ ControlPadSection = ['Delete', 'End', 'Help', 'Home', 'Insert', 'PageDown', 'PageUp']
15
+
16
+ ArrowPadSection = ['ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp']
17
+
18
+ NumpadSection = ['NumLock', 'Numpad0', 'Numpad1', 'Numpad2', 'Numpad3', 'Numpad4', 'Numpad5', 'Numpad6', 'Numpad7', 'Numpad8', 'Numpad9',
19
+ 'NumpadAdd', 'NumpadBackspace', 'NumpadClear', 'NumpadClearEntry', 'NumpadComma', 'NumpadDecimal', 'NumpadDivide', 'NumpadEnter',
20
+ 'NumpadEqual', 'NumpadMemoryAdd', 'NumpadMemoryClear', 'NumpadMemoryRecall', 'NumpadMemoryStore', 'NumpadMemorySubtract', 'NumpadMultiply',
21
+ 'NumpadParenLeft', 'NumpadParenRight', 'NumpadSubtract']
22
+
23
+ FunctionSection = ['Escape', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'Fn', 'FLock', 'PrintScreen', 'ScrollLock', 'Pause']
24
+
25
+ MediaKeys = ['BrowserBack', 'BrowserFavorites', 'BrowserForward', 'BrowserHome', 'BrowserRefresh', 'BrowserSearch', 'BrowserStop', 'Eject',
26
+ 'LaunchApp1', 'LaunchApp2', 'LaunchMail', 'MediaPlayPause', 'MediaSelect', 'MediaStop', 'MediaTrackNext', 'MediaTrackPrevious', 'Power',
27
+ 'Sleep', 'VolumeDown', 'VolumeMute', 'VolumeUp', 'WakeUp']
28
+
29
+ LegacyKeysandNon_StandardKeys = ['Hyper', 'Super', 'Turbo', 'Abort', 'Resume', 'Suspend', 'Again', 'Copy', 'Cut', 'Find', 'Open', 'Paste', 'Props', 'Select',
30
+ 'Undo', 'Hiragana', 'Katakana']
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,56 @@
1
+ module W3C
2
+ module DOM3
3
+ # @author jtzero
4
+ # http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-key-20140612/
5
+ module KeyValues
6
+ SpecialKeyValues = 'Unidentified'
7
+
8
+ ModifierKeys = ['Accel', 'Alt', 'AltGraph', 'CapsLock', 'Control', 'Fn', 'FnLock', 'Hyper', 'Meta',
9
+ 'NumLock', 'OS', 'ScrollLock', 'Shift', 'Super', 'Symbol', 'SymbolLock']
10
+
11
+ WhitespaceKeys = ['Enter', 'Separator', 'Tab']
12
+
13
+ NavigationKeys = ['ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'End', 'Home', 'PageDown', 'PageUp']
14
+
15
+ EditingKeys = ['Backspace', 'Clear', 'Copy', 'CrSel', 'Cut', 'Delete', 'EraseEof', 'ExSel', 'Insert', 'Paste', 'Redo', 'Undo' ]
16
+
17
+ UIKeys = ['Accept', 'Again', 'Attn', 'Cancel', 'ContextMenu', 'Escape', 'Execute', 'Find', 'Help', 'Pause',
18
+ 'Play', 'Props', 'Select', 'ZoomIn', 'ZoomOut']
19
+
20
+ DeviceKeys = ['BrightnessDown', 'BrightnessUp', 'Camera', 'Eject', 'LogOff', 'Power', 'PowerOff', 'PrintScreen', 'Hibernate', 'Standby', 'WakeUp']
21
+
22
+ IMEandCompositionKeys = ['AllCandidates', 'Alphanumeric', 'CodeInput', 'Compose', 'Convert', 'Dead', 'FinalMode', 'GroupFirst', 'GroupLast',
23
+ 'GroupNext', 'GroupPrevious', 'ModeChange', 'NextCandidate', 'NonConvert', 'PreviousCandidate',
24
+ 'Process', 'SingleCandidate', 'HangulMode', 'HanjaMode', 'JunjaMode']
25
+
26
+ KeysspecifictoKoreankeyboards = ['Eisu', 'Hankaku', 'Hiragana', 'HiraganaKatakana', 'KanaMode', 'KanjiMode', 'Katakana', 'Romaji',
27
+ 'Zenkaku', 'ZenkakuHankaku']
28
+ KeysspecifictoJapanesekeyboards = ['Eisu', 'Hankaku', 'Hiragana', 'HiraganaKatakana', 'KanaMode', 'KanjiMode', 'Katakana', 'Romaji',
29
+ 'Zenkaku', 'ZenkakuHankaku']
30
+
31
+ General_PurposeFunctionKeys = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'Soft1', 'Soft2', 'Soft3', 'Soft4']
32
+
33
+ MultimediaKeys = ['Close', 'MailForward', 'MailReply', 'MailSend', 'MediaPlayPause', 'MediaSelect', 'MediaStop', 'MediaTrackNext',
34
+ 'MediaTrackPrevious', 'New', 'Open', 'Print', 'Save', 'SpellCheck', 'VolumeDown', 'VolumeUp', 'VolumeMute']
35
+
36
+ ApplicationKeys = ['Launch', 'LaunchCalculator', 'LaunchCalendar', 'LaunchMail', 'LaunchMediaPlayer', 'LaunchMusicPlayer',
37
+ 'LaunchMyComputer', 'LaunchScreenSaver', 'LaunchSpreadsheet', 'LaunchWebBrowser', 'LaunchWebCam', 'LaunchWordProcessor']
38
+
39
+ BrowserKeys = ['BrowserBack', 'BrowserFavorites', 'BrowserForward', 'BrowserHome', 'BrowserRefresh', 'BrowserSearch', 'BrowserStop']
40
+
41
+ MediaControllerKeys = ['AudioBalanceLeft', 'AudioBalanceRight', 'AudioBassBoostDown', 'AudioBassBoostUp', 'AudioFaderFront', 'AudioFaderRear',
42
+ 'AudioSurroundModeNext', 'AVRInput', 'AVRPower', 'ChannelDown', 'ChannelUp', 'ColorF0Red', 'ColorF1Green',
43
+ 'ColorF2Yellow', 'ColorF3Blue', 'ColorF4Grey', 'ColorF5Brown', 'ClosedCaptionToggle', 'Dimmer', 'DisplaySwap', 'Exit',
44
+ 'FavoriteClear0', 'FavoriteClear1', 'FavoriteClear2', 'FavoriteClear3', 'FavoriteRecall0', 'FavoriteRecall1',
45
+ 'FavoriteRecall2', 'FavoriteRecall3', 'FavoriteStore0', 'FavoriteStore1', 'FavoriteStore2', 'FavoriteStore3', 'Guide',
46
+ 'GuideNextDay', 'GuidePreviousDay', 'Info', 'InstantReplay', 'Link', 'ListProgram', 'LiveContent', 'Lock', 'MediaApps',
47
+ 'ContextMenu', 'MediaFastForward', 'MediaLast', 'MediaPause', 'MediaPlay', 'MediaRecord', 'MediaRewind', 'MediaSkip',
48
+ 'NextFavoriteChannel', 'NextUserProfile', 'OnDemand', 'PinPDown', 'PinPMove', 'PinPToggle', 'PinPUp', 'PlaySpeedDown',
49
+ 'PlaySpeedReset', 'PlaySpeedUp', 'RandomToggle', 'RcLowBattery', 'RecordSpeedNext', 'RfBypass', 'ScanChannelsToggle',
50
+ 'ScreenModeNext', 'Settings', 'SplitScreenToggle', 'STBInput', 'STBPower', 'Subtitle', 'Teletext', 'TV', 'TVInput',
51
+ 'TVPower', 'VideoModeNext', 'Wink', 'ZoomToggle','BrowserBack', 'BrowserForward', 'ContextMenu', 'Eject', 'End', 'Enter',
52
+ 'Home', 'MediaPlayPause', 'MediaStop', 'MediaNextTrack', 'MediaPreviousTrack', 'Power', 'Unidentified', 'VolumeDown',
53
+ 'VolumeUp', 'VolumeMute']
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,90 @@
1
+ require 'vigilem/support/core_ext'
2
+
3
+ module W3C
4
+ module DOM3
5
+ #
6
+ # http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-code-20140612/
7
+ # http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-key-20140612/
8
+ #
9
+ # http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#idl-def-KeyboardEvent
10
+ #
11
+ # https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.code
12
+ class KeyboardEvent < UIEvent
13
+
14
+ module KeyLocations
15
+ DOM_KEY_LOCATION_STANDARD = 0x00
16
+ DOM_KEY_LOCATION_LEFT = 0x01
17
+ DOM_KEY_LOCATION_RIGHT = 0x02
18
+ DOM_KEY_LOCATION_NUMPAD = 0x03
19
+ end
20
+
21
+ include KeyLocations
22
+
23
+ #
24
+ # @param [String] typeArg
25
+ # @param [Hash] keyboardEventInitDict
26
+ # @option keyboardEventInitDict
27
+ def initialize(typeArg, keyboardEventInitDict={})
28
+ @key = keyboardEventInitDict[:key] || ''
29
+
30
+ if (not @key.empty?) and keyboardEventInitDict[:code].to_s.empty?
31
+ raise ArgumentError, "keyboardEventInitDict[:key] has a value while keyboardEventInitDict[:code] is nil"
32
+ end
33
+
34
+ @code = keyboardEventInitDict[:code] || ''
35
+
36
+ @location = keyboardEventInitDict[:location] || 0
37
+ @repeat = keyboardEventInitDict[:repeat] || false
38
+ @isComposing = keyboardEventInitDict[:isComposing] || false
39
+
40
+ @modifier_state = Hash[DOM3::KeyValues::ModifierKeys.zip([false,
41
+ !!keyboardEventInitDict[:altKey],
42
+ !!keyboardEventInitDict[:keyModifierStateAltGraph],
43
+ !!keyboardEventInitDict[:keyModifierStateCapsLock],
44
+ !!keyboardEventInitDict[:ctrlKey],
45
+ !!keyboardEventInitDict[:keyModifierStateFn],
46
+ !!keyboardEventInitDict[:keyModifierStateFnLock],
47
+ !!keyboardEventInitDict[:keyModifierStateHyper],
48
+ !!keyboardEventInitDict[:metaKey],
49
+ !!keyboardEventInitDict[:keyModifierStateNumLock],
50
+ !!keyboardEventInitDict[:keyModifierStateOS],
51
+ !!keyboardEventInitDict[:keyModifierStateScrollLock],
52
+ !!keyboardEventInitDict[:shiftKey],
53
+ !!keyboardEventInitDict[:keyModifierStateSuper],
54
+ !!keyboardEventInitDict[:keyModifierStateSymbol],
55
+ !!keyboardEventInitDict[:keyModifierStateSymbolLock]])]
56
+ @modifier_state.default = false
57
+ super(typeArg, keyboardEventInitDict)
58
+ end
59
+
60
+ private
61
+
62
+ # attrs and modifier_state
63
+ @modifer_attrs = [:ctrlKey, :shiftKey, :altKey, :metaKey]
64
+
65
+ @dom_3_attrs = [:key, :code, :location, :repeat, :isComposing] + @modifer_attrs
66
+ # Legacy, introduced in DOM Level 3:
67
+ @attrs = @dom_3_attrs + [:charCode, :keyCode, :which]
68
+
69
+ public
70
+ attr_reader *@attrs
71
+
72
+ # defines methods for the modifiers that are also
73
+ # attr's
74
+ @modifer_attrs.each do |attr|
75
+ define_method(attr) do
76
+ instance_variable_get(:@modifier_state)[attr.to_s.gsub(/key/i, '').titlecase]
77
+ end
78
+ end
79
+
80
+ # Queries the state of a modifier using a key value.
81
+ #
82
+ # @param [String] keyArg A modifier key value
83
+ # @return [TrueClass || FalseClass]
84
+ def getModifierState(keyArg)
85
+ !!@modifier_state[keyArg]
86
+ end
87
+
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,31 @@
1
+ require 'w3c/dom4/event'
2
+
3
+ #
4
+ # http://www.w3.org/TR/DOM-Level-3-Events/#idl-def-UIEvent
5
+ #
6
+ module W3C
7
+ module DOM3
8
+ class UIEvent < DOM4::Event
9
+
10
+ #
11
+ # @param [String] type
12
+ # @param [Hash] eventInitDict
13
+ # @option :view unused
14
+ # @option :detail unused
15
+ # @return self
16
+ def initialize(type, eventInitDict={})
17
+ @view = eventInitDict[:view] || nil
18
+ @detail = eventInitDict[:detail] || 0
19
+ super(type, eventInitDict)
20
+ end
21
+
22
+ private
23
+ @attrs = [:view, :detail]
24
+ attr_writer *@attrs
25
+
26
+ public
27
+
28
+ attr_reader *@attrs
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ module W3C
2
+ module DOM4
3
+ #
4
+ # http://www.w3.org/TR/2014/WD-dom-20140710/#interface-event
5
+ class Event
6
+ NONE = 0;
7
+ CAPTURING_PHASE = 1;
8
+ AT_TARGET = 2;
9
+ BUBBLING_PHASE = 3;
10
+
11
+ #
12
+ # @param [String] type
13
+ # @param [Hash] eventInitDict
14
+ def initialize(type, eventInitDict={})
15
+ @type = type
16
+ @bubbles = eventInitDict[:bubbles] || false
17
+ @cancelable = eventInitDict[:cancelable] || false
18
+ @timeStamp = Time.now.to_i
19
+ @isTrusted = true
20
+ end
21
+
22
+ private
23
+ # Unforgeable
24
+ @attrs = [:type, :target, :currentTarget, :eventPhase, :bubbles, :cancelable, :defaultPrevented, :isTrusted, :timeStamp]
25
+
26
+ public
27
+ attr_reader *@attrs
28
+
29
+ def stopPropagation() end
30
+ def stopImmediatePropagation() end
31
+ def preventDefault() end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+
3
+ Bundler.setup
4
+
5
+ require 'vigilem/core'
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vigilem/dom'
4
+
5
+ describe Vigilem::DOM::KeyboardEvent do
6
+
7
+ subject { described_class.new('keyup', key: 'a', code: 'KeyA', location: 0, repeat: false) }
8
+
9
+ describe '::new/initialization' do
10
+
11
+ it 'sets the default modifier_state' do
12
+
13
+ expect(subject.send(:modifier_state)).to eql({ "Accel"=>false,
14
+ "Alt"=>false, "AltGraph"=>false, "CapsLock"=>false,
15
+ "Control"=>false, "Fn"=>false, "FnLock"=>false,
16
+ "Hyper"=>false, "Meta"=>false, "NumLock"=>false,
17
+ "OS"=>false, "ScrollLock"=>false, "Shift"=>false,
18
+ "Super"=>false, "Symbol"=>false, "SymbolLock"=>false})
19
+ end
20
+
21
+ it 'sets up the key by the argument' do
22
+ expect(subject.key).to eql('a')
23
+ end
24
+
25
+ it 'sets up the code' do
26
+ expect(subject.code).to eql('KeyA')
27
+ end
28
+
29
+ it 'sets up location by the argument' do
30
+ expect(subject.location).to eql(0)
31
+ end
32
+
33
+ it 'sets up ctrlKey by defaults' do
34
+ expect(subject.ctrlKey).to eql(false)
35
+ end
36
+
37
+ it 'sets up shiftKey by defaults' do
38
+ expect(subject.shiftKey).to eql(false)
39
+ end
40
+
41
+ it 'sets up altKey by defaults' do
42
+ expect(subject.altKey).to eql(false)
43
+ end
44
+
45
+ it 'sets up metaKey by defaults' do
46
+ expect(subject.metaKey).to eql(false)
47
+ end
48
+
49
+ it 'sets up repeat by args' do
50
+ expect(subject.repeat).to eql(false)
51
+ end
52
+
53
+ end
54
+
55
+ describe '#copy_to' do
56
+ it 'copies the existing keyboard event and converts it the type passed in' do
57
+ expect(subject.copy_to('keydown').key).to eql('a')
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,37 @@
1
+ require 'vigilem/dom/utils'
2
+
3
+ describe Vigilem::DOM::Utils do
4
+ describe '::name_codes' do
5
+ it 'is a map of common strings to DOM values' do
6
+ expect(described_class::name_codes.keys.map(&:class).uniq).to contain_exactly(String)
7
+ expect(described_class::name_codes.values.map(&:class).uniq).to contain_exactly(Fixnum)
8
+ end
9
+ end
10
+
11
+ describe '::codes_regex' do
12
+ it 'it is a map of common patterns to DOM values' do
13
+ expect(described_class::codes_regex.keys.map(&:class).uniq).to contain_exactly(Fixnum)
14
+ expect(described_class::codes_regex.values.map(&:class).uniq).to contain_exactly(Regexp)
15
+ end
16
+ end
17
+
18
+ describe '::common_str_to_dom_location' do
19
+ it 'converts some common strings to DOM Location Codes' do
20
+ expect(described_class::common_str_to_dom_location('L')).to eql(1)
21
+ end
22
+ end
23
+
24
+ describe '::to_dom_location_common_str' do
25
+ it 'converts some common strings to DOM Location Codes' do
26
+ expect(described_class::to_dom_location_common_str(1)).to eql('L')
27
+ end
28
+
29
+ it 'defaults to empty String' do
30
+ expect(described_class::to_dom_location_common_str(nil)).to eql('')
31
+ end
32
+ end
33
+
34
+ it 'creates a shortcut constant' do
35
+ expect(Object.const_get(:VDOM)).to eql(Vigilem.const_get(:DOM))
36
+ end
37
+ end
@@ -0,0 +1,54 @@
1
+ require 'ostruct'
2
+
3
+ require 'w3c/dom4/event'
4
+
5
+ require 'w3c/dom3/ui_event'
6
+
7
+ require 'w3c/dom3/keyboard_event'
8
+
9
+ describe W3C::DOM3::KeyboardEvent do
10
+
11
+
12
+ describe described_class::KeyLocations do
13
+ it 'will match the w3c specs' do
14
+ expect(described_class::DOM_KEY_LOCATION_STANDARD).to eql(0x00)
15
+ expect(described_class::DOM_KEY_LOCATION_LEFT).to eql(0x01)
16
+ expect(described_class::DOM_KEY_LOCATION_RIGHT).to eql(0x02)
17
+ expect(described_class::DOM_KEY_LOCATION_NUMPAD).to eql(0x03)
18
+ end
19
+ end
20
+
21
+ describe 'read only attributes' do
22
+ it %q(aren't allowed to be updated) do
23
+ event = described_class.new('str')
24
+ expect(
25
+ described_class.instance_variable_get(:@attrs).map do |attr|
26
+ begin
27
+ event.public_send(:"#{attr}=", 'anything')
28
+ rescue StandardError => e
29
+ e
30
+ end
31
+ end
32
+ ).to all(be_a NoMethodError)
33
+ end
34
+
35
+ end
36
+
37
+ describe '#getModifierState' do
38
+ event = described_class.new('str', keyModifierStateNumLock: true)
39
+
40
+ it 'returns the status of passed in key name, at the time the event was fired' do
41
+ expect(event.getModifierState('NumLock')).to be_truthy
42
+ end
43
+
44
+ end
45
+
46
+ describe '::new/initialize' do
47
+ it 'errors if key have a value and code is empty' do
48
+ expect do
49
+ described_class.new('keyup', key: 'a', location: 0, repeat: false)
50
+ end.to raise_error(ArgumentError)
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,21 @@
1
+ require 'w3c/dom3/ui_event'
2
+
3
+ describe W3C::DOM3::UIEvent do
4
+
5
+ describe 'read only attributes' do
6
+ it %q(aren't allowed to be updated) do
7
+ host = described_class.new('str')
8
+ expect(
9
+ described_class.instance_variable_get(:@attrs).map do |attr|
10
+ begin
11
+ host.public_send(:"#{attr}=", 'anything')
12
+ rescue StandardError => e
13
+ e
14
+ end
15
+ end
16
+ ).to all(be_a NoMethodError)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,31 @@
1
+ #http://www.w3.org/TR/DOM-Level-2-Events/idl/events.idl
2
+ require 'w3c/dom4/event'
3
+
4
+ describe W3C::DOM4::Event do
5
+
6
+ describe 'constants' do
7
+ it 'will equal the value given in the dom spec' do
8
+ expect(described_class::NONE).to eql(0)
9
+ expect(described_class::CAPTURING_PHASE).to eql(1)
10
+ expect(described_class::AT_TARGET).to eql(2)
11
+ expect(described_class::BUBBLING_PHASE).to eql(3)
12
+ end
13
+ end
14
+
15
+ describe 'read only attributes' do
16
+ it %q(aren't allowed to be updated) do
17
+ host = described_class.new('str')
18
+ expect(
19
+ described_class.instance_variable_get(:@attrs).map do |attr|
20
+ begin
21
+ host.public_send(:"#{attr}=", 'anything')
22
+ rescue StandardError => e
23
+ e
24
+ end
25
+ end
26
+ ).to all(be_a NoMethodError)
27
+ end
28
+
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vigilem-dom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jtzero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vigilem-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-given
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: turnip
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: DOM Structures for Vigilem
126
+ email: jtzero511@gmail
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - lib/vigilem/dom.rb
132
+ - lib/vigilem/dom/keyboard_event.rb
133
+ - lib/vigilem/dom/utils.rb
134
+ - lib/vigilem/dom/version.rb
135
+ - lib/w3c/dom.rb
136
+ - lib/w3c/dom3/code_values.rb
137
+ - lib/w3c/dom3/key_values.rb
138
+ - lib/w3c/dom3/keyboard_event.rb
139
+ - lib/w3c/dom3/ui_event.rb
140
+ - lib/w3c/dom4/event.rb
141
+ - spec/spec_helper.rb
142
+ - spec/vigilem/dom/keyboard_event_spec.rb
143
+ - spec/vigilem/dom/utils_spec.rb
144
+ - spec/w3c/dom3/keyboard_event_spec.rb
145
+ - spec/w3c/dom3/ui_event_spec.rb
146
+ - spec/w3c/dom4/event_spec.rb
147
+ homepage: http://rubygems.org/gems/vigilem-dom
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.4.6
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: DOM Structures for Vigilem
171
+ test_files:
172
+ - spec/spec_helper.rb
173
+ - spec/vigilem/dom/keyboard_event_spec.rb
174
+ - spec/vigilem/dom/utils_spec.rb
175
+ - spec/w3c/dom3/keyboard_event_spec.rb
176
+ - spec/w3c/dom3/ui_event_spec.rb
177
+ - spec/w3c/dom4/event_spec.rb