From 61ee14cc3e73aa2c0837382ebdf434a4d1fff3e4 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sun, 18 Feb 2024 16:48:00 +0000 Subject: [PATCH] Remove support for `process_attributes` --- CHANGELOG.md | 3 +++ lib/phlex/elements.rb | 6 +++--- lib/phlex/sgml.rb | 26 ++++++++++++-------------- test/phlex/view/process_attributes.rb | 19 ------------------- 4 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 test/phlex/view/process_attributes.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index d041bf63..a83aa5fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +- [Breaking] Removed support for `process_attributes`. Instead, override `__attributes__`, calling `super` with your processed attributes. `__attributes__` receives a single positional argument which is the original attributes hash. + ## [1.9.0] 2024-11-24 - Improved documentation diff --git a/lib/phlex/elements.rb b/lib/phlex/elements.rb index 5093fa8d..824523bd 100644 --- a/lib/phlex/elements.rb +++ b/lib/phlex/elements.rb @@ -39,11 +39,11 @@ def #{method_name}(**attributes, &block) if attributes.length > 0 # with attributes if block # with content block - target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(**attributes)) << ">" + target << "<#{tag}" << __attributes__(attributes) << ">" yield_content(&block) target << "" else # without content block - target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(**attributes)) << ">" + target << "<#{tag}" << __attributes__(attributes) << ">" end else # without attributes if block # with content block @@ -77,7 +77,7 @@ def #{method_name}(**attributes) target = @_context.target if attributes.length > 0 # with attributes - target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(**attributes)) << ">" + target << "<#{tag}" << __attributes__(attributes) << ">" else # without attributes target << "<#{tag}>" end diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 9fe5f073..1c4e5009 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -348,23 +348,21 @@ def __text__(content) end # @api private - def __attributes__(**attributes) - if respond_to?(:process_attributes) - attributes = process_attributes(**attributes) - end - - if attributes[:href]&.start_with?(/\s*javascript:/) - attributes.delete(:href) - end + def __attributes__(attributes) + Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= ( + if attributes[:href]&.start_with?(/\s*javascript:/) + attributes.delete(:href) + end - if attributes["href"]&.start_with?(/\s*javascript:/) - attributes.delete("href") - end + if attributes["href"]&.start_with?(/\s*javascript:/) + attributes.delete("href") + end - buffer = +"" - __build_attributes__(attributes, buffer: buffer) + buffer = +"" + __build_attributes__(attributes, buffer: buffer) - buffer + buffer + ) end # @api private diff --git a/test/phlex/view/process_attributes.rb b/test/phlex/view/process_attributes.rb deleted file mode 100644 index aec6b761..00000000 --- a/test/phlex/view/process_attributes.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -describe Phlex::HTML do - extend ViewHelper - - view do - def view_template - div(class: "foo") - end - - def process_attributes(**attributes) - attributes.transform_values { |v| "#{v}-bar" } - end - end - - it "works" do - expect(output).to be == %(
) - end -end