Fix syntax errors, fix formatting bugs, and fix code vanishing issue due to stale cache#293
Fix syntax errors, fix formatting bugs, and fix code vanishing issue due to stale cache#293Soumyajit7 wants to merge 4 commits into
Conversation
…DocumentApp enum from global scope
…ale cache preview payload
Reviewer's GuideUpdates server- and client-side logic to run on V8, harden XML/HTML handling, and prevent stale cached previews from causing code-loss when formatting highlighted text. Sequence diagram for insertCodeOrGetSelectionAndThemeCss cache validation and safe insertionsequenceDiagram
actor User
participant Sidebar
participant Server
participant Document
User->>Sidebar: click Format
Sidebar->>Server: insertCodeOrGetSelectionAndThemeCss(html, prefs)
Server->>Server: hasValidHtml = html && html.indexOf('class="hljs') !== -1
Server->>Server: selection = getSelection()
Server->>Server: selectedText = getTextFromSelection(selection)
alt hasValidHtml && alreadySelected(selectedText) && alreadySaved(prefs)
Server->>Document: insertCode(html, prefs.noBackground, selection)
Server-->>Sidebar: confirm insertion
else cache invalid or missing highlight
Server->>Server: [regenerate preview HTML]
Server-->>Sidebar: return new preview HTML and theme CSS
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new trimming/sanitization in
replaceSelectionandcreateHighlightedBlockremoves all leading/trailing whitespace (including ), which may unintentionally strip meaningful indentation or trailing spaces in code samples; consider narrowing the sanitization to only what breaks XML parsing (e.g., BOMs) or limiting it to outer wrapper elements. - The
hasValidHtmlcheck ininsertCodeOrGetSelectionAndThemeCssrelies on the literal substringclass="hljs", which could fail if classes are reordered or single quotes are used; consider a more robust validation (e.g., regex forhljsas a class token or parsing the root node) to avoid false negatives. - In
createHighlightedBlock,$.parseHTML(highlighted)is now filtered to element nodes only, but you still treatblockas a single jQuery collection; if multiple root elements can be produced, clarify whether that is expected and, if not, explicitly select or wrap the intended root element.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new trimming/sanitization in `replaceSelection` and `createHighlightedBlock` removes all leading/trailing whitespace (including ` `), which may unintentionally strip meaningful indentation or trailing spaces in code samples; consider narrowing the sanitization to only what breaks XML parsing (e.g., BOMs) or limiting it to outer wrapper elements.
- The `hasValidHtml` check in `insertCodeOrGetSelectionAndThemeCss` relies on the literal substring `class="hljs"`, which could fail if classes are reordered or single quotes are used; consider a more robust validation (e.g., regex for `hljs` as a class token or parsing the root node) to avoid false negatives.
- In `createHighlightedBlock`, `$.parseHTML(highlighted)` is now filtered to element nodes only, but you still treat `block` as a single jQuery collection; if multiple root elements can be produced, clarify whether that is expected and, if not, explicitly select or wrap the intended root element.
## Individual Comments
### Comment 1
<location path="server/document.js" line_range="338" />
<code_context>
+ attrs[DocumentApp.Attribute[attrName]] = true;
}
return;
case constants.document.cssAttrs.background:
</code_context>
<issue_to_address>
**issue (bug_risk):** Background attribute key is now inconsistent with the new string-based docAttrs mapping.
For `background`, `constants.document.docAttrs.background` now resolves to the string `'BACKGROUND_COLOR'`, so `attrs[constants.document.docAttrs.background] = val;` uses a plain string key, unlike the other branches which use `DocumentApp.Attribute[attrName]`. For consistency and correct typing, this should also use `DocumentApp.Attribute[constants.document.docAttrs.background]` (or keep `docAttrs.background` as the enum instead of a string).
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| attrs[DocumentApp.Attribute[attrName]] = true; | ||
| } | ||
| return; | ||
| case constants.document.cssAttrs.background: |
There was a problem hiding this comment.
issue (bug_risk): Background attribute key is now inconsistent with the new string-based docAttrs mapping.
For background, constants.document.docAttrs.background now resolves to the string 'BACKGROUND_COLOR', so attrs[constants.document.docAttrs.background] = val; uses a plain string key, unlike the other branches which use DocumentApp.Attribute[attrName]. For consistency and correct typing, this should also use DocumentApp.Attribute[constants.document.docAttrs.background] (or keep docAttrs.background as the enum instead of a string).
…prove hljs regex validation, and wrap multi-root highlighted blocks
|
@Soumyajit7 did you take a look at the PR a few days before yours? |
|
@pointmatic No I did not look at those PR, I took fork from master branch and tried to fix the issue, and it's working now. |
- Updated title in server/constants.js from 'Code Blocks' to 'New Code Blocks' - Added addOns section to appsscript.json to register as a Google Docs add-on - Added build_win.sh helper script for building on Windows via Git Bash
Fix execution errors, XML parsing failures, and data-loss bug
This PR addresses several major issues preventing the add-on from functioning properly in modern Google Docs environments, including a critical data-loss bug where code would vanish upon formatting.
1. Migrated away from deprecated Rhino runtime
Google has officially deprecated the older Rhino runtime for Apps Script.
appsscript.jsonto properly specify the V8 runtime environment to resolve theThe Rhino runtime is deprecated and no longer supportedexecution failure.2. Fixed
SAXParseException("Content is not allowed in prolog")The underlying
XmlService.parse()was failing strictly due to invisible characters and malformed characters hitting the parser.dist/styles.htmlfile generated during the build pipeline occasionally contained UTF-16LE invisible Byte Order Marks (BOM). I added regex to strip any leading whitespace and BOMs before parsing. to prior to XML parsing to prevent unescaped entities from crashing the strict XML parser.client/sidebar.jsto strictly filter for element nodes, dropping loose whitespace text nodes that were previously polluting the<pre>element payload sent to the server.3. Fixed "Code Vanishing" bug (Stale Cache Issue)
Users were experiencing a major issue where clicking "Format" on a fresh sidebar load would successfully delete their highlighted code, but insert an empty invisible table in its place.
CacheServicestores the hash of the last highlighted text. If a user reloaded the sidebar and highlighted the same text, the server would see a cache match and tell the client "the selection hasn't changed, I will blindly insert the preview HTML you passed me." However, on a fresh sidebar load, the preview HTML is an empty<pre id="preview"></pre>placeholder.insertCodeOrGetSelectionAndThemeCssinsideserver/main.js. The server will no longer blindly trust the cache match if the client payload is missing the expectedhighlight.jsstructural classes, forcing a safe regeneration instead.Summary by Sourcery
Fix XML parsing and caching issues that caused formatting to fail or remove code blocks in the Google Docs add-on.
Bug Fixes:
Build: