Skip to content

fix: update static url for image and link - #1794

Merged
leangseu-edx merged 1 commit into
masterfrom
leangseu-edx/au-490
Feb 2, 2022
Merged

fix: update static url for image and link#1794
leangseu-edx merged 1 commit into
masterfrom
leangseu-edx/au-490

Conversation

@leangseu-edx

@leangseu-edx leangseu-edx commented Jan 26, 2022

Copy link
Copy Markdown
Contributor

TL;DR - correctly update static url for image and link

JIRA: AU-490

What changed?

  • Modify static url on insert/edit image and anchor link (update logic)
  • Modify static url on save contents (getcontent in this case)
  • Remove Sanitize url on init because it would be redundant with on save.

Developer Checklist

Testing Instructions

Follow the jira ticket instruction

Reviewer Checklist

Collectively, these should be completed by reviewers of this PR:

  • I've done a visual code review
  • I've tested the new functionality

FYI: @edx/masters-devs-gta

@leangseu-edx
leangseu-edx requested a review from a team January 26, 2022 15:20
@codecov

codecov Bot commented Jan 26, 2022

Copy link
Copy Markdown

Codecov Report

Merging #1794 (ec4f1c5) into master (b7dd0c0) will not change coverage.
The diff coverage is n/a.

❗ Current head ec4f1c5 differs from pull request most recent head d5d2be8. Consider uploading reports for the commit d5d2be8 to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1794   +/-   ##
=======================================
  Coverage   93.91%   93.91%           
=======================================
  Files         151      151           
  Lines       16592    16592           
  Branches     1828     1828           
=======================================
  Hits        15583    15583           
  Misses        807      807           
  Partials      202      202           
Flag Coverage Δ
unittests 93.91% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b7dd0c0...d5d2be8. Read the comment docs.

@leangseu-edx
leangseu-edx requested a review from matthugs January 27, 2022 17:25

const sanitizeContent = (content) => {
// rewrite anchor link
let newContent = rewriteStaticLinks(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Where does rewriteStaticLinks come from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

edx-platform a global js function

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +32 to +51
`<a href="${staticUrl}`,
`<a href="${baseAssetUrl}`,
);

// rewrite image src
newContent = rewriteStaticLinks(
newContent,
`<img src="${staticUrl}`,
`<img src="${baseAssetUrl}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a bit of a hack, which would fail on html like, say <img class="w-50" src="..." />

Why not use this the same way we do elsewhere in this library?

@leangseu-edx leangseu-edx Jan 27, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

getContent is actually get everything in html. If the user put anything even text that has /static/ will get replace. It can be quite a confusing experience. This is just an improvement to what it was.

For image tag, it always generate <img src="....." alt .... />. I tested with multiple iteration of input.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, so it sounds like we only support the rewriting of static links if folks insert the image via the WYSIWYG widget for adding images, and we don't rewrite static links if the user types html manually. Is that right?

If I have that right, I think a comment explaining what you've explained here would be helpful. (I would also urge you to make the comment longer than you'd consider necessary.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you expand your comment to include the assumptions you expressed in this thread? Especially that the specific placement of src or href is assured in image or anchor tags generated via the WYSIWYG editor.

I am encouraging you to document these assumptions because the behavior of other code may change in the future, causing this assumption to no longer be true.

Comment thread openassessment/xblock/static/js/src/studio/oa_tiny_mce.js
const baseAssetUrl = options.base_asset_url;
const staticUrl = '/static';

const sanitizeContent = (content) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would recommend choosing a different name for this function. sanitize is a word often used specifically for security-related changes to input. The use of that word in this context could easily give the wrong idea to the reader of your code.

Comment on lines +32 to +51
`<a href="${staticUrl}`,
`<a href="${baseAssetUrl}`,
);

// rewrite image src
newContent = rewriteStaticLinks(
newContent,
`<img src="${staticUrl}`,
`<img src="${baseAssetUrl}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you expand your comment to include the assumptions you expressed in this thread? Especially that the specific placement of src or href is assured in image or anchor tags generated via the WYSIWYG editor.

I am encouraging you to document these assumptions because the behavior of other code may change in the future, causing this assumption to no longer be true.

Comment thread openassessment/xblock/static/js/src/studio/oa_tiny_mce.js
@leangseu-edx
leangseu-edx force-pushed the leangseu-edx/au-490 branch 2 times, most recently from ec4f1c5 to 7a8cfa6 Compare February 2, 2022 17:45
@leangseu-edx
leangseu-edx requested a review from jansenk February 2, 2022 19:39
Comment on lines +38 to +39
// These url are generated by WYSIWYG editor, it is assumed to be formatted
// <a href='.....' >...</a> for link.

@matthugs matthugs Feb 2, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think a future reader of this code would be able to read the lines below it and know that this code would only work for things formatted as in the example in this comment. So they can probably figure out what your comment tells them, even without you having commented it.

The part that is most non-obvious about this code is why anchor tags (and image tags, below) can safely be assumed to be in that format. Why that is is probably the most important thing to include in a comment here.

- update url on insert/edit and save. It is less confusing than doing it on init.

chore: update comments for sanitize content method

chore: update comments and method name

chore: update translation

chore: version bump
@leangseu-edx
leangseu-edx merged commit 68a8ba0 into master Feb 2, 2022
@leangseu-edx
leangseu-edx deleted the leangseu-edx/au-490 branch February 2, 2022 20:50
jansenk added a commit that referenced this pull request Mar 7, 2022
fix: restore original tinymce behavior, fix trailing slash

In PR #1512 we introduced the bug by improperly combining the "dataHandler" methods into one method and only setting/checking src rather than also including href
In PR #1794 we attempted to fix the issue by fixing the order of the to->from in the dataHandler. We also did a more specific replacement of <a href=<url> and <img src=<url> This fixed the issue of inserting bad links and images, but the static url const used in the "load all content" transform was missing a slash resulting in incorrect "resolved" URLs. Thankfully according to the read replica, there are only 3 of those that currently exist.

Removing the handlers for EditImage and EditLink. Originally their purpose was so that you would only ever see the "static" link. When the image/link was saved, we'd convert it from the static link to the real link, and then when you would click "edit" we'd transition back from the real link to the "static" link. I'm getting rid of that. The URL presented in the edit dialog should actually be the URL and having it go back and forth hides the actual real value of the URL. Unnecessary and potentially confusing.

Removing the specific <a href=<url> and <img src=<url> filtering for the full content transform. It's nice, and it will potentially prevent issues where we have a /static/ that we don't want transformed, but unfortunately having the global replace is the current only way we can support resolving static URLs in audio or video embeds. (see https://openedx.atlassian.net/browse/AU-544) Rather than adding those specific "special cases" it seems like it's probably just easier to do the more general resolve?
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.

4 participants