fix: update static url for image and link - #1794
Conversation
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report at Codecov.
|
|
|
||
| const sanitizeContent = (content) => { | ||
| // rewrite anchor link | ||
| let newContent = rewriteStaticLinks( |
There was a problem hiding this comment.
Where does rewriteStaticLinks come from?
There was a problem hiding this comment.
edx-platform a global js function
There was a problem hiding this comment.
| `<a href="${staticUrl}`, | ||
| `<a href="${baseAssetUrl}`, | ||
| ); | ||
|
|
||
| // rewrite image src | ||
| newContent = rewriteStaticLinks( | ||
| newContent, | ||
| `<img src="${staticUrl}`, | ||
| `<img src="${baseAssetUrl}`, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
| const baseAssetUrl = options.base_asset_url; | ||
| const staticUrl = '/static'; | ||
|
|
||
| const sanitizeContent = (content) => { |
There was a problem hiding this comment.
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.
| `<a href="${staticUrl}`, | ||
| `<a href="${baseAssetUrl}`, | ||
| ); | ||
|
|
||
| // rewrite image src | ||
| newContent = rewriteStaticLinks( | ||
| newContent, | ||
| `<img src="${staticUrl}`, | ||
| `<img src="${baseAssetUrl}`, |
There was a problem hiding this comment.
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.
ec4f1c5 to
7a8cfa6
Compare
| // These url are generated by WYSIWYG editor, it is assumed to be formatted | ||
| // <a href='.....' >...</a> for link. |
There was a problem hiding this comment.
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
7a8cfa6 to
d5d2be8
Compare
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?
TL;DR - correctly update static url for image and link
JIRA: AU-490
What changed?
Developer Checklist
Testing Instructions
Follow the jira ticket instruction
Reviewer Checklist
Collectively, these should be completed by reviewers of this PR:
FYI: @edx/masters-devs-gta