Skip to content

chore: rewrite set_class() to handle directives #15352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Feb 24, 2025
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tweak
  • Loading branch information
Rich-Harris committed Feb 24, 2025
commit 501f726efb023147b4330082202104433f4354cd
34 changes: 17 additions & 17 deletions packages/svelte/src/internal/shared/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function clsx(value) {
}
}

const white_spaces = [...' \t\n\r\f\u00a0\u000b\ufeff'];
const whitespace = [...' \t\n\r\f\u00a0\u000b\ufeff'];

/**
* @param {any} value
Expand All @@ -50,39 +50,39 @@ const white_spaces = [...' \t\n\r\f\u00a0\u000b\ufeff'];
* @returns {string | null}
*/
export function to_class(value, hash, directives) {
let class_name = value == null ? '' : '' + value;
var classname = value == null ? '' : '' + value;

if (hash) {
class_name = class_name ? class_name + ' ' + hash : hash;
classname = classname ? classname + ' ' + hash : hash;
}

if (directives) {
for (const key in directives) {
for (var key in directives) {
if (directives[key]) {
class_name = class_name ? class_name + ' ' + key : key;
} else if (class_name.length) {
const len = key.length;
let start = 0;
classname = classname ? classname + ' ' + key : key;
} else if (classname.length) {
var len = key.length;
var a = 0;

while ((start = class_name.indexOf(key, start)) >= 0) {
let stop = start + len;
while ((a = classname.indexOf(key, a)) >= 0) {
var b = a + len;

if (
white_spaces.includes(class_name[start - 1] ?? ' ') &&
white_spaces.includes(class_name[stop] ?? ' ')
(a === 0 || whitespace.includes(classname[a - 1])) &&
(b === classname.length || whitespace.includes(classname[b]))
) {
class_name = (
class_name.substring(0, start).trim() +
classname = (
classname.substring(0, a).trim() +
' ' +
class_name.substring(stop).trim()
classname.substring(b).trim()
).trim();
} else {
start = stop;
a = b;
}
}
}
}
}

return class_name === '' ? null : class_name;
return classname === '' ? null : classname;
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy