Skip to content

feat: add Range to raw elements (RawElement, ScriptElement)#1231

Merged
a-h merged 2 commits intoa-h:mainfrom
dgrundel:raw-elements
Aug 2, 2025
Merged

feat: add Range to raw elements (RawElement, ScriptElement)#1231
a-h merged 2 commits intoa-h:mainfrom
dgrundel:raw-elements

Conversation

@dgrundel
Copy link
Copy Markdown
Contributor

Hi, back again! This time adding Range to the elements included in the rawElements parser: RawElement and ScriptElement.

Testing: Here again I just updated the existing tests. Testing is a little slim for these elements, let me know if you'd like to see additional cases added.

Use case notes: In my organization we generally ban script and style tags directly in markup, preferring CSS and JS served from CDNs. (Or, better yet, no additional CSS at all except for those provided by Tailwind.) In cases where they are allowed, you want to be able to extract the contents of these tags and run them through separate, language-specific tools (e.g. stylelint, eslint). Lastly, there's a common pattern where server-side state is emitted into the page by inserting a script tag (with type like application/json) containing only a JSON payload. For these we'd want to ensure a consistent type attribute, perhaps require an id, and ensure that the contents of the tag is only a single go expression.

@a-h a-h changed the title Add Range to raw elements (RawElement, ScriptElement) feat: add Range to raw elements (RawElement, ScriptElement) Aug 2, 2025
@a-h
Copy link
Copy Markdown
Owner

a-h commented Aug 2, 2025

Looks good!

re: tests for this section, the parser tests check that the elements can be parsed into the object model well, but there's also formatting tests and generation tests that cover the same area from different angles.

You might be interested in #1230 which uses prettier to format <script> and <style> elements in templ code.

In that PR, I've also refactored the fmt command and behaviour so that it is easier to read and re-use.

@a-h a-h merged commit 71566a0 into a-h:main Aug 2, 2025
5 checks passed
dgrundel added a commit to dgrundel/templ that referenced this pull request Aug 16, 2025
Co-authored-by: Daniel <daniel@grundel.local>
AdamVig added a commit to AdamVig/templ that referenced this pull request Feb 15, 2026
71566a0 / a-h#1231 added `Range` to
`ScriptExpression` nodes, but only for `<script>` elements with a
JavaScript `type` (as decided by `hasJavaScriptType`). This commit
tracks `Range` for `<script>` elements with _non_-Javascript `type`s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants