Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Renderable interfaces to module #619

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fixtures/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def view(&block)
end
end

def html_renderable_view(...)
let :view do
Class.new(Phlex::HTML) do
include Phlex::HtmlRenderable

class_eval(...)
end
end
end

def svg_view(&block)
let :view do
Class.new(Phlex::SVG, &block)
Expand Down
49 changes: 3 additions & 46 deletions lib/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,8 @@

module Phlex
# @abstract Subclass and define {#template} to create an HTML component class.
class HTML < SGML
# A list of HTML attributes that have the potential to execute unsafe JavaScript.
EVENT_ATTRIBUTES = %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].to_h { [_1, true] }.freeze

UNBUFFERED_MUTEX = Mutex.new

class << self
# @api private
def __unbuffered_class__
UNBUFFERED_MUTEX.synchronize do
if defined? @unbuffered_class
@unbuffered_class
else
@unbuffered_class = Class.new(Unbuffered)
end
end
end
end

extend Elements
include Helpers, VoidElements, StandardElements

# Output an HTML doctype.
def doctype
@_context.target << "<!DOCTYPE html>"
nil
end

# Outputs an `<svg>` tag
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/SVG/Element/svg
def svg(...)
super do
render Phlex::SVG.new do |svg|
yield(svg)
end
end
end

# @api private
def unbuffered
self.class.__unbuffered_class__.new(self)
end

# This should be extended after all method definitions
extend ElementClobberingGuard
class HTML
include Renderable
include HtmlRenderable
end
end
60 changes: 60 additions & 0 deletions lib/phlex/html_renderable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Phlex
# @abstract Subclass and define {#template} to create an HTML component class.
module HtmlRenderable
include Renderable

def self.included(including_class)
super

including_class.extend(ClassMethods)
including_class.extend(Phlex::Elements)

# This should be extended after all method definitions
including_class.extend(ElementClobberingGuard)
end

# A list of HTML attributes that have the potential to execute unsafe JavaScript.
EVENT_ATTRIBUTES = %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].to_h { [_1, true] }.freeze

UNBUFFERED_MUTEX = Mutex.new

module ClassMethods
include Phlex::Renderable::ClassMethods

# @api private
def __unbuffered_class__
UNBUFFERED_MUTEX.synchronize do
if defined? @unbuffered_class
@unbuffered_class
else
@unbuffered_class = Class.new(Unbuffered)
end
end
end
end

include Phlex::Helpers, Phlex::HTML::VoidElements, Phlex::HTML::StandardElements

# Output an HTML doctype.
def doctype
@_context.target << "<!DOCTYPE html>"
nil
end

# Outputs an `<svg>` tag
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/SVG/Element/svg
def svg(...)
super do
render Phlex::SVG.new do |svg|
yield(svg)
end
end
end

# @api private
def unbuffered
self.class.__unbuffered_class__.new(self)
end
end
end
Loading
Loading