Skip to content

Commit

Permalink
Lets go (#703)
Browse files Browse the repository at this point in the history
Closes #687 
Closes #632
  • Loading branch information
joeldrapper authored Apr 6, 2024
2 parents 756b006 + a184218 commit 31553f4
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 93 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ jobs:
matrix:
os: ["ubuntu-latest", "macos-latest"]
ruby-version:
- "2.7"
- "3.0"
- "3.1"
- "3.2"
- "3.3"
- "head"
- "truffleruby-22.2"
- "truffleruby-22.3"
- "jruby-9.4.6.0"
- "jruby-head"
- "truffleruby-23.1"
- "truffleruby-24.0"

runs-on: ${{ matrix.os }}
steps:
Expand Down
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ inherit_from:
- "https://www.goodcop.style/tabs.yml"

AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.2

Style/ExplicitBlockArgument:
Enabled: false
Expand All @@ -25,3 +25,9 @@ Naming/MethodName:

Style/ReturnNilInPredicateMethodDefinition:
Enabled: false

Naming/BlockForwarding:
Enabled: false

Style/ArgumentsForwarding:
Enabled: false
2 changes: 1 addition & 1 deletion fixtures/components/say_hi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(name, times: 1)
@times = times
end

def template
def view_template
article {
@times.times { h1 { "Hi #{@name}" } }
yield
Expand Down
10 changes: 1 addition & 9 deletions lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ class NameError < ::NameError
# @api private
ATTRIBUTE_CACHE = {}

SUPPORTS_FIBER_STORAGE = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2")
end

if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
class Symbol
def name
to_s
end
end
SUPPORTS_FIBER_STORAGE = RUBY_ENGINE == "ruby"
end

def 💪
Expand Down
12 changes: 4 additions & 8 deletions lib/phlex/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class Phlex::CSV
include Phlex::Callable

FORMULA_PREFIXES = ["=", "+", "-", "@", "\t", "\r"].to_h { |prefix| [prefix, true] }.freeze
SPACE_CHARACTERS = [" ", "\t", "\r"].to_h { |char| [char, true] }.freeze
FORMULA_PREFIXES = Set["=", "+", "-", "@", "\t", "\r"].freeze
SPACE_CHARACTERS = Set[" ", "\t", "\r"].freeze

def initialize(collection)
@collection = collection
Expand Down Expand Up @@ -92,10 +92,6 @@ def yielder(record)
yield(record)
end

def template(...)
nil
end

# Override and set to `false` to disable rendering headers.
def render_headers?
true
Expand All @@ -120,11 +116,11 @@ def escape(value)
first_char = value[0]
last_char = value[-1]

if escape_csv_injection? && FORMULA_PREFIXES[first_char]
if escape_csv_injection? && FORMULA_PREFIXES.include?(first_char)
# Prefix a single quote to prevent Excel, Google Docs, etc. from interpreting the value as a formula.
# See https://owasp.org/www-community/attacks/CSV_Injection
%("'#{value.gsub('"', '""')}")
elsif (!trim_whitespace? && (SPACE_CHARACTERS[first_char] || SPACE_CHARACTERS[last_char])) || value.include?('"') || value.include?(",") || value.include?("\n")
elsif (!trim_whitespace? && (SPACE_CHARACTERS.include?(first_char) || SPACE_CHARACTERS.include?(last_char))) || value.include?('"') || value.include?(",") || value.include?("\n")
%("#{value.gsub('"', '""')}")
else
value
Expand Down
11 changes: 1 addition & 10 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,11 @@ def #{method_name}(**attributes, &block)
end

# @api private
def register_void_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false)
if deprecated
deprecation = <<~RUBY
Kernel.warn "#{deprecated}"
RUBY
else
deprecation = ""
end

def register_void_element(method_name, tag: method_name.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true
def #{method_name}(**attributes)
#{deprecation}
context = @_context
buffer = context.buffer
fragment = context.fragments
Expand Down
2 changes: 0 additions & 2 deletions lib/phlex/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "set"

module Phlex::Helpers
private

Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HTML < SGML
autoload :VoidElements, "phlex/html/void_elements"

# 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
EVENT_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]).freeze

UNBUFFERED_MUTEX = Mutex.new

Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/html/standard_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ module Phlex::HTML::StandardElements
# @return [nil]
# @yieldparam component [self]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/template
register_element :template_tag, tag: "template"
register_element :template

# @!method textarea(**attributes, &content)
# Outputs a `<textarea>` tag.
Expand Down
6 changes: 0 additions & 6 deletions lib/phlex/html/void_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ module Phlex::HTML::VoidElements
# @see https://developer.mozilla.org/docs/Web/HTML/Element/meta
register_void_element :meta

# @!method param(**attributes, &content)
# Outputs a `<param>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/param
register_void_element :param, deprecated: "⚠️ [DEPRECATION] The <param> tag is deprecated. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/param"

# @!method source(**attributes, &content)
# Outputs a `<source>` tag.
# @return [nil]
Expand Down
5 changes: 0 additions & 5 deletions lib/phlex/kit.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# frozen_string_literal: true

module Phlex::Kit
def self.extended(mod)
warn "⚠️ [WARNING] Phlex::Kit is experimental and may be removed from future versions of Phlex."
super
end

# When a kit is included in a module, we need to load all of its components.
def included(mod)
constants.each { |c| const_get(c) if autoload?(c) }
Expand Down
24 changes: 7 additions & 17 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call(...)
def new(*args, **kwargs, &block)
if block
object = super(*args, **kwargs, &nil)
object.instance_variable_set(:@_content_block, block)
object.instance_exec { @_content_block = block }
object
else
super
Expand Down Expand Up @@ -65,18 +65,8 @@ def element_method?(method_name)
# def view_template(&block)
# article(class: "card", &block)
# end
def template
yield
end

def self.method_added(method_name)
if method_name == :template
Kernel.warn "⚠️ [DEPRECATION] Defining the `template` method on a Phlex component will not be supported in Phlex 2.0. Please rename the method to `view_template` instead."
end
end

def view_template(&block)
template(&block)
def view_template
yield if block_given?
end

def await(task)
Expand Down Expand Up @@ -105,8 +95,8 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil,
@_context = context
@_view_context = view_context
@_parent = parent

if fragments
warn "⚠️ [WARNING] Selective Rendering is experimental, incomplete, and may change in future versions."
@_context.target_fragments(fragments)
end

Expand Down Expand Up @@ -407,7 +397,7 @@ def __final_attributes__(**attributes)
end

buffer = +""
__build_attributes__(attributes, buffer: buffer)
__build_attributes__(attributes, buffer:)

buffer
end
Expand All @@ -427,7 +417,7 @@ def __build_attributes__(attributes, buffer:)
next if lower_name == "href" && v.start_with?(/\s*javascript:/i)

# Detect unsafe attribute names. Attribute names are considered unsafe if they match an event attribute or include unsafe characters.
if HTML::EVENT_ATTRIBUTES[lower_name] || name.match?(/[<>&"']/)
if HTML::EVENT_ATTRIBUTES.include?(lower_name) || name.match?(/[<>&"']/)
raise ArgumentError, "Unsafe attribute name detected: #{k}."
end

Expand All @@ -447,7 +437,7 @@ def __build_attributes__(attributes, buffer:)
when Symbol then"#{name}-#{subkey.name.tr('_', '-')}"
else "#{name}-#{subkey}"
end
}, buffer: buffer
}, buffer:
)
when Array
buffer << " " << name << '="' << Phlex::Escape.html_escape(v.compact.join(" ")) << '"'
Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/testing/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def render(view, &block)
view = view.new
end

view.call(view_context: view_context, &block)
view.call(view_context:, &block)
end

def view_context
Expand Down
2 changes: 1 addition & 1 deletion phlex.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
spec.description = "A high-performance view framework optimised for fun."
spec.homepage = "https://www.phlex.fun"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7"
spec.required_ruby_version = ">= 3.2"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/phlex-ruby/phlex"
Expand Down
4 changes: 2 additions & 2 deletions test/phlex/kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
class Example < Phlex::HTML
include Components

def template
def view_template
SayHi("Joel", times: 2) { "Inside" }
Components::SayHi("Will", times: 1) { "Inside" }
end
end

# This feature is only supported in Ruby 3.2 or later.
if RUBY_VERSION >= "3.2"
if Phlex::SUPPORTS_FIBER_STORAGE
describe Phlex::Kit do
it "raises when you try to render a component outside of a rendering context" do
expect { Components::SayHi() }.to raise_exception(RuntimeError)
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def view_template
def view_template
srcdoc = capture { yield } if block_given?

iframe srcdoc: srcdoc
iframe srcdoc:
end
end
end
Expand Down
17 changes: 0 additions & 17 deletions test/phlex/view/legacy_template_method.rb

This file was deleted.

6 changes: 3 additions & 3 deletions test/phlex/view/naughty_business.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

with "naughty javascript links" do
view do
def template
def view_template
a(href: "javascript:alert(1)") { "a" }
a(href: "JAVASCRIPT:alert(1)") { "b" }
a(href: :"JAVASCRIPT:alert(1)") { "c" }
Expand All @@ -20,7 +20,7 @@ def template

with "naughty uppercase event tag" do
view do
def template
def view_template
button ONCLICK: "ALERT(1)" do
"naughty button"
end
Expand Down Expand Up @@ -85,7 +85,7 @@ def view_template
end
end

Phlex::HTML::EVENT_ATTRIBUTES.each_key do |event_attribute|
Phlex::HTML::EVENT_ATTRIBUTES.each do |event_attribute|
with "with naughty #{event_attribute} attribute" do
naughty_attributes = { event_attribute => "alert(1);" }

Expand Down

0 comments on commit 31553f4

Please sign in to comment.