duck.html.components.script¶
Script HTML Component.
This module defines a reusable Script component for embedding JavaScript code within an HTML document.
Module Contents¶
Classes¶
Script HTML Component. |
Functions¶
Heuristic: is a “/” at this point grammatically a regex literal rather than division, based on the preceding token? |
|
Single-pass scanner that strips comments and incidental whitespace while copying strings, template literals, and regex literals verbatim. |
|
Attempts to find the end of a regex literal starting at index i. |
|
Returns the index just past the closing quote of the string literal starting at index i. |
|
Returns the index just past the closing backtick of the template
literal starting at index i, treating any |
|
Returns the index just past the “}” that closes a |
|
Conservatively minifies JavaScript: strips comments and collapses incidental whitespace (indentation, blank lines, redundant inline spaces), without ever removing a newline that follows real code. |
Data¶
API¶
- class duck.html.components.script.Script(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]¶
Bases:
duck.html.components.InnerComponentScript HTML Component.
The
Scriptcomponent allows developers to embed JavaScript code within an HTML page dynamically. It can be used to define inline scripts that interact with other components.- Parameters:
inner_html – The raw JavaScript to embed.
minify – Optional. Whether to minify the JavaScript. Defaults to True.
Features:
Supports inline JavaScript execution.
Can be dynamically added to any component.
Provides flexibility for defining custom client-side logic.
Minifies the embedded script by default (comments, indentation, and blank lines only – see
minify_js), passminify=Falseto disable.
Example Usage:
script = Script( inner_html=''' function showAlert() { alert("Hello, world!"); } ''' ) component.add_child(script)This will generate the following HTML output:
<script> function showAlert() { alert("Hello, world!"); } </script>Notes:
Automatic Nonce Addition: When
ENABLE_HEADERS_SECURITY_POLICY=Trueandcsp_nonce_flagis set inCSP_TRUSTED_SOURCES, thenonceproperty is automatically added.Request Resolution Required: For the
nonceto be set automatically, the request must be resolved. This is achieved by callingget_request_or_raiseon the component root (or the current component if the root isNone).
Initialization
Initialize an HTML component.
- Parameters:
element – The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.
accept_inner_html – Whether the HTML component accepts an inner body (e.g., inner-body-here).
inner_html – Inner html to add to the HTML component. Defaults to None.
properties – Dictionary for properties to initialize the component with.
props – Just same as properties argument (added for simplicity).
style – Dictionary for style to initialize the component with.
event_handlers – Events to automatically bind. Each dictionary maps an event name to its handler and may include additional keyword arguments forwarded to
bind(), e.g.event_handlers=[{"click": on_button_click, **extra_kwargs}].**kwargs – Extra keyword arguments
- Raises:
HtmlComponentError – If ‘element’ is not a string or ‘inner_html’ is set but ‘accept_inner_html’ is False.
- on_create() None[source]¶
Initializes the component and, unless disabled, minifies the embedded script.
- property properties: dict¶
- duck.html.components.script._JS_REGEX_PRECEDING_KEYWORDS¶
None
- duck.html.components.script._JS_REGEX_PRECEDING_PUNCTUATION¶
‘set(…)’
- duck.html.components.script._js_regex_may_start(last_token: str) bool[source]¶
Heuristic: is a “/” at this point grammatically a regex literal rather than division, based on the preceding token?
- duck.html.components.script._minify_js_scan(js: str) str[source]¶
Single-pass scanner that strips comments and incidental whitespace while copying strings, template literals, and regex literals verbatim.
- duck.html.components.script._skip_js_regex(js: str, i: int) int[source]¶
Attempts to find the end of a regex literal starting at index i.
- Returns:
The index just past the closing “/” and its flags, or None if this doesn’t look like a valid regex literal (the caller should then treat the “/” as division instead).
- duck.html.components.script._skip_js_string(js: str, i: int) int[source]¶
Returns the index just past the closing quote of the string literal starting at index i.
- duck.html.components.script._skip_js_template(js: str, i: int) int[source]¶
Returns the index just past the closing backtick of the template literal starting at index i, treating any
${...}substitution (including nested template literals within it) as opaque.
- duck.html.components.script._skip_js_template_expression(js: str, i: int) int[source]¶
Returns the index just past the “}” that closes a
${...}substitution, given i just after its opening “${”.
- duck.html.components.script.minify_js(js: str) str[source]¶
Conservatively minifies JavaScript: strips comments and collapses incidental whitespace (indentation, blank lines, redundant inline spaces), without ever removing a newline that follows real code.
JavaScript relies on newlines for Automatic Semicolon Insertion (ASI) –
return\n{ a: 1 }andreturn { a: 1 }behave differently – so merging lines the way a CSS minifier safely can is not safe here. This function only drops blank/whitespace-only lines, leading indentation, comments, and redundant runs of spaces/tabs within a line; every newline that separates two lines of real code is kept.String and regex literal contents are left completely untouched. Template literals (including their
${...}substitutions) are preserved exactly as written and are not minified internally, to avoid any risk of misinterpreting nested code as whitespace.- Parameters:
js – Raw JavaScript source.
- Returns:
The minified JavaScript.