### raw/1 examples Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html IEx examples showing how raw/1 marks content as raw. ```elixir iex> raw("") {:safe, ""} iex> raw({:safe, ""}) {:safe, ""} iex> raw(nil) {:safe, ""} ``` -------------------------------- ### css_escape/1 examples Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html IEx examples showing how css_escape/1 escapes strings for CSS identifiers. ```elixir iex> css_escape("hello world") "hello\\ world" iex> css_escape("-123") "-\\31 23" ``` -------------------------------- ### html_escape/1 examples Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html IEx examples showing how html_escape/1 escapes HTML entities in various inputs. ```elixir iex> html_escape("") {:safe, [[[] | "<"], "hello" | ">"]} iex> html_escape(~c"") {:safe, ["<", 104, 101, 108, 108, 111, ">"]} iex> html_escape(1) {:safe, "1"} iex> html_escape({:safe, ""}) {:safe, ""} ``` -------------------------------- ### Raw HTML output example Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Shows how to use the raw function to output unescaped HTML content. ```html <%= raw "" %> ``` -------------------------------- ### input_name/2 spec and example Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec and IEx example for input_name/2, which returns the name of a form field. ```elixir @spec input_name(t() | atom(), field()) :: String.t() ``` ```elixir iex> Phoenix.HTML.Form.input_name(:user, :first_name) "user[first_name]" ``` -------------------------------- ### javascript_escape/1 usage example Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Example showing how to use javascript_escape/1 in a JavaScript template to escape rendered HTML. ```elixir $("#container").append("<%= javascript_escape(render("post.html", post: @post)) %>"); ``` -------------------------------- ### HTML escaping example Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Shows that by default, data output in templates is escaped, so '' is displayed as '<hello>'. ```html <%= "" %> ``` ```html <hello> ``` -------------------------------- ### attributes_escape/1 examples Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Shows how attributes_escape/1 renders attributes, including dasherization of atom keys, handling of :data and :phx keyword lists, and boolean attributes. ```elixir iex> safe_to_string attributes_escape(title: "the title", id: "the id", selected: true) " title=\"the title\" id=\"the id\" selected" ``` ```elixir iex> safe_to_string attributes_escape(%{data: [confirm: "Are you sure?"]}) " data-confirm=\"Are you sure?\"" ``` ```elixir iex> safe_to_string attributes_escape(%{phx: [value: [foo: "bar"]]}) " phx-value-foo=\"bar\"" ``` -------------------------------- ### Override default confirmation behavior Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Example of hooking into the 'phoenix.link.click' event to introduce custom confirmation behavior using data-prompt attributes. ```javascript window.addEventListener('phoenix.link.click', function (e) { // Introduce custom behaviour var message = e.target.getAttribute("data-prompt"); var answer = e.target.getAttribute("data-prompt-answer"); if(message && answer && (answer != window.prompt(message))) { e.preventDefault(); } }, false); ``` -------------------------------- ### %Phoenix.HTML.Form{} Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Defines the Phoenix.HTML.Form struct and documents each of its fields. ```APIDOC ## %Phoenix.HTML.Form{} ### Description Defines the Phoenix.HTML.Form struct. This struct is used to represent form data in Phoenix applications. ### Struct Fields - `:source` - the data structure that implements the form data protocol - `:action` - The action that was taken against the form. This value can be used to distinguish between different operations such as the user typing into a form for validation, or submitting a form for a database insert. - `:impl` - the module with the form data protocol implementation. This is used to avoid multiple protocol dispatches. - `:id` - the id to be used when generating input fields - `:index` - the index of the struct in the form - `:name` - the name to be used when generating input fields - `:data` - the field used to store lookup data - `:params` - the parameters associated with this form - `:hidden` - a keyword list of fields that are required to submit the form behind the scenes as hidden inputs - `:options` - a copy of the options given when creating the form without any form data specific key - `:errors` - a keyword list of errors that are associated with the form ``` -------------------------------- ### %Phoenix.HTML.Form{} Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Defines the Phoenix.HTML.Form struct. ```APIDOC ## %Phoenix.HTML.Form{} ### Description Defines the Phoenix.HTML.Form struct. ### Struct `%Phoenix.HTML.Form{}` ``` -------------------------------- ### options_for_select with keyword list and single selected value Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Generates option elements from a keyword list, marking the option with value "admin" as selected. ```elixir options_for_select(["Admin": "admin", "User": "user"], "admin") #=> #=> ``` -------------------------------- ### options_for_select with horizontal separators Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Shows how to add horizontal separators using the :hr atom in both a simple list and a keyword list. ```elixir options_for_select(["Admin", "User", :hr, "New"], nil) #=> #=> #=>
#=> ``` ```elixir options_for_select(["Admin": "admin", "User": "user", hr: nil, "New": "new"], nil) #=> #=> #=>
#=> ``` -------------------------------- ### options_for_select with option groups Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Generates an optgroup with nested options when the options list contains a keyword with a list value. ```elixir options_for_select(["Europe": ["UK", "Sweden", "France"], ...], nil) #=> #=> #=> #=> #=> ``` -------------------------------- ### options_for_select(options, selected_values, extra \\ []) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns options to be used inside a select element. Supports keyword lists, two-item tuples, and simple values, with optional optgroups, custom tags, and horizontal separators. ```APIDOC ## options_for_select(options, selected_values, extra \\ []) ### Description Returns options to be used inside a select element. `options` is an enumerable used to generate each `option` element. Supports keyword lists with `:key` and `:value` keys, two-item tuples (label, value), and simple values used as both label and value. If `options` is a map or keyword list where the first element is a string, atom, or integer and the second element is a list or map, it generates an `` with nested options. Custom tags can be specified via the `:tag` option, and horizontal separators can be added using `:hr`. ### Parameters - `options` (enumerable) - Required - The enumerable of options to generate. - `selected_values` (any) - Required - The value(s) to mark as selected. Can be a single value or a list. - `extra` (keyword list) - Optional - Additional options, e.g., `tag: "opt"` for custom tags. ### Returns - A list of option tags (and optional optgroup/separator tags) as iodata. ### Examples ``` options_for_select(["Admin": "admin", "User": "user"], "admin") #=> #=> ``` Multiple selected values: ``` options_for_select(["Admin": "admin", "User": "user", "Moderator": "moderator"], ["admin", "moderator"]) #=> #=> #=> ``` Groups: ``` options_for_select(["Europe": ["UK", "Sweden", "France"], ...], nil) #=> #=> #=> #=> #=> ``` Custom option tags: ``` options_for_select(["Admin": "admin", "User": "user"], nil, tag: "opt") #=> Admin #=> User ``` Horizontal separators: ``` options_for_select(["Admin", "User", :hr, "New"], nil) #=> #=> #=>
#=> options_for_select(["Admin": "admin", "User": "user", hr: nil, "New": "new"], nil) #=> #=> #=>
#=> ``` ``` -------------------------------- ### options_for_select with custom tag Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Generates option elements using a custom tag "opt" instead of the default "option". ```elixir options_for_select(["Admin": "admin", "User": "user"], nil, tag: "opt") #=> Admin #=> User ``` -------------------------------- ### options_for_select(options, selected_values, extra \\ []) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns options to be used inside a select element. ```APIDOC ## options_for_select(options, selected_values, extra \\ []) ### Description Returns options to be used inside a select element. ### Function Signature `options_for_select(options, selected_values, extra \\ [])` ``` -------------------------------- ### raw/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Marks the given content as raw, preventing HTML escaping. ```APIDOC ## raw(value) ### Description Marks the given content as raw, so it will not be HTML escaped. ### Parameters - **value** (term) - Required - The content to mark as raw. ### Returns A safe term representing the raw content. ``` -------------------------------- ### input_value(form, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the value of a corresponding form field. The form should either be a Phoenix.HTML.Form or an atom. The field is either a string or an atom. If the field is given as an atom, it will attempt to look data with atom keys. If a string, it will look data with string keys. When a form is given, it will look for changes, then fallback to parameters, and finally fallback to the default struct/map value. ```APIDOC ## input_value(form, field) ### Description Returns a value of a corresponding form field. The `form` should either be a `Phoenix.HTML.Form` or an atom. The field is either a string or an atom. If the field is given as an atom, it will attempt to look data with atom keys. If a string, it will look data with string keys. When a form is given, it will look for changes, then fallback to parameters, and finally fallback to the default struct/map value. Since the function looks up parameter values too, there is no guarantee that the value will have a certain type. For example, a boolean field will be sent as "false" as a parameter, and this function will return it as is. If you need to normalize the result of `input_value`, see `normalize_value/2`. ### Spec ```elixir @spec input_value(t() | atom(), field()) :: term() ``` ### Parameters - `form` (t() | atom()) - Required - The form struct or an atom representing the form name. - `field` (field()) - Required - The field name. ### Returns - (term()) - The value of the field. ``` -------------------------------- ### raw/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Marks the given content as raw, meaning any HTML code inside the given string won't be escaped. This is useful when you trust the content and want to render it as-is. ```APIDOC ## raw/1 ### Description Marks the given content as raw. This means any HTML code inside the given string won't be escaped. ### Function Signature ```elixir @spec raw(iodata() | safe() | nil) :: safe() ``` ### Parameters - **value** (iodata() | safe() | nil) - The content to mark as raw. ### Return Value - **safe()** - A tuple `{:safe, value}`. ### Examples ```elixir iex> raw("") {:safe, ""} iex> raw({:safe, ""}) {:safe, ""} iex> raw(nil) {:safe, ""} ``` ``` -------------------------------- ### to_iodata(data) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Safe.html Converts the given data to an HTML-safe iodata representation. This function is part of the Phoenix.HTML.Safe protocol and must be implemented by data structures to ensure HTML safety. ```APIDOC ## to_iodata(data) ### Description Converts the given data to an HTML-safe iodata representation. This function is part of the Phoenix.HTML.Safe protocol and must be implemented by data structures to ensure HTML safety. ### Function Signature `to_iodata(data)` ### Parameters - **data** (term) - Required - The data to convert to HTML-safe iodata. ### Returns - **iodata** - The HTML-safe iodata representation of the input data. ### Protocol Phoenix.HTML.Safe ### Usage Example ```elixir Phoenix.HTML.Safe.to_iodata("") ``` ``` -------------------------------- ### Combine html_escape/1 and safe_to_string/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Pipeline that converts a data structure to an escaped string by first applying html_escape/1 and then safe_to_string/1. ```elixir data |> html_escape() |> safe_to_string() ``` -------------------------------- ### input_name(form_or_name, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns a name of a corresponding form field. ```APIDOC ## input_name(form_or_name, field) ### Description Returns a name of a corresponding form field. ### Function Signature `input_name(form_or_name, field)` ``` -------------------------------- ### input_value(form, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns a value of a corresponding form field. ```APIDOC ## input_value(form, field) ### Description Returns a value of a corresponding form field. ### Function Signature `input_value(form, field)` ``` -------------------------------- ### options_for_select with multiple selected values Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Generates option elements with multiple selected values, marking both "admin" and "moderator" as selected. ```elixir options_for_select(["Admin": "admin", "User": "user", "Moderator": "moderator"], ["admin", "moderator"]) #=> #=> #=> ``` -------------------------------- ### safe_to_string/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Converts a safe result into a string. ```APIDOC ## safe_to_string(arg) ### Description Converts a safe result into a string. ### Parameters - **arg** (safe) - Required - The safe result to convert. ### Returns String representation of the safe result. ``` -------------------------------- ### t() Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Defines the structure of the Phoenix.HTML.Form struct, including all its fields and their types. ```APIDOC ## t() ### Description Defines the structure of the Phoenix.HTML.Form struct, including all its fields and their types. ### Type Definition ```elixir @type t() :: %Phoenix.HTML.Form{ action: atom(), data: %{required(field()) => term()}, errors: [{field(), term()}], hidden: Keyword.t(), id: String.t(), impl: module(), index: nil | non_neg_integer(), name: String.t(), options: Keyword.t(), params: %{required(binary()) => term()}, source: Phoenix.HTML.FormData.t() } ``` ``` -------------------------------- ### input_name(form_or_name, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the name of a corresponding form field. The first argument should either be a Phoenix.HTML.Form or an atom. ```APIDOC ## input_name(form_or_name, field) ### Description Returns a name of a corresponding form field. The first argument should either be a `Phoenix.HTML.Form` or an atom. ### Spec ```elixir @spec input_name(t() | atom(), field()) :: String.t() ``` ### Parameters - `form_or_name` (t() | atom()) - Required - The form struct or an atom representing the form name. - `field` (field()) - Required - The field name. ### Returns - (String.t()) - The name string for the field. ### Examples ```elixir iex> Phoenix.HTML.Form.input_name(:user, :first_name) "user[first_name]" ``` ``` -------------------------------- ### t() Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.FormField.html The type specification for the Phoenix.HTML.FormField struct, defining its fields and their types. ```APIDOC ## t() ### Description Defines the structure of a Phoenix.HTML.FormField struct. ### Type ```elixir @type t() :: %Phoenix.HTML.FormField{ errors: [term()], field: Phoenix.HTML.Form.field(), form: Phoenix.HTML.Form.t(), id: String.t(), name: String.t(), value: term() } ``` ### Fields - **errors** (list of term()) - A list of errors belonging to the field. - **field** (Phoenix.HTML.Form.field()) - The field name as an atom or a string. - **form** (Phoenix.HTML.Form.t()) - The parent `form` struct. - **id** (String.t()) - The `id` to be used as form input as a string. - **name** (String.t()) - The `name` to be used as form input as a string. - **value** (term()) - The value for the input. ``` -------------------------------- ### input_changed?(form1, form2, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Receives two forms structs and checks if the given field changed. ```APIDOC ## input_changed?(form1, form2, field) ### Description Receives two forms structs and checks if the given field changed. ### Function Signature `input_changed?(form1, form2, field)` ``` -------------------------------- ### input_validations(form, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the HTML validations that would apply to the given field. ```APIDOC ## input_validations(form, field) ### Description Returns the HTML validations that would apply to the given field. ### Spec ```elixir @spec input_validations(t(), field()) :: Keyword.t() ``` ### Parameters - `form` (t()) - Required - The form struct. - `field` (field()) - Required - The field name. ### Returns - (Keyword.t()) - A keyword list of HTML validations. ``` -------------------------------- ### input_validations(form, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the HTML validations that would apply to the given field. ```APIDOC ## input_validations(form, field) ### Description Returns the HTML validations that would apply to the given field. ### Function Signature `input_validations(form, field)` ``` -------------------------------- ### field() Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Defines the type for a form field, which can be an atom or a string. ```APIDOC ## field() ### Description Defines the type for a form field, which can be an atom or a string. ### Type Definition ```elixir @type field() :: atom() | String.t() ``` ``` -------------------------------- ### safe_to_string/1 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html The type specification for safe_to_string/1, showing it takes a safe() and returns a String.t(). ```elixir @spec safe_to_string(safe()) :: String.t() ``` -------------------------------- ### raw/1 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Specification for the raw/1 function, which marks content as raw so HTML is not escaped. ```elixir @spec raw(iodata() | safe() | nil) :: safe() ``` -------------------------------- ### input_id(name, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns an id of a corresponding form field. ```APIDOC ## input_id(name, field) ### Description Returns an id of a corresponding form field. ### Function Signature `input_id(name, field)` ``` -------------------------------- ### html_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes the HTML entities in the given term, returning safe iodata. ```APIDOC ## html_escape(safe) ### Description Escapes the HTML entities in the given term, returning safe iodata. ### Parameters - **safe** (term) - Required - The term to escape. ### Returns Safe iodata with HTML entities escaped. ``` -------------------------------- ### javascript_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes HTML content to be inserted into a JavaScript string. ```APIDOC ## javascript_escape(data) ### Description Escapes HTML content to be inserted into a JavaScript string. ### Parameters - **data** (term) - Required - The content to escape. ### Returns Escaped string suitable for JavaScript string insertion. ``` -------------------------------- ### Type specification for safe() Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Defines the safe type as a tuple {:safe, iodata()}, guaranteeing the content is safe. ```elixir @type safe() :: {:safe, iodata()} ``` -------------------------------- ### Type specification for unsafe() Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Defines the unsafe type as Phoenix.HTML.Safe.t(), indicating content that may be safe or unsafe and needs conversion. ```elixir @type unsafe() :: Phoenix.HTML.Safe.t() ``` -------------------------------- ### css_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes a string for use as a CSS identifier. ```APIDOC ## css_escape(value) ### Description Escapes a string for use as a CSS identifier. ### Parameters - **value** (string) - Required - The string to escape. ### Returns Escaped string suitable for CSS identifiers. ``` -------------------------------- ### input_id(name, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the id of a corresponding form field. The form should either be a Phoenix.HTML.Form or an atom. ```APIDOC ## input_id(name, field) ### Description Returns an id of a corresponding form field. The form should either be a `Phoenix.HTML.Form` or an atom. ### Spec ```elixir @spec input_id(t() | atom(), field()) :: String.t() ``` ### Parameters - `name` (t() | atom()) - Required - The form struct or an atom representing the form name. - `field` (field()) - Required - The field name. ### Returns - (String.t()) - The id string for the field. ``` -------------------------------- ### input_id(name, field, value) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns an id of a corresponding form field and value attached to it. ```APIDOC ## input_id(name, field, value) ### Description Returns an id of a corresponding form field and value attached to it. ### Function Signature `input_id(name, field, value)` ``` -------------------------------- ### safe_to_string/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Converts a safe result into a string. Fails if the result is not safe. Can be combined with html_escape/1 to convert a data structure to an escaped string. ```APIDOC ## safe_to_string/1 ### Description Converts a safe result into a string. Fails if the result is not safe. In such cases, you can invoke `html_escape/1` or `raw/1` accordingly before. ### Spec ```elixir @spec safe_to_string(safe()) :: String.t() ``` ### Usage You can combine `html_escape/1` and `safe_to_string/1` to convert a data structure to an escaped string: ```elixir data |> html_escape() |> safe_to_string() ``` ``` -------------------------------- ### normalize_value(type, value) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Normalizes an input `value` according to its input `type`. ```APIDOC ## normalize_value(type, value) ### Description Normalizes an input `value` according to its input `type`. ### Function Signature `normalize_value(type, value)` ``` -------------------------------- ### input_value/2 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec for input_value/2, which returns the value of a form field. ```elixir @spec input_value(t() | atom(), field()) :: term() ``` -------------------------------- ### html_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes the HTML entities in the given term, returning safe iodata. This is the core function for preventing XSS attacks when rendering user content. ```APIDOC ## html_escape/1 ### Description Escapes the HTML entities in the given term, returning safe iodata. ### Function Signature ```elixir @spec html_escape(unsafe()) :: safe() ``` ### Parameters - **safe** (unsafe()) - The term to escape. Can be a binary, charlist, number, or a `{:safe, ...}` tuple. ### Return Value - **safe()** - A tuple `{:safe, iodata}` containing the escaped content. ### Examples ```elixir iex> html_escape("") {:safe, [[[] | "<"], "hello" | ">"]} iex> html_escape(~c"") {:safe, ["<", 104, 101, 108, 108, 111, ">"]} iex> html_escape(1) {:safe, "1"} iex> html_escape({:safe, ""}) {:safe, ""} ``` ``` -------------------------------- ### input_validations/2 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec for input_validations/2, which returns HTML validations for a field. ```elixir @spec input_validations(t(), field()) :: Keyword.t() ``` -------------------------------- ### attributes_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes an enumerable of attributes, returning iodata. Handles special attributes like :aria, :data, :phx, :class, and :id. ```APIDOC ## attributes_escape(attrs) ### Description Escapes an enumerable of attributes, returning iodata. The attributes are rendered in the given order. If a map is given, key ordering is not guaranteed. Keys and values can be any shape implementing the Phoenix.HTML.Safe protocol. Atom keys are dasherized (e.g., :phx_value_id becomes phx-value-id). Special handling: :aria, :data, and :phx accept keyword lists; :class accepts a list of classes; :id raises if a number is given. ### Parameters - **attrs** (enumerable) - Required - The attributes to escape. ### Examples ```elixir iex> safe_to_string attributes_escape(title: "the title", id: "the id", selected: true) " title=\"the title\" id=\"the id\" selected" iex> safe_to_string attributes_escape(%{data: [confirm: "Are you sure?"]}) " data-confirm=\"Are you sure?\"" iex> safe_to_string attributes_escape(%{phx: [value: [foo: "bar"]]}) " phx-value-foo=\"bar\"" ``` ``` -------------------------------- ### normalize_value(type, value) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Normalizes an input value according to its input type. Handles datetime-local, checkbox, and textarea types to ensure proper rendering and submission behavior. ```APIDOC ## normalize_value(type, value) ### Description Normalizes an input `value` according to its input `type`. Certain HTML input values must be cast to avoid idiosyncrasies when rendered. For "datetime-local" types, it converts `DateTime` and `NaiveDateTime` to strings without second precision. For "checkbox" types, it returns a boolean based on whether the input is "true" or not. For "textarea", it prefixes a newline to ensure newlines won't be ignored on submission. ### Parameters - `type` (atom) - Required - The HTML input type (e.g., :datetime-local, :checkbox, :textarea). - `value` (any) - Required - The value to normalize. ### Returns - Normalized value based on the input type. ``` -------------------------------- ### javascript_escape/1 specs Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Specifications for the javascript_escape/1 function, which escapes HTML content for insertion into a JavaScript string. ```elixir @spec javascript_escape(binary()) :: binary() ``` ```elixir @spec javascript_escape(safe()) :: safe() ``` -------------------------------- ### input_changed?(form1, form2, field) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Checks if a given field changed between two form structs. The field is considered changed if its value, errors, action, or implementation changed. ```APIDOC ## input_changed?(form1, form2, field) ### Description Receives two forms structs and checks if the given field changed. The field will have changed if either its associated value, errors, action, or implementation changed. This is mostly used for optimization engines as an extension of the `Access` behaviour. ### Spec ```elixir @spec input_changed?(t(), t(), field()) :: boolean() ``` ### Parameters - `form1` (t()) - Required - The first form struct. - `form2` (t()) - Required - The second form struct. - `field` (field()) - Required - The field to check for changes. ### Returns - (boolean()) - `true` if the field changed, `false` otherwise. ``` -------------------------------- ### input_id/2 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec for input_id/2, which returns the id of a form field. ```elixir @spec input_id(t() | atom(), field()) :: String.t() ``` -------------------------------- ### css_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes a string for use as a CSS identifier. This is useful when dynamically generating CSS class names or selectors from user input to prevent CSS injection. ```APIDOC ## css_escape/1 ### Description Escapes a string for use as a CSS identifier. ### Function Signature ```elixir @spec css_escape(String.t()) :: String.t() ``` ### Parameters - **value** (String.t()) - The string to escape. ### Return Value - **String.t()** - The escaped string. ### Examples ```elixir iex> css_escape("hello world") "hello\\ world" iex> css_escape("-123") "-\\31 23" ``` ``` -------------------------------- ### javascript_escape/1 Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Escapes HTML content to be inserted into a JavaScript string. This is useful when embedding HTML rendered from templates into JavaScript responses to prevent breaking out of the string context. ```APIDOC ## javascript_escape/1 ### Description Escapes HTML content to be inserted into a JavaScript string. This function is useful in JavaScript responses when there is a need to escape HTML rendered from other templates. ### Function Signatures ```elixir @spec javascript_escape(binary()) :: binary() @spec javascript_escape(safe()) :: safe() ``` ### Parameters - **data** (binary() or safe()) - The content to escape. ### Return Value - **binary()** or **safe()** - The escaped content. ### Usage Example ```elixir $("#container").append("<%= javascript_escape(render("post.html", post: @post)) %>"); ``` ### Notes It escapes quotes (double and single), double backslashes and others. ``` -------------------------------- ### css_escape/1 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Specification for the css_escape/1 function, which escapes a string for use as a CSS identifier. ```elixir @spec css_escape(String.t()) :: String.t() ``` -------------------------------- ### input_changed?/3 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec for input_changed?/3, which checks if a field changed between two form structs. ```elixir @spec input_changed?(t(), t(), field()) :: boolean() ``` -------------------------------- ### html_escape/1 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.html Specification for the html_escape/1 function, which escapes HTML entities in a term and returns safe iodata. ```elixir @spec html_escape(unsafe()) :: safe() ``` -------------------------------- ### input_id/3 spec Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Spec for input_id/3, which returns the id of a form field with a value attached. ```elixir @spec input_id(t() | atom(), field(), Phoenix.HTML.Safe.t()) :: String.t() ``` -------------------------------- ### input_id(name, field, value) Source: https://phoenix-html.hexdocs.pm/Phoenix.HTML.Form.html Returns the id of a corresponding form field with a value attached. Useful for radio buttons and inputs like multiselect checkboxes. ```APIDOC ## input_id(name, field, value) ### Description Returns an id of a corresponding form field and value attached to it. Useful for radio buttons and inputs like multiselect checkboxes. ### Spec ```elixir @spec input_id(t() | atom(), field(), Phoenix.HTML.Safe.t()) :: String.t() ``` ### Parameters - `name` (t() | atom()) - Required - The form struct or an atom representing the form name. - `field` (field()) - Required - The field name. - `value` (Phoenix.HTML.Safe.t()) - Required - The value to attach to the id. ### Returns - (String.t()) - The id string for the field with the value. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.