Sha256: 03401f6313cf078e43a805496435063ab6f9d698364437d32e15b2a8f415be6b

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Phlex
	class Compiler
		module Generators
			class Element
				def initialize(node, formatter:, mutating: false)
					@node = node
					@formatter = formatter
					@mutating = mutating
				end

				def call
					@formatter.append do |f|
						f.text "<"
						f.text tag
					end

					if @node.arguments&.parts&.any?
						@formatter.chain_append do |f|
							f.text "_attributes("
							@node.arguments.format(@formatter)
							f.text ")"
						end
					end

					@formatter.append do |f|
						f.text ">"
					end

					return if void?

					case @node.content
					in SyntaxTree::Statements[body: [c]]
						Content.new(@formatter, content: c, mutating: @mutating).call
					in nil
						nil
					else
						@node.content.format(@formatter)
					end

					@formatter.append do |f|
						f.text "</"
						f.text tag
						f.text ">"
					end
				end

				private

				def tag
					HTML::STANDARD_ELEMENTS[@node.name] || HTML::VOID_ELEMENTS[@node.name]
				end

				def void?
					HTML::VOID_ELEMENTS.key?(@node.name)
				end
			end
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phlex-0.5.3 lib/phlex/compiler/generators/element.rb
phlex-0.5.2 lib/phlex/compiler/generators/element.rb
phlex-0.5.1 lib/phlex/compiler/generators/element.rb
phlex-0.5.0 lib/phlex/compiler/generators/element.rb