diff --git a/src/gui/src/IPC.js b/src/gui/src/IPC.js
index 24a377f80..0564ab08c 100644
--- a/src/gui/src/IPC.js
+++ b/src/gui/src/IPC.js
@@ -20,6 +20,7 @@
import download from './helpers/download.js';
import ensureConfirmedEmail from './helpers/ensureConfirmedEmail.js';
import item_icon from './helpers/itemIcon.js';
+import { atPath } from './helpers/pathElements.js';
import socialLink from './helpers/socialLink.js';
import update_mouse_position from './helpers/updateMousePosition.js';
import path from './lib/path.js';
@@ -1702,7 +1703,7 @@ const ipc_listener = async (event, handled) => {
$(`.item[data-uid="${res.uid}"]`).removeItems();
// now add new items
UIItem({
- appendTo: $(`.item-container[data-path="${html_encode(path.dirname(target_path))}" i]`),
+ appendTo: atPath('.item-container', path.dirname(target_path)),
immutable: res.immutable || res.writable === false,
associated_app_name: res.associated_app?.name,
path: target_path,
@@ -1716,7 +1717,7 @@ const ipc_listener = async (event, handled) => {
suggested_apps: res.suggested_apps,
});
// sort each window
- $(`.item-container[data-path="${html_encode(path.dirname(target_path))}" i]`).each(function () {
+ atPath('.item-container', path.dirname(target_path)).each(function () {
window.sort_items(this, $(this).attr('data-sort_by'), $(this).attr('data-sort_order'));
});
$(el_filedialog_window).close();
diff --git a/src/gui/src/UI/UIDesktop.js b/src/gui/src/UI/UIDesktop.js
index 31833ad0c..dcee74021 100644
--- a/src/gui/src/UI/UIDesktop.js
+++ b/src/gui/src/UI/UIDesktop.js
@@ -34,6 +34,7 @@ import UIWindowProgress from './UIWindowProgress.js';
import UITaskbar from './UITaskbar.js';
import new_context_menu_item from '../helpers/newContextMenuItem.js';
import refresh_item_container from '../helpers/refreshItemContainer.js';
+import { atPath, retargetPaths, underPath } from '../helpers/pathElements.js';
import changeLanguage from '../i18n/i18nChangeLanguage.js';
import UIWindowTaskManager from './UIWindowTaskManager.js';
import truncate_filename from '../helpers/truncateFilename.js';
@@ -416,12 +417,12 @@ async function UIDesktop (options) {
}
// hide all UIItems with matching uids
- $(`.item[data-path='${item.path}']`).fadeOut(150, function () {
+ atPath('.item', item.path).fadeOut(150, function () {
// close all windows with matching uids
// $('.window-' + item.uid).close();
// close all windows that belong to a descendant of this item
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${item.path}/"]`).close();
+ // todo this has to be case-insensitive
+ underPath('.window', item.path).close();
});
});
@@ -459,20 +460,7 @@ async function UIDesktop (options) {
$(`.item[data-uid='${item.uid}']`).attr('data-path', new_path);
$(`.window-${item.uid}`).attr('data-path', new_path);
- // Update all elements that have matching paths
- $(`[data-path="${html_encode(item.old_path)}" i]`).each(function () {
- $(this).attr('data-path', new_path);
- if ( $(this).hasClass('window-navbar-path-dirname') )
- {
- $(this).text(item.name);
- }
- });
-
- // Update all elements whose paths start with old_path
- $(`[data-path^="${`${html_encode(item.old_path) }/`}"]`).each(function () {
- const new_el_path = $(this).attr('data-path').replace(`${item.old_path }/`, `${new_path }/`);
- $(this).attr('data-path', new_el_path);
- });
+ retargetPaths(item.old_path, new_path, item.name);
// Update all exact-matching windows
$(`.window-${item.uid}`).each(function () {
@@ -507,7 +495,7 @@ async function UIDesktop (options) {
const metadata = parse_item_metadata(fsentry.metadata);
// update all shortcut_to_path
- $(`.item[data-shortcut_to_path="${html_encode(resp.old_path)}" i]`).attr('data-shortcut_to_path', html_encode(fsentry.path));
+ atPath('.item', resp.old_path, 'data-shortcut_to_path').attr('data-shortcut_to_path', fsentry.path);
// remove all items with matching uids
$(`.item[data-uid='${fsentry.uid}']`).fadeOut(150, function () {
@@ -524,16 +512,16 @@ async function UIDesktop (options) {
// if trashing, close windows of trashed items and its descendants
if ( dest_path === window.trash_path ) {
- $(`.window[data-path="${html_encode(resp.old_path)}" i]`).close();
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${html_encode(resp.old_path)}/"]`).close();
+ atPath('.window', resp.old_path).close();
+ // todo this has to be case-insensitive
+ underPath('.window', resp.old_path).close();
}
// update all paths of its and its descendants' open windows
else {
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${html_encode(resp.old_path)}/"], .window[data-path="${html_encode(resp.old_path)}" i]`).each(function () {
- window.update_window_path(this, $(this).attr('data-path').replace(resp.old_path, fsentry.path));
+ // todo this has to be case-insensitive
+ underPath('.window', resp.old_path).add(atPath('.window', resp.old_path)).each(function () {
+ window.update_window_path(this, fsentry.path + $(this).attr('data-path').slice(resp.old_path.length));
});
}
@@ -560,7 +548,7 @@ async function UIDesktop (options) {
// create new item on matching containers
UIItem({
- appendTo: $(`.item-container[data-path='${html_encode(dest_path)}' i]`),
+ appendTo: atPath('.item-container', dest_path),
immutable: fsentry.immutable || fsentry.writable === false,
uid: fsentry.uid,
path: fsentry.path,
@@ -585,8 +573,8 @@ async function UIDesktop (options) {
// if these new path have any open parents that need to be updated
fsentry.parent_dirs_created.forEach(async dir => {
- let item_container = $(`.item-container[data-path='${html_encode(path.dirname(dir.path))}' i]`);
- if ( item_container.length > 0 && $(`.item[data-path="${html_encode(dir.path)}" i]`).length === 0 ) {
+ let item_container = atPath('.item-container', path.dirname(dir.path));
+ if ( item_container.length > 0 && atPath('.item', dir.path).length === 0 ) {
UIItem({
appendTo: item_container,
immutable: false,
@@ -606,7 +594,7 @@ async function UIDesktop (options) {
});
}
//sort each container
- $(`.item-container[data-path='${html_encode(dest_path)}' i]`).each(function () {
+ atPath('.item-container', dest_path).each(function () {
window.sort_items(this, $(this).attr('data-sort_by'), $(this).attr('data-sort_order'));
});
});
@@ -693,20 +681,7 @@ async function UIDesktop (options) {
$(`.item[data-uid='${item.uid}']`).attr('data-path', new_path);
$(`.window-${item.uid}`).attr('data-path', new_path);
- // Update all elements that have matching paths
- $(`[data-path="${html_encode(item.old_path)}" i]`).each(function () {
- $(this).attr('data-path', new_path);
- if ( $(this).hasClass('window-navbar-path-dirname') )
- {
- $(this).text(item.name);
- }
- });
-
- // Update all elements whose paths start with old_path
- $(`[data-path^="${`${html_encode(item.old_path) }/`}"]`).each(function () {
- const new_el_path = $(this).attr('data-path').replace(`${item.old_path }/`, `${new_path }/`);
- $(this).attr('data-path', new_el_path);
- });
+ retargetPaths(item.old_path, new_path, item.name);
// Update all exact-matching windows
$(`.window-${item.uid}`).each(function () {
diff --git a/src/gui/src/UI/UIWindow.js b/src/gui/src/UI/UIWindow.js
index be0d86f7b..7164d4f15 100644
--- a/src/gui/src/UI/UIWindow.js
+++ b/src/gui/src/UI/UIWindow.js
@@ -25,6 +25,7 @@ import UIAuthLogin from './Auth/UIAuthLogin.js';
import UIWindowItemProperties from './UIWindowItemProperties.js';
import new_context_menu_item from '../helpers/newContextMenuItem.js';
import refresh_item_container from '../helpers/refreshItemContainer.js';
+import { atPath } from '../helpers/pathElements.js';
import UIWindowAppFeedback from './UIWindowAppFeedback.js';
import launch_app from '../helpers/launchApp.js';
import publish_as_website from '../helpers/publishAsWebsite.js';
@@ -3348,7 +3349,7 @@ window.update_window_path = async function (el_window, target_path) {
// update sidebar items' active status
$(el_window).find('.window-sidebar-item').removeClass('window-sidebar-item-active');
- $(el_window).find(`.window-sidebar-item[data-path="${html_encode(target_path)}"]`).addClass('window-sidebar-item-active');
+ atPath($(el_window).find('.window-sidebar-item'), target_path).addClass('window-sidebar-item-active');
// clean
$(el_window).find('.explore-table-headers-th > .header-sort-icon').html('');
@@ -3432,8 +3433,8 @@ window.update_window_path = async function (el_window, target_path) {
}
}
- $(el_window).attr('data-path', html_encode(target_path));
- $(el_window).attr('data-name', html_encode(path.basename(target_path)));
+ $(el_window).attr('data-path', target_path);
+ $(el_window).attr('data-name', path.basename(target_path));
// /stat
if ( target_path === window.shared_path ) {
@@ -3447,7 +3448,7 @@ window.update_window_path = async function (el_window, target_path) {
try {
puter.fs.stat({ path: target_path, consistency: 'eventual' }).then(fsentry => {
// the window may have moved on while this stat was in flight
- if ( $(el_window).attr('data-path') !== html_encode(target_path) ) {
+ if ( $(el_window).attr('data-path') !== target_path ) {
return;
}
$(el_window).removeClass(`window-${ $(el_window).attr('data-uid')}`);
@@ -3491,9 +3492,9 @@ window.update_window_path = async function (el_window, target_path) {
$(el_window).find('.window-head-title').text(fsentry.name);
}
// data-name
- $(el_window).attr('data-name', html_encode(fsentry.name));
+ $(el_window).attr('data-name', fsentry.name);
// data-path
- $(el_window).attr('data-path', html_encode(target_path));
+ $(el_window).attr('data-path', target_path);
$(el_window_navbar_path_input).val(target_path);
$(el_window_navbar_path_input).attr('data-path', target_path);
// update layout
diff --git a/src/gui/src/UI/UIWindowPublishWebsite.js b/src/gui/src/UI/UIWindowPublishWebsite.js
index 00755293c..dc4d95cee 100644
--- a/src/gui/src/UI/UIWindowPublishWebsite.js
+++ b/src/gui/src/UI/UIWindowPublishWebsite.js
@@ -19,6 +19,7 @@
import UIWindow from './UIWindow.js';
import UIWindowMyWebsites from './UIWindowMyWebsites.js';
+import { underPath } from '../helpers/pathElements.js';
async function UIWindowPublishWebsite (target_dir_uid, target_dir_name, target_dir_path) {
let h = '';
@@ -110,7 +111,7 @@ async function UIWindowPublishWebsite (target_dir_uid, target_dir_name, target_d
});
// find all items whose path starts with target_dir_path
- $(`.item[data-path^="${target_dir_path}/"]`).each(function () {
+ underPath('.item', target_dir_path).each(function () {
// show the link badge
$(this).find('.item-has-website-url-badge').show();
// update item's website_url attribute
diff --git a/src/gui/src/UI/UIWindowPublishWorker.js b/src/gui/src/UI/UIWindowPublishWorker.js
index dc8d7bb66..f0e7135aa 100644
--- a/src/gui/src/UI/UIWindowPublishWorker.js
+++ b/src/gui/src/UI/UIWindowPublishWorker.js
@@ -19,6 +19,7 @@
import UIWindow from './UIWindow.js';
import UIWindowMyWebsites from './UIWindowMyWebsites.js';
+import { underPath } from '../helpers/pathElements.js';
async function UIWindowPublishWorker (target_dir_uid, target_dir_name, target_dir_path) {
let h = '';
@@ -117,7 +118,7 @@ async function UIWindowPublishWorker (target_dir_uid, target_dir_name, target_di
});
// find all items whose path starts with target_dir_path
- $(`.item[data-path^="${target_dir_path}/"]`).each(function () {
+ underPath('.item', target_dir_path).each(function () {
// show the link badge
// $(this).find('.item-has-website-url-badge').show();
// update item's website_url attribute
diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js
index 4417d9c37..2b11262a4 100644
--- a/src/gui/src/helpers.js
+++ b/src/gui/src/helpers.js
@@ -23,6 +23,7 @@ import { archiveEntryPath } from './helpers/archiveEntryPath.js';
import get_html_element_from_options from './helpers/getHtmlElementFromOptions.js';
import globToRegExp from './helpers/globToRegExp.js';
import item_icon from './helpers/itemIcon.js';
+import { atPath, retargetPaths, underPath } from './helpers/pathElements.js';
import { is_owned_by_me, trash_path_for } from './helpers/pathOwner.js';
import { invalidate_shared_roots } from './helpers/sharedAccess.js';
import truncate_filename from './helpers/truncateFilename.js';
@@ -1178,7 +1179,7 @@ window.create_folder = async (basedir, appendto_element) => {
rename: true,
overwrite: false,
success: function (data) {
- const el_created_dir = $(appendto_element).find(`.item[data-path="${html_encode(dirname)}/${html_encode(data.name)}"]`);
+ const el_created_dir = atPath($(appendto_element).find('.item'), `${dirname}/${data.name}`);
if ( el_created_dir.length > 0 ) {
window.activate_item_name_editor(el_created_dir);
@@ -1211,7 +1212,7 @@ window.create_file = async (options) => {
generateThumbnails: true,
thumbnailGenerator: createUploadThumbnailGenerator(),
success: async function (data) {
- const created_file = $(appendto_element).find(`.item[data-path="${html_encode(dirname)}/${html_encode(data.name)}"]`);
+ const created_file = atPath($(appendto_element).find('.item'), `${dirname}/${data.name}`);
if ( created_file.length > 0 ) {
window.activate_item_name_editor(created_file);
@@ -1583,8 +1584,8 @@ window.delete_item = async function (el_item, descendants_only = false) {
// close all windows with matching uids
$(`.window-${ $(el_item).attr('data-uid')}`).close();
// close all windows that belong to a descendant of this item
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${$(el_item).attr('data-path')}/"]`).close();
+ // todo this has to be case-insensitive
+ underPath('.window', $(el_item).attr('data-path')).close();
});
try {
@@ -1605,7 +1606,7 @@ window.delete_item = async function (el_item, descendants_only = false) {
window.update_explorer_footer_selected_items_count(this);
});
// update all shortcuts to this item
- $(`.item[data-shortcut_to_path="${html_encode($(el_item).attr('data-path'))}" i]`).attr('data-shortcut_to_path', '');
+ atPath('.item', $(el_item).attr('data-path'), 'data-shortcut_to_path').attr('data-shortcut_to_path', '');
});
} catch ( err ) {
// Nothing was deleted: bring back what was faded out above.
@@ -1619,7 +1620,7 @@ window.move_clipboard_items = function (el_target_container, target_path) {
let el_items = [];
if ( window.clipboard.length > 0 ) {
for ( let i = 0; i < window.clipboard.length; i++ ) {
- el_items.push($(`.item[data-path="${html_encode(window.clipboard[i])}" i]`));
+ el_items.push(atPath('.item', window.clipboard[i]));
}
if ( el_items.length > 0 )
{
@@ -1961,24 +1962,18 @@ window.move_items = async function (el_items, dest_path, is_undo = false) {
// skip next loop iteration because this iteration was successful
item_with_same_name_already_exists = false;
- // update all shortcut_to_path — compare raw attribute values
- // (item rows store paths unencoded, so an html_encode()d
- // selector misses names containing & < > " ')
- const moved_from_path_lc = String($(el_item).attr('data-path') || '').toLowerCase();
- $('.item[data-shortcut_to_path]').filter(function () {
- return String($(this).attr('data-shortcut_to_path')).toLowerCase() === moved_from_path_lc;
- }).attr('data-shortcut_to_path', fsentry.path);
+ const moved_from_path = $(el_item).attr('data-path');
+
+ // update all shortcut_to_path
+ atPath('.item', moved_from_path, 'data-shortcut_to_path').attr('data-shortcut_to_path', fsentry.path);
// Remove all items with matching uids from their OLD location(s).
// Exclude any row already at the item's new path: a concurrent
// item.moved socket handler may have just created a row at the
// destination (e.g. the dashboard file view showing the target
// directory), and removing by uid alone would delete it too.
- // Raw case-insensitive compare, for the same reason as above.
- const dest_item_path_lc = fsentry.path.toLowerCase();
- $(`.item[data-uid='${$(el_item).attr('data-uid')}']`).not(function () {
- return String($(this).attr('data-path') || '').toLowerCase() === dest_item_path_lc;
- }).fadeOut(150, function () {
+ const $moved_items = $(`.item[data-uid='${$(el_item).attr('data-uid')}']`);
+ $moved_items.not(atPath($moved_items, fsentry.path)).fadeOut(150, function () {
// find all parent windows that contain this item
let parent_windows = $(`.item[data-uid='${$(el_item).attr('data-uid')}']`).closest('.window');
// remove this item
@@ -1992,23 +1987,23 @@ window.move_items = async function (el_items, dest_path, is_undo = false) {
// if trashing, close windows of trashed items and its descendants
if ( is_trashing ) {
- $(`.window[data-path="${html_encode($(el_item).attr('data-path'))}" i]`).close();
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${html_encode($(el_item).attr('data-path'))}/"]`).close();
+ atPath('.window', moved_from_path).close();
+ // todo this has to be case-insensitive
+ underPath('.window', moved_from_path).close();
}
// update all paths of its and its descendants' open windows
else {
- // todo this has to be case-insensitive but the `i` selector doesn't work on ^=
- $(`.window[data-path^="${html_encode($(el_item).attr('data-path'))}/"], .window[data-path="${html_encode($(el_item).attr('data-path'))}" i]`).each(function () {
- window.update_window_path(this, $(this).attr('data-path').replace($(el_item).attr('data-path'), path.join(item_dest_path, fsentry.name)));
+ // todo this has to be case-insensitive
+ underPath('.window', moved_from_path).add(atPath('.window', moved_from_path)).each(function () {
+ window.update_window_path(this, path.join(item_dest_path, fsentry.name) + $(this).attr('data-path').slice(moved_from_path.length));
});
}
// The dashboard's Back/Forward follows a moved folder, and
// forgets a trashed one.
if ( $(el_item).attr('data-is_dir') === '1' ) {
- window.dashboard_object?.folderMoved?.($(el_item).attr('data-path'), is_trashing ? null : fsentry.path);
+ window.dashboard_object?.folderMoved?.(moved_from_path, is_trashing ? null : fsentry.path);
}
if ( is_trashing ) {
@@ -2032,7 +2027,7 @@ window.move_items = async function (el_items, dest_path, is_undo = false) {
// create new item on matching containers
const options = {
- appendTo: $(`.item-container[data-path="${html_encode(item_dest_path)}" i]`),
+ appendTo: atPath('.item-container', item_dest_path),
immutable: fsentry.immutable || (fsentry.writable === false),
associated_app_name: fsentry.associated_app?.name,
uid: fsentry.uid,
@@ -2063,8 +2058,8 @@ window.move_items = async function (el_items, dest_path, is_undo = false) {
// see if any of the directories in the path of this file is new AND
// if these new path have any open parents that need to be updated
resp.parent_dirs_created?.forEach(async dir => {
- let item_container = $(`.item-container[data-path="${html_encode(path.dirname(dir.path))}" i]`);
- if ( item_container.length > 0 && $(`.item[data-path="${html_encode(dir.path)}" i]`).length === 0 ) {
+ let item_container = atPath('.item-container', path.dirname(dir.path));
+ if ( item_container.length > 0 && atPath('.item', dir.path).length === 0 ) {
UIItem({
appendTo: item_container,
immutable: false,
@@ -2090,7 +2085,7 @@ window.move_items = async function (el_items, dest_path, is_undo = false) {
});
//sort each container
- $(`.item-container[data-path="${html_encode(item_dest_path)}" i]`).each(function () {
+ atPath('.item-container', item_dest_path).each(function () {
window.sort_items(this, $(this).attr('data-sort_by'), $(this).attr('data-sort_order'));
});
} catch ( err ) {
@@ -2285,9 +2280,7 @@ window.updateSubdomainsForItems = async function (fsentries, container) {
// If not found by uid, try path-based search
if ( $item.length === 0 && fsentry.path ) {
- // Escape special characters in path for jQuery selector
- const escapedPath = fsentry.path.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, '\\$&');
- $item = $(document).find(`.item[data-path="${escapedPath}"]`);
+ $item = atPath('.item', fsentry.path);
}
if ( $item.length > 0 ) {
@@ -3301,20 +3294,7 @@ window.rename_file = async (options, new_name, old_name, old_path, el_item, el_i
$(`.item[data-uid='${$(el_item).attr('data-uid')}']`).attr('data-path', new_path);
$(`.window-${options.uid}`).attr('data-path', new_path);
- // Update all elements that have matching paths
- $(`[data-path="${html_encode(old_path)}" i]`).each(function () {
- $(this).attr('data-path', new_path);
- if ( $(this).hasClass('window-navbar-path-dirname') )
- {
- $(this).text(new_name);
- }
- });
-
- // Update the paths of all elements whose paths start with `old_path`
- $(`[data-path^="${`${html_encode(old_path) }/`}"]`).each(function () {
- const new_el_path = $(this).attr('data-path').replace(`${old_path }/`, `${new_path}/`);
- $(this).attr('data-path', new_el_path);
- });
+ retargetPaths(old_path, new_path, new_name);
// Update the 'Sites Cache'
if ( $(el_item).attr('data-has_website') === '1' )
diff --git a/src/gui/src/helpers.pathElements.test.js b/src/gui/src/helpers.pathElements.test.js
new file mode 100644
index 000000000..85fccf320
--- /dev/null
+++ b/src/gui/src/helpers.pathElements.test.js
@@ -0,0 +1,148 @@
+// @vitest-environment jsdom
+/*
+ * Copyright (C) 2024-present Puter Technologies Inc.
+ *
+ * This file is part of Puter.
+ *
+ * Puter is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see