phlex 2.0.0.beta1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,71 +2,109 @@
2
2
 
3
3
  # Void HTML elements don't accept content and never have a closing tag.
4
4
  module Phlex::HTML::VoidElements
5
- extend Phlex::Elements
5
+ extend Phlex::SGML::Elements
6
6
 
7
- # @!method area(**attributes, &content)
8
- # Outputs an `<area>` tag.
9
- # @return [nil]
10
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/area
11
- register_void_element :area
7
+ # Outputs an `<area>` tag.
8
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/area
9
+ __register_void_element__ def area(
10
+ class: nil,
11
+ id: nil,
12
+ **attributes
13
+ ) = nil
12
14
 
13
- # @!method br(**attributes, &content)
14
- # Outputs a `<br>` tag.
15
- # @return [nil]
16
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/br
17
- register_void_element :br
15
+ # Outputs a `<base>` tag.
16
+ # See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
17
+ __register_void_element__ def base(
18
+ class: nil,
19
+ href: nil,
20
+ id: nil,
21
+ target: nil,
22
+ **attributes
23
+ ) = nil
18
24
 
19
- # @!method col(**attributes, &content)
20
- # Outputs a `<col>` tag.
21
- # @return [nil]
22
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/col
23
- register_void_element :col
25
+ # Outputs a `<br>` tag.
26
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/br
27
+ __register_void_element__ def br(
28
+ class: nil,
29
+ id: nil,
30
+ **attributes
31
+ ) = nil
24
32
 
25
- # @!method embed(**attributes, &content)
26
- # Outputs an `<embed>` tag.
27
- # @return [nil]
28
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/embed
29
- register_void_element :embed
33
+ # Outputs a `<col>` tag.
34
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/col
35
+ __register_void_element__ def col(
36
+ class: nil,
37
+ id: nil,
38
+ **attributes
39
+ ) = nil
30
40
 
31
- # @!method hr(**attributes, &content)
32
- # Outputs an `<hr>` tag.
33
- # @return [nil]
34
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/hr
35
- register_void_element :hr
41
+ # Outputs an `<embed>` tag.
42
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/embed
43
+ __register_void_element__ def embed(
44
+ class: nil,
45
+ id: nil,
46
+ **attributes
47
+ ) = nil
36
48
 
37
- # @!method img(**attributes, &content)
38
- # Outputs an `<img>` tag.
39
- # @return [nil]
40
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/img
41
- register_void_element :img
49
+ # Outputs an `<hr>` tag.
50
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/hr
51
+ __register_void_element__ def hr(
52
+ class: nil,
53
+ id: nil,
54
+ **attributes
55
+ ) = nil
42
56
 
43
- # @!method input(**attributes, &content)
44
- # Outputs an `<input>` tag.
45
- # @return [nil]
46
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/input
47
- register_void_element :input
57
+ # Outputs an `<img>` tag.
58
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/img
59
+ __register_void_element__ def img(
60
+ alt: nil,
61
+ class: nil,
62
+ id: nil,
63
+ src: nil,
64
+ **attributes
65
+ ) = nil
48
66
 
49
- # @!method link(**attributes, &content)
50
- # Outputs a `<link>` tag.
51
- # @return [nil]
52
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/link
53
- register_void_element :link
67
+ # Outputs an `<input>` tag.
68
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/input
69
+ __register_void_element__ def input(
70
+ class: nil,
71
+ id: nil,
72
+ name: nil,
73
+ type: nil,
74
+ **attributes
75
+ ) = nil
54
76
 
55
- # @!method meta(**attributes, &content)
56
- # Outputs a `<meta>` tag.
57
- # @return [nil]
58
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/meta
59
- register_void_element :meta
77
+ # Outputs a `<link>` tag.
78
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/link
79
+ __register_void_element__ def link(
80
+ class: nil,
81
+ id: nil,
82
+ **attributes
83
+ ) = nil
60
84
 
61
- # @!method source(**attributes, &content)
62
- # Outputs a `<source>` tag.
63
- # @return [nil]
64
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/source
65
- register_void_element :source
85
+ # Outputs a `<meta>` tag.
86
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/meta
87
+ __register_void_element__ def meta(
88
+ charset: nil,
89
+ class: nil,
90
+ id: nil,
91
+ name: nil,
92
+ **attributes
93
+ ) = nil
66
94
 
67
- # @!method track(**attributes, &content)
68
- # Outputs a `<track>` tag.
69
- # @return [nil]
70
- # @see https://developer.mozilla.org/docs/Web/HTML/Element/track
71
- register_void_element :track
95
+ # Outputs a `<source>` tag.
96
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/source
97
+ __register_void_element__ def source(
98
+ class: nil,
99
+ id: nil,
100
+ **attributes
101
+ ) = nil
102
+
103
+ # Outputs a `<track>` tag.
104
+ # See https://developer.mozilla.org/docs/Web/HTML/Element/track
105
+ __register_void_element__ def track(
106
+ class: nil,
107
+ id: nil,
108
+ **attributes
109
+ ) = nil
72
110
  end
data/lib/phlex/html.rb CHANGED
@@ -4,10 +4,7 @@ class Phlex::HTML < Phlex::SGML
4
4
  autoload :StandardElements, "phlex/html/standard_elements"
5
5
  autoload :VoidElements, "phlex/html/void_elements"
6
6
 
7
- # A list of HTML attributes that have the potential to execute unsafe JavaScript.
8
- UNSAFE_ATTRIBUTES = Set.new(%w[onabort onafterprint onbeforeprint onbeforeunload onblur oncanplay oncanplaythrough onchange onclick oncontextmenu oncopy oncuechange oncut ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop ondurationchange onemptied onended onerror onfocus onhashchange oninput oninvalid onkeydown onkeypress onkeyup onload onloadeddata onloadedmetadata onloadstart onmessage onmousedown onmousemove onmouseout onmouseover onmouseup onmousewheel onoffline ononline onpagehide onpageshow onpaste onpause onplay onplaying onpopstate onprogress onratechange onreset onresize onscroll onsearch onseeked onseeking onselect onstalled onstorage onsubmit onsuspend ontimeupdate ontoggle onunload onvolumechange onwaiting onwheel srcdoc]).freeze
9
-
10
- extend Phlex::Elements
7
+ extend Phlex::SGML::Elements
11
8
  include VoidElements, StandardElements
12
9
 
13
10
  # Output an HTML doctype.
@@ -41,7 +38,4 @@ class Phlex::HTML < Phlex::SGML
41
38
  def content_type
42
39
  "text/html"
43
40
  end
44
-
45
- # This should be extended after all method definitions
46
- extend Phlex::ElementClobberingGuard
47
41
  end
data/lib/phlex/kit.rb CHANGED
@@ -3,7 +3,9 @@
3
3
  module Phlex::Kit
4
4
  module LazyLoader
5
5
  def method_missing(name, ...)
6
- if name[0] == name[0].upcase && __phlex_kit_constants__.include?(name) && __get_phlex_kit_constant__(name) && methods.include?(name)
6
+ mod = self.class
7
+
8
+ if name[0] == name[0].upcase && mod.constants.include?(name) && mod.const_get(name) && methods.include?(name)
7
9
  public_send(name, ...)
8
10
  else
9
11
  super
@@ -11,7 +13,9 @@ module Phlex::Kit
11
13
  end
12
14
 
13
15
  def respond_to_missing?(name, include_private = false)
14
- if name[0] == name[0].upcase && __phlex_kit_constants__.include?(name) && __get_phlex_kit_constant__(name) && methods.include?(name)
16
+ mod = self.class
17
+
18
+ if name[0] == name[0].upcase && mod.constants.include?(name) && mod.const_get(name) && methods.include?(name)
15
19
  true
16
20
  else
17
21
  super
@@ -19,20 +23,24 @@ module Phlex::Kit
19
23
  end
20
24
  end
21
25
 
22
- include LazyLoader
23
-
24
26
  def self.extended(mod)
25
27
  mod.include(LazyLoader)
26
- mod.define_method(:__phlex_kit_constants__) { mod.__phlex_kit_constants__ }
27
- mod.define_method(:__get_phlex_kit_constant__) { |name| mod.__get_phlex_kit_constant__(name) }
28
28
  end
29
29
 
30
- def __phlex_kit_constants__
31
- constants
30
+ def method_missing(name, ...)
31
+ if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
32
+ public_send(name, ...)
33
+ else
34
+ super
35
+ end
32
36
  end
33
37
 
34
- def __get_phlex_kit_constant__(name)
35
- const_get(name)
38
+ def respond_to_missing?(name, include_private = false)
39
+ if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
40
+ true
41
+ else
42
+ super
43
+ end
36
44
  end
37
45
 
38
46
  def const_added(name)
@@ -44,14 +52,18 @@ module Phlex::Kit
44
52
  if Class === constant && constant < Phlex::SGML
45
53
  constant.include(self)
46
54
 
55
+ constant = nil
56
+
47
57
  define_method(name) do |*args, **kwargs, &block|
48
58
  constant = me.const_get(name)
49
59
  render(constant.new(*args, **kwargs), &block)
50
60
  end
51
61
 
52
62
  define_singleton_method(name) do |*args, **kwargs, &block|
53
- if (component = Fiber[:__phlex_component__])
63
+ component, fiber_id = Thread.current[:__phlex_component__]
64
+ if (component && fiber_id == Fiber.current.object_id)
54
65
  component.instance_exec do
66
+ constant = me.const_get(name)
55
67
  render(constant.new(*args, **kwargs), &block)
56
68
  end
57
69
  else
@@ -1,33 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Extending this module provides the {register_element} macro for registering your own custom elements. It's already extended by {HTML} and {SVG}.
4
- # @example
5
- # module MyCustomElements
6
- # extend Phlex::Elements
7
- #
8
- # register_element :trix_editor
9
- # end
10
- #
11
- # class MyComponent < Phlex::HTML
12
- # include MyCustomElements
13
- #
14
- # def view_template
15
- # trix_editor
16
- # end
17
- # end
18
- module Phlex::Elements
19
- # @api private
20
- def registered_elements
21
- @registered_elements ||= {}
3
+ module Phlex::SGML::Elements
4
+ def __registered_elements__
5
+ @__registered_elements__ ||= {}
22
6
  end
23
7
 
24
- # Register a custom element. This macro defines an element method for the current class and descendents only. There is no global element registry.
25
- # @param method_name [Symbol]
26
- # @param tag [String] the name of the tag, otherwise this will be the method name with underscores replaced with dashes.
27
- # @return [Symbol] the name of the method created
28
- # @note The methods defined by this macro depend on other methods from {SGML} so they should always be mixed into an {HTML} or {SVG} component.
29
- # @example Register the custom element `<trix-editor>`
30
- # register_element :trix_editor
31
8
  def register_element(method_name, tag: method_name.name.tr("_", "-"))
32
9
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
33
10
  # frozen_string_literal: true
@@ -63,24 +40,24 @@ module Phlex::Elements
63
40
  content = yield(self)
64
41
  if original_length == buffer.bytesize
65
42
  case content
43
+ when ::Phlex::SGML::SafeObject
44
+ buffer << content.to_s
66
45
  when String
67
- buffer << Phlex::Escape.html_escape(content)
46
+ buffer << ::Phlex::Escape.html_escape(content)
68
47
  when Symbol
69
- buffer << Phlex::Escape.html_escape(content.name)
48
+ buffer << ::Phlex::Escape.html_escape(content.name)
70
49
  when nil
71
50
  nil
72
- when Phlex::SGML::SafeObject
73
- buffer << content.to_s
74
51
  else
75
52
  if (formatted_object = format_object(content))
76
- buffer << Phlex::Escape.html_escape(formatted_object)
53
+ buffer << ::Phlex::Escape.html_escape(formatted_object)
77
54
  end
78
55
  end
79
56
  end
80
57
 
81
58
  buffer << "</#{tag}>"
82
59
  else # without content
83
- buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
60
+ buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
84
61
  end
85
62
  else # without attributes
86
63
  if block_given # with content block
@@ -90,17 +67,17 @@ module Phlex::Elements
90
67
  content = yield(self)
91
68
  if original_length == buffer.bytesize
92
69
  case content
70
+ when ::Phlex::SGML::SafeObject
71
+ buffer << content.to_s
93
72
  when String
94
- buffer << Phlex::Escape.html_escape(content)
73
+ buffer << ::Phlex::Escape.html_escape(content)
95
74
  when Symbol
96
- buffer << Phlex::Escape.html_escape(content.name)
75
+ buffer << ::Phlex::Escape.html_escape(content.name)
97
76
  when nil
98
77
  nil
99
- when Phlex::SGML::SafeObject
100
- buffer << content.to_s
101
78
  else
102
79
  if (formatted_object = format_object(content))
103
- buffer << Phlex::Escape.html_escape(formatted_object)
80
+ buffer << ::Phlex::Escape.html_escape(formatted_object)
104
81
  end
105
82
  end
106
83
  end
@@ -117,17 +94,14 @@ module Phlex::Elements
117
94
 
118
95
  nil
119
96
  end
120
-
121
- alias_method :_#{method_name}, :#{method_name}
122
97
  RUBY
123
98
 
124
- registered_elements[method_name] = tag
99
+ __registered_elements__[method_name] = tag
125
100
 
126
101
  method_name
127
102
  end
128
103
 
129
- # @api private
130
- def register_void_element(method_name, tag: method_name.name.tr("_", "-"))
104
+ def __register_void_element__(method_name, tag: method_name.name.tr("_", "-"))
131
105
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
132
106
  # frozen_string_literal: true
133
107
 
@@ -152,7 +126,7 @@ module Phlex::Elements
152
126
  end
153
127
 
154
128
  if attributes.length > 0 # with attributes
155
- buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
129
+ buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
156
130
  else # without attributes
157
131
  buffer << "<#{tag}>"
158
132
  end
@@ -165,7 +139,7 @@ module Phlex::Elements
165
139
  alias_method :_#{method_name}, :#{method_name}
166
140
  RUBY
167
141
 
168
- registered_elements[method_name] = tag
142
+ __registered_elements__[method_name] = tag
169
143
 
170
144
  method_name
171
145
  end