opal-vite 0.2.13 → 0.2.14

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: 2b438a8a413bcf69318c12c10472a9c70ad6bd86c8d2dfe51350d1030709957a
4
- data.tar.gz: 6a411ed3661770d1b704f2b8e1b9881f0e84059b0aaec9d9745d5fb23ac2e4f3
3
+ metadata.gz: 93399be68e49a18a612682b0fe50eb3791fda0a6dc14ddbcd8f9970e5d29be7b
4
+ data.tar.gz: 4153c1d7194c000104493d2be30516e5db36b9231e947b0aa6f72ce4f3930fef
5
5
  SHA512:
6
- metadata.gz: b1c964bc106778120177f21e80b34464163d11eafb699e9ea4925d1637328bb3b51bf1b14e9b869142e22cd361f118e59e452d9cd6627ef7f764369939043ab7
7
- data.tar.gz: 3ddff355186dc4f6b84636ad0d08a363ef21400ecd3db715a0135718681ae274442f99bd8e7598e0413166e3ee4890e68b2a08d29d74e34db38c369b16da7f14
6
+ metadata.gz: e184450fa18a680b8155b2c3190781ef31de310eae209659a493fe601ed25421981b0c777ef44f631daef7c16a50a6065ba8fb6d6c850fa2bc7f4478c5ecf56c
7
+ data.tar.gz: 8dd3d802b5e6f476dec0b402de184b55f55a5fc111af0fe1ca804072205d1108e80a30114ef70f534a8033e3da34faf4b280e4d2a86d5f42fd1e300ec1fccfc7
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Vite
3
- VERSION = "0.2.13"
3
+ VERSION = "0.2.14"
4
4
  end
5
5
  end
@@ -22,13 +22,17 @@ module OpalVite
22
22
  end
23
23
 
24
24
  # Query selector shorthand on element
25
+ # Returns Native-wrapped element or nil
25
26
  def query(selector)
26
- element.query_selector(selector)
27
+ el = element.query_selector(selector)
28
+ el.nil? ? nil : el
27
29
  end
28
30
 
29
31
  # Query selector all shorthand on element
32
+ # Returns Ruby Array of Native-wrapped elements
30
33
  def query_all(selector)
31
- element.query_selector_all(selector)
34
+ node_list = element.query_selector_all(selector)
35
+ Native(`Array.from(#{node_list.to_n})`).to_a.map { |el| Native(el) }
32
36
  end
33
37
 
34
38
  # Add CSS class to element
@@ -101,18 +101,24 @@ module OpalVite
101
101
  # ===================
102
102
 
103
103
  # Query single element
104
+ # Returns Native-wrapped element or nil
104
105
  def query(selector)
105
- `document.querySelector(#{selector})`
106
+ el = `document.querySelector(#{selector})`
107
+ `#{el} === null` ? nil : Native(el)
106
108
  end
107
109
 
108
110
  # Query all elements
111
+ # Returns Ruby Array of Native-wrapped elements
109
112
  def query_all(selector)
110
- `Array.from(document.querySelectorAll(#{selector}))`
113
+ elements = `Array.from(document.querySelectorAll(#{selector}))`
114
+ Native(elements).to_a.map { |el| Native(el) }
111
115
  end
112
116
 
113
117
  # Get element by ID
118
+ # Returns Native-wrapped element or nil
114
119
  def get_element_by_id(id)
115
- `document.getElementById(#{id})`
120
+ el = `document.getElementById(#{id})`
121
+ `#{el} === null` ? nil : Native(el)
116
122
  end
117
123
 
118
124
  # ===================
@@ -491,25 +491,38 @@ module OpalVite
491
491
 
492
492
  # ===== Element Methods =====
493
493
 
494
+ # Convert element to native JavaScript element
495
+ # Handles both raw JS elements and Native-wrapped elements
496
+ # @param element [Native, Object] DOM element (raw or Native-wrapped)
497
+ # @return [Object] Raw JavaScript element
498
+ def to_native_element(element)
499
+ # Check if element is an Opal object with to_n method
500
+ # Use backtick JavaScript to safely check without calling Ruby methods on JS objects
501
+ `typeof #{element}.$to_n === 'function'` ? element.to_n : element
502
+ end
503
+
494
504
  # Add class to element
495
505
  # @param element [Native] DOM element
496
506
  # @param class_name [String] CSS class to add
497
507
  def add_class(element, class_name)
498
- `#{element}.classList.add(#{class_name})`
508
+ el = to_native_element(element)
509
+ `#{el}.classList.add(#{class_name})`
499
510
  end
500
511
 
501
512
  # Remove class from element
502
513
  # @param element [Native] DOM element
503
514
  # @param class_name [String] CSS class to remove
504
515
  def remove_class(element, class_name)
505
- `#{element}.classList.remove(#{class_name})`
516
+ el = to_native_element(element)
517
+ `#{el}.classList.remove(#{class_name})`
506
518
  end
507
519
 
508
520
  # Toggle class on element
509
521
  # @param element [Native] DOM element
510
522
  # @param class_name [String] CSS class to toggle
511
523
  def toggle_class(element, class_name)
512
- `#{element}.classList.toggle(#{class_name})`
524
+ el = to_native_element(element)
525
+ `#{el}.classList.toggle(#{class_name})`
513
526
  end
514
527
 
515
528
  # Check if element has class
@@ -517,7 +530,8 @@ module OpalVite
517
530
  # @param class_name [String] CSS class to check
518
531
  # @return [Boolean]
519
532
  def has_class?(element, class_name)
520
- `#{element}.classList.contains(#{class_name})`
533
+ el = to_native_element(element)
534
+ `#{el}.classList.contains(#{class_name})`
521
535
  end
522
536
 
523
537
  # Set element attribute
@@ -525,7 +539,8 @@ module OpalVite
525
539
  # @param attr [String] Attribute name
526
540
  # @param value [String] Attribute value
527
541
  def set_attr(element, attr, value)
528
- `#{element}.setAttribute(#{attr}, #{value})`
542
+ el = to_native_element(element)
543
+ `#{el}.setAttribute(#{attr}, #{value})`
529
544
  end
530
545
 
531
546
  # Get element attribute
@@ -533,14 +548,16 @@ module OpalVite
533
548
  # @param attr [String] Attribute name
534
549
  # @return [String, nil] Attribute value
535
550
  def get_attr(element, attr)
536
- `#{element}.getAttribute(#{attr})`
551
+ el = to_native_element(element)
552
+ `#{el}.getAttribute(#{attr})`
537
553
  end
538
554
 
539
555
  # Remove element attribute
540
556
  # @param element [Native] DOM element
541
557
  # @param attr [String] Attribute name
542
558
  def remove_attr(element, attr)
543
- `#{element}.removeAttribute(#{attr})`
559
+ el = to_native_element(element)
560
+ `#{el}.removeAttribute(#{attr})`
544
561
  end
545
562
 
546
563
  # Set element style
@@ -548,41 +565,47 @@ module OpalVite
548
565
  # @param property [String] CSS property
549
566
  # @param value [String] CSS value
550
567
  def set_style(element, property, value)
551
- `#{element}.style[#{property}] = #{value}`
568
+ el = to_native_element(element)
569
+ `#{el}.style[#{property}] = #{value}`
552
570
  end
553
571
 
554
572
  # Set element innerHTML
555
573
  # @param element [Native] DOM element
556
574
  # @param html [String] HTML content
557
575
  def set_html(element, html)
558
- `#{element}.innerHTML = #{html}`
576
+ el = to_native_element(element)
577
+ `#{el}.innerHTML = #{html}`
559
578
  end
560
579
 
561
580
  # Set element textContent
562
581
  # @param element [Native] DOM element
563
582
  # @param text [String] Text content
564
583
  def set_text(element, text)
565
- `#{element}.textContent = #{text}`
584
+ el = to_native_element(element)
585
+ `#{el}.textContent = #{text}`
566
586
  end
567
587
 
568
588
  # Get element value (for inputs)
569
589
  # @param element [Native] DOM element
570
590
  # @return [String] Element value
571
591
  def get_value(element)
572
- `#{element}.value`
592
+ el = to_native_element(element)
593
+ `#{el}.value`
573
594
  end
574
595
 
575
596
  # Set element value (for inputs)
576
597
  # @param element [Native] DOM element
577
598
  # @param value [String] Value to set
578
599
  def set_value(element, value)
579
- `#{element}.value = #{value}`
600
+ el = to_native_element(element)
601
+ `#{el}.value = #{value}`
580
602
  end
581
603
 
582
604
  # Focus element
583
605
  # @param element [Native] DOM element
584
606
  def focus(element)
585
- `#{element}.focus()`
607
+ el = to_native_element(element)
608
+ `#{el}.focus()`
586
609
  end
587
610
 
588
611
  # Check if element has attribute
@@ -590,14 +613,15 @@ module OpalVite
590
613
  # @param attr [String] Attribute name
591
614
  # @return [Boolean]
592
615
  def has_attr?(element, attr)
593
- `#{element}.hasAttribute(#{attr})`
616
+ el = to_native_element(element)
617
+ `#{el}.hasAttribute(#{attr})`
594
618
  end
595
619
 
596
620
  # ===== DOM Creation Methods =====
597
621
 
598
622
  # Create a new DOM element
599
623
  # @param tag [String] HTML tag name
600
- # @return [Native] Created element
624
+ # @return [Object] Created element (raw JavaScript)
601
625
  def create_element(tag)
602
626
  `document.createElement(#{tag})`
603
627
  end
@@ -606,34 +630,40 @@ module OpalVite
606
630
  # @param parent [Native] Parent element
607
631
  # @param child [Native] Child element to append
608
632
  def append_child(parent, child)
609
- `#{parent}.appendChild(#{child})`
633
+ p = to_native_element(parent)
634
+ c = to_native_element(child)
635
+ `#{p}.appendChild(#{c})`
610
636
  end
611
637
 
612
638
  # Remove element from DOM
613
639
  # @param element [Native] Element to remove
614
640
  def remove_element(element)
615
- `#{element}.remove()`
641
+ el = to_native_element(element)
642
+ `#{el}.remove()`
616
643
  end
617
644
 
618
645
  # Get next element sibling
619
646
  # @param element [Native] DOM element
620
647
  # @return [Native, nil] Next sibling element
621
648
  def next_sibling(element)
622
- `#{element}.nextElementSibling`
649
+ el = to_native_element(element)
650
+ `#{el}.nextElementSibling`
623
651
  end
624
652
 
625
653
  # Get previous element sibling
626
654
  # @param element [Native] DOM element
627
655
  # @return [Native, nil] Previous sibling element
628
656
  def prev_sibling(element)
629
- `#{element}.previousElementSibling`
657
+ el = to_native_element(element)
658
+ `#{el}.previousElementSibling`
630
659
  end
631
660
 
632
661
  # Get parent element
633
662
  # @param element [Native] DOM element
634
663
  # @return [Native, nil] Parent element
635
664
  def parent(element)
636
- `#{element}.parentElement`
665
+ el = to_native_element(element)
666
+ `#{el}.parentElement`
637
667
  end
638
668
 
639
669
  # ===== DOM Query Methods =====
@@ -642,28 +672,32 @@ module OpalVite
642
672
  # @param selector [String] CSS selector
643
673
  # @return [Native, nil] Element or nil
644
674
  def query(selector)
645
- `document.querySelector(#{selector})`
675
+ el = `document.querySelector(#{selector})`
676
+ `#{el} === null` ? nil : Native(el)
646
677
  end
647
678
 
648
679
  # Query selector all on document
649
680
  # @param selector [String] CSS selector
650
- # @return [Array] Array of elements
681
+ # @return [Array] Ruby Array of Native-wrapped elements
651
682
  def query_all(selector)
652
- `Array.from(document.querySelectorAll(#{selector}))`
683
+ elements = `Array.from(document.querySelectorAll(#{selector}))`
684
+ Native(elements).to_a.map { |el| Native(el) }
653
685
  end
654
686
 
655
687
  # Query selector on controller element
656
688
  # @param selector [String] CSS selector
657
689
  # @return [Native, nil] Element or nil
658
690
  def query_element(selector)
659
- `this.element.querySelector(#{selector})`
691
+ el = `this.element.querySelector(#{selector})`
692
+ `#{el} === null` ? nil : Native(el)
660
693
  end
661
694
 
662
695
  # Query selector all on controller element
663
696
  # @param selector [String] CSS selector
664
- # @return [Array] Array of elements
697
+ # @return [Array] Ruby Array of Native-wrapped elements
665
698
  def query_all_element(selector)
666
- `Array.from(this.element.querySelectorAll(#{selector}))`
699
+ elements = `Array.from(this.element.querySelectorAll(#{selector}))`
700
+ Native(elements).to_a.map { |el| Native(el) }
667
701
  end
668
702
 
669
703
  # ===== Document Methods =====
@@ -161,18 +161,24 @@ module OpalVite
161
161
  # ===================
162
162
 
163
163
  # Query single element
164
+ # Returns Native-wrapped element or nil
164
165
  def query(selector)
165
- `document.querySelector(#{selector})`
166
+ el = `document.querySelector(#{selector})`
167
+ `#{el} === null` ? nil : Native(el)
166
168
  end
167
169
 
168
170
  # Query all elements
171
+ # Returns Ruby Array of Native-wrapped elements
169
172
  def query_all(selector)
170
- `Array.from(document.querySelectorAll(#{selector}))`
173
+ elements = `Array.from(document.querySelectorAll(#{selector}))`
174
+ Native(elements).to_a.map { |el| Native(el) }
171
175
  end
172
176
 
173
177
  # Get element by ID
178
+ # Returns Native-wrapped element or nil
174
179
  def get_element_by_id(id)
175
- `document.getElementById(#{id})`
180
+ el = `document.getElementById(#{id})`
181
+ `#{el} === null` ? nil : Native(el)
176
182
  end
177
183
 
178
184
  # ===================
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-vite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - stofu1234