Class: Ruact::HtmlConverter
- Inherits:
-
Object
- Object
- Ruact::HtmlConverter
- Defined in:
- lib/ruact/html_converter.rb
Overview
Converts an HTML string (ERB output) into a ReactElement tree.
Rules:
-
HTML attributes → React equivalents (class→className, for→htmlFor, etc.)
-
data-react-key=“x” → becomes the React key on the element
-
HTML comments matching RUACT_N tokens → replaced by client component refs
-
Text nodes → plain Ruby strings
-
Multiple root nodes → wrapped in a Fragment (array)
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- HTML_TO_REACT =
HTML attribute → React prop name mapping
{ "class" => "className", "for" => "htmlFor", "tabindex" => "tabIndex", "readonly" => "readOnly", "maxlength" => "maxLength", "cellpadding" => "cellPadding", "cellspacing" => "cellSpacing", "rowspan" => "rowSpan", "colspan" => "colSpan", "crossorigin" => "crossOrigin", "autocomplete" => "autoComplete", "autofocus" => "autoFocus", "accesskey" => "accessKey", "contenteditable" => "contentEditable", "enctype" => "encType", "formaction" => "formAction", "novalidate" => "noValidate", "spellcheck" => "spellCheck" }.freeze
- DEFAULT_VALUE_TAGS =
Tags whose ‘value` attribute maps to React’s ‘defaultValue` (uncontrolled).
%w[textarea select].freeze
- INPUT_BUTTON_TYPES =
<input type=…> values for which ‘value` keeps its name in React (button-like inputs).
%w[submit reset button image].freeze
Class Method Summary collapse
-
.convert(html, component_registry = []) ⇒ Object
Convert an HTML string into a ReactElement tree.
Instance Method Summary collapse
-
#convert(html) ⇒ Object
Convert an HTML string into a ReactElement tree (instance form).
-
#initialize(component_registry) ⇒ HtmlConverter
constructor
A new instance of HtmlConverter.
Constructor Details
#initialize(component_registry) ⇒ HtmlConverter
Returns a new instance of HtmlConverter.
54 55 56 |
# File 'lib/ruact/html_converter.rb', line 54 def initialize(component_registry) @registry = component_registry end |
Class Method Details
.convert(html, component_registry = []) ⇒ Object
Convert an HTML string into a ReactElement tree. component_registry is an array of { token:, name:, ref: ClientReference, props: Hash }
Raises Ruact::HtmlConverterError if html is not a String. The validation runs before Nokogiri is invoked, so the caller’s file:line appears at the top of the backtrace rather than Nokogiri internals.
50 51 52 |
# File 'lib/ruact/html_converter.rb', line 50 def self.convert(html, component_registry = []) new(component_registry).convert(html) end |
Instance Method Details
#convert(html) ⇒ Object
Convert an HTML string into a ReactElement tree (instance form).
Raises Ruact::HtmlConverterError if html is not a String. The validation runs before Nokogiri is invoked, so the caller’s file:line appears at the top of the backtrace rather than Nokogiri internals.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruact/html_converter.rb', line 63 def convert(html) validate_html_input!(html) # Wrap in a fragment container so Nokogiri gives us a consistent root. # Use HTML4 fragment parser (universally available, no libgumbo needed). doc = Nokogiri::HTML::DocumentFragment.parse(html) children = doc.children.reject { |n| ignorable?(n) }.filter_map { |n| convert_node(n) } case children.length when 0 then nil when 1 then children.first else children # Fragment: array of elements end end |