Skip to content

Commit

Permalink
webview js: Fold image URL rewriting into processIncomingHtml.
Browse files Browse the repository at this point in the history
Also eliminate the legacy optionality of the second parameter.

This potentially involves some minor functional changes to
`handleInitialLoad`, in that the order of actions therewithin is
changed -- but if those actions don't commute, then we should
probably have been rewriting the source HTML before scrolling anyway.
  • Loading branch information
rk-for-zulip authored and gnprice committed Mar 27, 2020
1 parent 4cbfd15 commit 0b3d3b9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/webview/js/generatedEs3.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var compiledWebviewJs = (function (exports) {
return new RegExp(r);
});
var rewriteImageUrls = function rewriteImageUrls(auth) {
var element = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
var rewriteImageUrls = function rewriteImageUrls(auth, element) {
var realm = new URL(auth.realm);
var imageTags = [].concat(element instanceof HTMLImageElement ? [element] : [], Array.from(element.getElementsByTagName('img')));
imageTags.forEach(function (img) {
Expand Down Expand Up @@ -396,14 +395,15 @@ var compiledWebviewJs = (function (exports) {
window.scrollBy(0, newBoundRect.top - prevBoundTop);
};
var processIncomingHtml = function processIncomingHtml(root) {
var processIncomingHtml = function processIncomingHtml(auth, root) {
rewriteImageUrls(auth, root);
fixupKatex(root);
};
var handleUpdateEventContent = function handleUpdateEventContent(uevent) {
var contentNode = document.createElement('div');
contentNode.innerHTML = uevent.content;
processIncomingHtml(contentNode);
processIncomingHtml(uevent.auth, contentNode);
var target;
if (uevent.updateStrategy === 'replace') {
Expand All @@ -424,7 +424,6 @@ var compiledWebviewJs = (function (exports) {
}
documentBody.innerHTML = contentNode.innerHTML;
rewriteImageUrls(uevent.auth);
if (target.type === 'bottom') {
scrollToBottom();
Expand All @@ -444,11 +443,10 @@ var compiledWebviewJs = (function (exports) {
document.addEventListener('message', handleMessageEvent);
}
processIncomingHtml(auth, documentBody);
scrollToMessage(scrollMessageId);
rewriteImageUrls(auth);
sendScrollMessageIfListShort();
scrollEventsDisabled = false;
processIncomingHtml(documentBody);
};
var handleUpdateEventFetching = function handleUpdateEventFetching(uevent) {
Expand Down
11 changes: 4 additions & 7 deletions src/webview/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,16 @@ const scrollToPreserve = (msgId: number, prevBoundTop: number) => {
*
* The root itself must not need fixups.
*/
const processIncomingHtml = (root: Element) => {
const processIncomingHtml = (auth: Auth, root: Element) => {
rewriteImageUrls(auth, root);
fixupKatex(root);
};

const handleUpdateEventContent = (uevent: WebViewUpdateEventContent) => {
// Perform preprocessing on the webview content.
const contentNode: HTMLDivElement = document.createElement('div');
contentNode.innerHTML = uevent.content;
processIncomingHtml(contentNode);
processIncomingHtml(uevent.auth, contentNode);

let target: ScrollTarget;
if (uevent.updateStrategy === 'replace') {
Expand All @@ -512,8 +513,6 @@ const handleUpdateEventContent = (uevent: WebViewUpdateEventContent) => {
// (which currently breaks our touch event handling).
documentBody.innerHTML = contentNode.innerHTML;

rewriteImageUrls(uevent.auth);

if (target.type === 'bottom') {
scrollToBottom();
} else if (target.type === 'anchor') {
Expand Down Expand Up @@ -541,12 +540,10 @@ export const handleInitialLoad = (
document.addEventListener('message', handleMessageEvent);
}

processIncomingHtml(auth, documentBody);
scrollToMessage(scrollMessageId);
rewriteImageUrls(auth);
sendScrollMessageIfListShort();
scrollEventsDisabled = false;

processIncomingHtml(documentBody);
};

/*
Expand Down
5 changes: 1 addition & 4 deletions src/webview/js/rewriteImageUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ const inlineApiRoutes: RegExp[] = ['^/user_uploads/', '^/thumbnail$', '^/avatar/
* than the document location.
* 2. If the source URL names an endpoint known to require authentication,
* inject an API key into its query parameters.
*
* DEPRECATED: If no root element is specified, transform every <img> in the
* entire document.
*/
const rewriteImageUrls = (auth: Auth, element: Element | Document = document) => {
const rewriteImageUrls = (auth: Auth, element: Element) => {
const realm = new URL(auth.realm);

// Find the image elements to act on.
Expand Down

0 comments on commit 0b3d3b9

Please sign in to comment.