Skip to content

fix: Variables Editor client script fails to parse, blanking the whole UI - #229

Merged
sergak01 merged 3 commits into
mainfrom
develop
Aug 7, 2026
Merged

fix: Variables Editor client script fails to parse, blanking the whole UI#229
sergak01 merged 3 commits into
mainfrom
develop

Conversation

@sergak01

@sergak01 sergak01 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Patch for a bug in the just-released v1.2.0: escapeJsAttr() (added in #227's XSS fix) lives inside the single giant template literal that getVariablesEditorHtml() returns as the page's inline <script>. Its backslash-escaping regexes were correct for a standalone file, but the outer template literal consumes one level of backslash-escaping when the .ts source itself is parsed — so the browser received a syntactically broken script:

Invalid regular expression: /\/g, '\').replace(/: Unmatched ')'

That SyntaxError aborted the entire inline script, so neither the Schema nor the Values tab ever rendered — reproduced live against a consumer project running @metricinsights/pp-dev@1.2.0.

Key changes

  • 🔧 Doubled the escaping in escapeJsAttr() to compensate for the extra template-literal unescape pass.
  • 🧪 Added a regression test (tests/unit/lib/variables-editor.spec.ts) that extracts every <script> block from the rendered page and parses it with new Function() — fails on the pre-fix code, passes on the fix.

Included commits

f16465f Merge pull request #228 from mi-examples/pp-3990
2e48b2e fix: Variables Editor client script fails to parse, blanking the whole UI

Testing

  • npx tsc --noEmit — clean
  • npm run lint — clean
  • npm run test:unit — 275/275 passing
  • Live verification: built + packed pp-dev locally, installed into a consumer project, restarted its dev server, confirmed the Variables Editor renders both tabs with no console errors on the exact file that reproduced the bug.

Merge Request: origin/developorigin/main

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where the Variables Editor could appear blank when schema values caused a client-side script parsing error.
    • Improved handling of special characters in embedded values.
  • Tests

    • Added coverage to verify that generated Variables Editor scripts parse successfully.
  • Chores

    • Updated the release version to 1.2.0-beta.3.
    • Added release notes for this fix.

sergak01 and others added 3 commits August 7, 2026 14:55
…e UI

escapeJsAttr() lives inside the one giant template literal that
getVariablesEditorHtml() returns as the page's inline <script>. Its
backslash-escaping regexes were written for a standalone file, but the
outer template literal consumes one level of backslash-escaping when the
.ts source is parsed — so the browser received a syntactically broken
script (Invalid regular expression: /\/g, '\').replace(/: Unmatched ')').
The resulting SyntaxError aborted the entire inline script, so neither the
Schema nor the Values tab ever rendered, regardless of whether the schema
had any tags.

Doubles the escaping to compensate for the extra template-literal layer,
verified against the exact embedded-string pipeline. Adds a regression
test that extracts every <script> block from the rendered page and parses
it with `new Function()`, which fails on the old code and passes on the fix.
fix: Variables Editor client script fails to parse, blanking the whole UI
# [1.2.0-beta.3](v1.2.0-beta.2...v1.2.0-beta.3) (2026-08-07)

### Bug Fixes

* Variables Editor client script fails to parse, blanking the whole UI ([2e48b2e](2e48b2e))
@sergak01 sergak01 self-assigned this Aug 7, 2026
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Variables Editor now preserves JavaScript string escaping for schema-derived HTML attributes. A regression test checks embedded script parsing. The package version and changelog now identify release 1.2.0-beta.3.

Changes

Variables Editor script escaping

Layer / File(s) Summary
Fix generated script escaping
src/lib/variables-editor.ts, tests/unit/lib/variables-editor.spec.ts, package.json, CHANGELOG.md
escapeJsAttr now escapes backslashes before single quotes. The regression test parses each embedded <script> block. The package version and changelog now use 1.2.0-beta.3.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the Variables Editor parsing bug that the pull request fixes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch develop

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sergak01
sergak01 merged commit 790686e into main Aug 7, 2026
1 check was pending

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CHANGELOG.md`:
- Line 4: Change the “Bug Fixes” heading from H3 to H2 so it follows the H1
release heading without skipping a level and satisfies markdownlint MD001.

In `@src/lib/variables-editor.ts`:
- Line 529: Update escapeJsAttr() to escape carriage return, newline, U+2028,
and U+2029 in addition to backslashes and single quotes, applying these
JavaScript escapes before escapeHtml() so generated single-quoted inline
handlers remain valid.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1627dcc8-20db-4ff4-b887-fffc5d0373aa

📥 Commits

Reviewing files that changed from the base of the PR and between 2a76fde and e207d1a.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • CHANGELOG.md
  • package.json
  • src/lib/variables-editor.ts
  • tests/unit/lib/variables-editor.spec.ts

Comment thread CHANGELOG.md
# [1.2.0-beta.3](https://github.com/mi-examples/pp-dev/compare/v1.2.0-beta.2...v1.2.0-beta.3) (2026-08-07)


### Bug Fixes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix the heading-level jump.

Line 1 uses an H1 release heading. Line 4 uses an H3 heading. This skips H2 and triggers markdownlint MD001. Change ### Bug Fixes to ## Bug Fixes.

Suggested fix
-### Bug Fixes
+## Bug Fixes
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
### Bug Fixes
## Bug Fixes
🧰 Tools
🪛 markdownlint-cli2 (0.23.2)

[warning] 4-4: Heading levels should only increment by one level at a time
Expected: h2; Actual: h3

(MD001, heading-increment)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@CHANGELOG.md` at line 4, Change the “Bug Fixes” heading from H3 to H2 so it
follows the H1 release heading without skipping a level and satisfies
markdownlint MD001.

Source: Linters/SAST tools

// context first, then HTML-escape the result so it can't break out of the attribute either.
function escapeJsAttr(s) {
return escapeHtml(String(s).replace(/\\/g, '\\\\').replace(/'/g, "\\'"));
return escapeHtml(String(s).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, "\\\\'"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Escape JavaScript line terminators as well.

Although the backslash fix resolves the reported issue, escapeJsAttr() still leaves \r, \n, U+2028, and U+2029 unchanged. A schema-derived list-column name containing one of these characters can make the generated single-quoted inline handler invalid. Add JavaScript escapes for these characters before calling escapeHtml().

Suggested source-level fix
-  return escapeHtml(String(s).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, "\\\\'"));
+  return escapeHtml(
+    String(s)
+      .replace(/\\\\/g, '\\\\\\\\')
+      .replace(/'/g, "\\\\'")
+      .replace(/\\r/g, '\\\\r')
+      .replace(/\\n/g, '\\\\n')
+      .replace(/\\u2028/g, '\\\\u2028')
+      .replace(/\\u2029/g, '\\\\u2029'),
+  );
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/variables-editor.ts` at line 529, Update escapeJsAttr() to escape
carriage return, newline, U+2028, and U+2029 in addition to backslashes and
single quotes, applying these JavaScript escapes before escapeHtml() so
generated single-quoted inline handlers remain valid.

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.

3 participants