Skip to content

Commit

Permalink
Preview Base URL Fix
Browse files Browse the repository at this point in the history
Fixes:
- Fixed the way I implemented adding the Base URL tag to the document when loading an HTML page in the Preview window with a set custom Base URL from STE's menus. Previously I added the `<base>` tag to the editor's source by parsing the string as HTML, to a DOM object, appending the custom tag to the live document, then serializing that modified DOM with the custom tag back to a plain HTML string, using `new XMLSerializer()`. This worked most of the time, but it was much more complicated than it needed to be, and it was actually escaping characters like `<` or `>` if they weren't part of a full HTML tag, which started to cause errors with things like arrow functions, which would turn into `&gt;`, causing the JS to error-out. Now I am simply adding the `<base>` tag to the first unedited HTML string from the editor, then displaying that. No need to add extra parsing, as this works just as well, and better! haha

Additions:
- In conjunction with the previous note, Display windows created from the View menu (or keyboard shortcut) will now use the Base URL if one is present from STE's menu. I hadn't added it before this, so it was just using the default url for STE, which would be wherever STE was running from (stedit.app in production, localhost while in dev). Now the custom Base URL set by the user is followed.
  • Loading branch information
Offroaders123 committed Jan 16, 2022
1 parent f9efb99 commit 382b35c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2945,7 +2945,10 @@
left = window.screen.availWidth / 2 + window.screen.availLeft - width / 2,
top = window.screen.availHeight / 2 + window.screen.availTop - height / 2,
features = (Editor.appearance().standalone || Editor.appearance().fullscreen) ? "popup" : "",
link = window.URL.createObjectURL(new Blob([Editor.query().textarea.value],{ type: "text/html" })),
baseURL = Editor.settings.get("preview-base") || null,
source = Editor.query().textarea.value;
if (baseURL) source = `<!-- Document Base URL appended by Smart Text Editor -->\n<base href="${baseURL}">\n\n${source}`;
var link = window.URL.createObjectURL(new Blob([source],{ type: "text/html" })),
win = window.open(link,"_blank",features);
window.URL.revokeObjectURL(link);
win.moveTo(left,top);
Expand Down Expand Up @@ -3008,12 +3011,7 @@
var change = (editor.tab.hasAttribute("data-editor-refresh") && Editor.settings.get("automatic-refresh") != false);
if (!change && !force) return;
var baseURL = Editor.settings.get("preview-base") || null, source = editor.textarea.value;
if (baseURL){
var sourceDOM = new DOMParser().parseFromString(source,"text/html"), baseElement = document.createElement("base");
sourceDOM.head.prepend(baseElement);
baseElement.href = baseURL;
source = new XMLSerializer().serializeToString(sourceDOM);
}
if (baseURL) source = `<!-- Document Base URL appended by Smart Text Editor -->\n<base href="${baseURL}">\n\n${source}`;
preview.addEventListener("load",() => {
preview.contentWindow.document.open();
preview.contentWindow.document.write(source);
Expand Down
2 changes: 1 addition & 1 deletion service-worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
self.Editor = {
version: 3.16,
version: 3.17,
cache: true,
environment: () => ({
macOS_device: (/(macOS|Mac)/i.test(("userAgentData" in navigator) ? navigator.userAgentData.platform : navigator.platform) && navigator.standalone === undefined)
Expand Down

0 comments on commit 382b35c

Please sign in to comment.