@editora/toast 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +14 -0
  2. package/dist/index.d.ts +18 -0
  3. package/dist/index.js +70 -0
  4. package/dist/packages/core/src/Editor.d.ts +15 -0
  5. package/dist/packages/core/src/Editor.js +37 -0
  6. package/dist/packages/core/src/EditorState.d.ts +13 -0
  7. package/dist/packages/core/src/EditorState.js +14 -0
  8. package/dist/packages/core/src/Selection.d.ts +126 -0
  9. package/dist/packages/core/src/Selection.js +232 -0
  10. package/dist/packages/core/src/commands/CommandManager.d.ts +56 -0
  11. package/dist/packages/core/src/commands/CommandManager.js +166 -0
  12. package/dist/packages/core/src/index.d.ts +7 -0
  13. package/dist/packages/core/src/index.js +4 -0
  14. package/dist/packages/core/src/model/Fragment.d.ts +80 -0
  15. package/dist/packages/core/src/model/Fragment.js +172 -0
  16. package/dist/packages/core/src/model/Mark.d.ts +108 -0
  17. package/dist/packages/core/src/model/Mark.js +138 -0
  18. package/dist/packages/core/src/model/Node.d.ts +172 -0
  19. package/dist/packages/core/src/model/Node.js +458 -0
  20. package/dist/packages/core/src/model/types.d.ts +29 -0
  21. package/dist/packages/core/src/model/types.js +1 -0
  22. package/dist/packages/core/src/plugins/Plugin.d.ts +37 -0
  23. package/dist/packages/core/src/plugins/Plugin.js +31 -0
  24. package/dist/packages/core/src/plugins/PluginManager.d.ts +75 -0
  25. package/dist/packages/core/src/plugins/PluginManager.js +185 -0
  26. package/dist/packages/core/src/schema/Node.d.ts +26 -0
  27. package/dist/packages/core/src/schema/Node.js +12 -0
  28. package/dist/packages/core/src/schema/Schema.d.ts +52 -0
  29. package/dist/packages/core/src/schema/Schema.js +296 -0
  30. package/dist/packages/core/src/security/Sanitizer.d.ts +81 -0
  31. package/dist/packages/core/src/security/Sanitizer.js +200 -0
  32. package/dist/packages/editora-toast/src/index.d.ts +18 -0
  33. package/dist/packages/editora-toast/src/index.js +70 -0
  34. package/dist/toast.css +42 -0
  35. package/package.json +27 -0
  36. package/src/index.ts +83 -0
  37. package/src/toast.css +42 -0
@@ -0,0 +1,81 @@
1
+ /**
2
+ * HTML sanitization configuration.
3
+ */
4
+ export interface SanitizeConfig {
5
+ /** Allowed HTML tags */
6
+ allowedTags?: string[];
7
+ /** Allowed HTML attributes */
8
+ allowedAttributes?: Record<string, string[]>;
9
+ /** Whether to allow data URLs */
10
+ allowDataUrls?: boolean;
11
+ /** Maximum URL length */
12
+ maxUrlLength?: number;
13
+ /** Custom URL validator */
14
+ urlValidator?: (url: string) => boolean;
15
+ }
16
+ /**
17
+ * HTML sanitizer for cleaning user input.
18
+ */
19
+ export declare class Sanitizer {
20
+ private config;
21
+ constructor(config?: SanitizeConfig);
22
+ /**
23
+ * Sanitize HTML content.
24
+ */
25
+ sanitize(html: string): string;
26
+ /**
27
+ * Sanitize a DOM element recursively.
28
+ */
29
+ private sanitizeElement;
30
+ /**
31
+ * Check if a tag is allowed.
32
+ */
33
+ private isAllowedTag;
34
+ /**
35
+ * Sanitize element attributes.
36
+ */
37
+ private sanitizeAttributes;
38
+ /**
39
+ * Validate a URL.
40
+ */
41
+ private isValidUrl;
42
+ /**
43
+ * Default URL validator.
44
+ */
45
+ private defaultUrlValidator;
46
+ }
47
+ /**
48
+ * Content validator for editor input.
49
+ */
50
+ export declare class ContentValidator {
51
+ /**
52
+ * Validate text content for potential security issues.
53
+ */
54
+ static validateText(text: string): {
55
+ valid: boolean;
56
+ warnings: string[];
57
+ };
58
+ /**
59
+ * Calculate maximum nesting level in HTML.
60
+ */
61
+ private static calculateNestingLevel;
62
+ /**
63
+ * Validate file upload.
64
+ */
65
+ static validateFile(file: File, options?: {
66
+ maxSize?: number;
67
+ allowedTypes?: string[];
68
+ allowEmpty?: boolean;
69
+ }): {
70
+ valid: boolean;
71
+ error?: string;
72
+ };
73
+ }
74
+ /**
75
+ * Create a sanitizer instance.
76
+ */
77
+ export declare function createSanitizer(config?: SanitizeConfig): Sanitizer;
78
+ /**
79
+ * Default sanitizer instance.
80
+ */
81
+ export declare const defaultSanitizer: Sanitizer;
@@ -0,0 +1,200 @@
1
+ /**
2
+ * HTML sanitizer for cleaning user input.
3
+ */
4
+ export class Sanitizer {
5
+ constructor(config = {}) {
6
+ this.config = Object.assign({ allowedTags: [
7
+ 'p', 'br', 'strong', 'em', 'u', 's', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
8
+ 'ul', 'ol', 'li', 'blockquote', 'code', 'pre', 'a', 'img', 'table', 'tr', 'td', 'th'
9
+ ], allowedAttributes: {
10
+ 'a': ['href', 'title', 'target'],
11
+ 'img': ['src', 'alt', 'title', 'width', 'height'],
12
+ 'td': ['colspan', 'rowspan'],
13
+ 'th': ['colspan', 'rowspan'],
14
+ '*': ['class', 'style']
15
+ }, allowDataUrls: false, maxUrlLength: 2048, urlValidator: (url) => this.defaultUrlValidator(url) }, config);
16
+ }
17
+ /**
18
+ * Sanitize HTML content.
19
+ */
20
+ sanitize(html) {
21
+ // Create a temporary DOM element to parse HTML
22
+ const parser = new DOMParser();
23
+ const doc = parser.parseFromString(html, 'text/html');
24
+ // Sanitize the document
25
+ this.sanitizeElement(doc.body);
26
+ // Return the sanitized HTML
27
+ return doc.body.innerHTML;
28
+ }
29
+ /**
30
+ * Sanitize a DOM element recursively.
31
+ */
32
+ sanitizeElement(element) {
33
+ const children = Array.from(element.children);
34
+ for (const child of children) {
35
+ if (this.isAllowedTag(child.tagName.toLowerCase())) {
36
+ // Sanitize attributes
37
+ this.sanitizeAttributes(child);
38
+ // Recursively sanitize children
39
+ this.sanitizeElement(child);
40
+ }
41
+ else {
42
+ // Remove disallowed elements
43
+ element.removeChild(child);
44
+ }
45
+ }
46
+ }
47
+ /**
48
+ * Check if a tag is allowed.
49
+ */
50
+ isAllowedTag(tagName) {
51
+ return this.config.allowedTags.includes(tagName.toLowerCase());
52
+ }
53
+ /**
54
+ * Sanitize element attributes.
55
+ */
56
+ sanitizeAttributes(element) {
57
+ const attributes = Array.from(element.attributes);
58
+ const tagName = element.tagName.toLowerCase();
59
+ for (const attr of attributes) {
60
+ const attrName = attr.name.toLowerCase();
61
+ // Check if attribute is allowed for this tag
62
+ const allowedForTag = this.config.allowedAttributes[tagName] || [];
63
+ const allowedForAll = this.config.allowedAttributes['*'] || [];
64
+ if (!allowedForTag.includes(attrName) && !allowedForAll.includes(attrName)) {
65
+ element.removeAttribute(attr.name);
66
+ continue;
67
+ }
68
+ // Special validation for URLs
69
+ if (attrName === 'href' || attrName === 'src') {
70
+ if (!this.isValidUrl(attr.value)) {
71
+ element.removeAttribute(attr.name);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ /**
77
+ * Validate a URL.
78
+ */
79
+ isValidUrl(url) {
80
+ // Check length
81
+ if (url.length > this.config.maxUrlLength) {
82
+ return false;
83
+ }
84
+ // Check for data URLs
85
+ if (url.startsWith('data:') && !this.config.allowDataUrls) {
86
+ return false;
87
+ }
88
+ // Use custom validator
89
+ return this.config.urlValidator(url);
90
+ }
91
+ /**
92
+ * Default URL validator.
93
+ */
94
+ defaultUrlValidator(url) {
95
+ try {
96
+ // Allow relative URLs
97
+ if (url.startsWith('/') || url.startsWith('./') || url.startsWith('../') || url.startsWith('#')) {
98
+ return true;
99
+ }
100
+ // Allow mailto and tel URLs
101
+ if (url.startsWith('mailto:') || url.startsWith('tel:')) {
102
+ return true;
103
+ }
104
+ // Validate absolute URLs
105
+ const parsedUrl = new URL(url);
106
+ // Only allow http and https protocols
107
+ if (!['http:', 'https:'].includes(parsedUrl.protocol)) {
108
+ return false;
109
+ }
110
+ // Basic domain validation (no IP addresses that could be malicious)
111
+ const hostname = parsedUrl.hostname;
112
+ if (/^\d+\.\d+\.\d+\.\d+$/.test(hostname)) {
113
+ // Allow localhost for development
114
+ return hostname === 'localhost' || hostname === '127.0.0.1';
115
+ }
116
+ return true;
117
+ }
118
+ catch (_a) {
119
+ // Invalid URL format
120
+ return false;
121
+ }
122
+ }
123
+ }
124
+ /**
125
+ * Content validator for editor input.
126
+ */
127
+ export class ContentValidator {
128
+ /**
129
+ * Validate text content for potential security issues.
130
+ */
131
+ static validateText(text) {
132
+ const warnings = [];
133
+ // Check for suspicious patterns
134
+ if (text.includes('<script') || text.includes('javascript:')) {
135
+ warnings.push('Potential XSS attempt detected');
136
+ }
137
+ // Check for very long words (potential DoS)
138
+ const words = text.split(/\s+/);
139
+ const longWords = words.filter(word => word.length > 1000);
140
+ if (longWords.length > 0) {
141
+ warnings.push('Extremely long words detected');
142
+ }
143
+ // Check for excessive nesting (potential DoS)
144
+ const nestingLevel = this.calculateNestingLevel(text);
145
+ if (nestingLevel > 50) {
146
+ warnings.push('Excessive element nesting detected');
147
+ }
148
+ return {
149
+ valid: warnings.length === 0,
150
+ warnings
151
+ };
152
+ }
153
+ /**
154
+ * Calculate maximum nesting level in HTML.
155
+ */
156
+ static calculateNestingLevel(html) {
157
+ let maxDepth = 0;
158
+ let currentDepth = 0;
159
+ // Simple regex to count opening/closing tags
160
+ const tagRegex = /<\/?[a-zA-Z][^>]*>/g;
161
+ let match;
162
+ while ((match = tagRegex.exec(html)) !== null) {
163
+ const tag = match[0];
164
+ if (tag.startsWith('</')) {
165
+ currentDepth = Math.max(0, currentDepth - 1);
166
+ }
167
+ else if (!tag.endsWith('/>') && !tag.includes('br') && !tag.includes('img') && !tag.includes('input')) {
168
+ currentDepth++;
169
+ maxDepth = Math.max(maxDepth, currentDepth);
170
+ }
171
+ }
172
+ return maxDepth;
173
+ }
174
+ /**
175
+ * Validate file upload.
176
+ */
177
+ static validateFile(file, options = {}) {
178
+ const { maxSize = 10 * 1024 * 1024, allowedTypes = [], allowEmpty = true } = options;
179
+ if (!allowEmpty && file.size === 0) {
180
+ return { valid: false, error: 'File is empty' };
181
+ }
182
+ if (file.size > maxSize) {
183
+ return { valid: false, error: `File size exceeds ${maxSize} bytes` };
184
+ }
185
+ if (allowedTypes.length > 0 && !allowedTypes.includes(file.type)) {
186
+ return { valid: false, error: `File type ${file.type} not allowed` };
187
+ }
188
+ return { valid: true };
189
+ }
190
+ }
191
+ /**
192
+ * Create a sanitizer instance.
193
+ */
194
+ export function createSanitizer(config) {
195
+ return new Sanitizer(config);
196
+ }
197
+ /**
198
+ * Default sanitizer instance.
199
+ */
200
+ export const defaultSanitizer = new Sanitizer();
@@ -0,0 +1,18 @@
1
+ export type ToastLevel = 'info' | 'success' | 'error';
2
+ export interface ToastOptions {
3
+ message: string;
4
+ duration?: number;
5
+ level?: ToastLevel;
6
+ }
7
+ export declare const toast: {
8
+ info: (msg: string, duration?: number) => {
9
+ dismiss: () => void;
10
+ } | undefined;
11
+ success: (msg: string, duration?: number) => {
12
+ dismiss: () => void;
13
+ } | undefined;
14
+ error: (msg: string, duration?: number) => {
15
+ dismiss: () => void;
16
+ } | undefined;
17
+ };
18
+ export default toast;
@@ -0,0 +1,70 @@
1
+ const DEFAULT_DURATION = 4000;
2
+ let container = null;
3
+ function ensureContainer() {
4
+ if (container)
5
+ return container;
6
+ container = document.createElement('div');
7
+ container.className = 'editora-toast-container';
8
+ document.body.appendChild(container);
9
+ // try to inject css if not loaded
10
+ const existing = Array.from(document.styleSheets).find(s => { var _a, _b; return (_b = (_a = s.ownerNode) === null || _a === void 0 ? void 0 : _a.href) === null || _b === void 0 ? void 0 : _b.includes('toast.css'); });
11
+ if (!existing) {
12
+ const link = document.createElement('link');
13
+ link.rel = 'stylesheet';
14
+ link.href = '/packages/editora-toast/dist/toast.css';
15
+ document.head.appendChild(link);
16
+ }
17
+ return container;
18
+ }
19
+ function createToastEl(message, level) {
20
+ const el = document.createElement('div');
21
+ el.className = `editora-toast ${level}`;
22
+ el.textContent = message;
23
+ const close = document.createElement('span');
24
+ close.className = 'close';
25
+ close.textContent = '×';
26
+ close.style.float = 'right';
27
+ close.style.marginLeft = '8px';
28
+ close.onclick = () => {
29
+ // immediate hide for manual close (will mirror show.hide behavior)
30
+ el.classList.remove('show');
31
+ setTimeout(() => {
32
+ try {
33
+ el.remove();
34
+ }
35
+ catch (e) { }
36
+ }, 220);
37
+ };
38
+ el.appendChild(close);
39
+ return el;
40
+ }
41
+ function show(options) {
42
+ if (typeof document === 'undefined')
43
+ return;
44
+ const { message, duration = DEFAULT_DURATION, level = 'info' } = options;
45
+ const cont = ensureContainer();
46
+ const el = createToastEl(message, level);
47
+ cont.appendChild(el);
48
+ // force layout then show
49
+ requestAnimationFrame(() => el.classList.add('show'));
50
+ const timeout = setTimeout(() => hide(el), duration);
51
+ function hide(target) {
52
+ clearTimeout(timeout);
53
+ target.classList.remove('show');
54
+ setTimeout(() => {
55
+ try {
56
+ target.remove();
57
+ }
58
+ catch (e) { }
59
+ }, 220);
60
+ }
61
+ return {
62
+ dismiss: () => hide(el)
63
+ };
64
+ }
65
+ export const toast = {
66
+ info: (msg, duration) => show({ message: msg, level: 'info', duration }),
67
+ success: (msg, duration) => show({ message: msg, level: 'success', duration }),
68
+ error: (msg, duration) => show({ message: msg, level: 'error', duration })
69
+ };
70
+ export default toast;
package/dist/toast.css ADDED
@@ -0,0 +1,42 @@
1
+ /* Simple toast styles for editora-toast */
2
+ .editora-toast-container {
3
+ position: fixed;
4
+ z-index: 2147483647;
5
+ right: 16px;
6
+ bottom: 16px;
7
+ display: flex;
8
+ flex-direction: column;
9
+ gap: 8px;
10
+ pointer-events: none;
11
+ }
12
+
13
+ .editora-toast {
14
+ min-width: 200px;
15
+ max-width: 360px;
16
+ background: rgba(0,0,0,0.85);
17
+ color: #fff;
18
+ padding: 10px 14px;
19
+ border-radius: 6px;
20
+ box-shadow: 0 6px 18px rgba(0,0,0,0.25);
21
+ font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
22
+ font-size: 13px;
23
+ pointer-events: auto;
24
+ opacity: 0;
25
+ transform: translateY(8px) scale(0.98);
26
+ transition: opacity 160ms ease, transform 180ms cubic-bezier(.2,.9,.25,1);
27
+ }
28
+
29
+ .editora-toast.show {
30
+ opacity: 1;
31
+ transform: translateY(0) scale(1);
32
+ }
33
+
34
+ .editora-toast.success { background: #1b8c4a; }
35
+ .editora-toast.error { background: #b02a37; }
36
+ .editora-toast.info { background: #205081; }
37
+
38
+ .editora-toast .close {
39
+ margin-left: 8px;
40
+ opacity: 0.85;
41
+ cursor: pointer;
42
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@editora/toast",
3
+ "version": "1.0.1",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
18
+ "author": "Ajay Kumar <ajaykr089@gmail.com>",
19
+ "license": "MIT",
20
+ "scripts": {
21
+ "build": "tsc --project tsconfig.json && cp src/toast.css dist/toast.css"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.0.0"
25
+ },
26
+ "gitHead": "694494db58b809f0dcf24501696284faa1ab68a5"
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ export type ToastLevel = 'info' | 'success' | 'error';
2
+
3
+ export interface ToastOptions {
4
+ message: string;
5
+ duration?: number; // ms
6
+ level?: ToastLevel;
7
+ }
8
+
9
+ const DEFAULT_DURATION = 4000;
10
+ let container: HTMLElement | null = null;
11
+
12
+ function ensureContainer() {
13
+ if (container) return container;
14
+ container = document.createElement('div');
15
+ container.className = 'editora-toast-container';
16
+ document.body.appendChild(container);
17
+
18
+ // try to inject css if not loaded
19
+ const existing = Array.from(document.styleSheets).find(s => (s.ownerNode as any)?.href?.includes('toast.css'));
20
+ if (!existing) {
21
+ const link = document.createElement('link');
22
+ link.rel = 'stylesheet';
23
+ link.href = '/packages/editora-toast/dist/toast.css';
24
+ document.head.appendChild(link);
25
+ }
26
+
27
+ return container;
28
+ }
29
+
30
+ function createToastEl(message: string, level: ToastLevel) {
31
+ const el = document.createElement('div');
32
+ el.className = `editora-toast ${level}`;
33
+ el.textContent = message;
34
+
35
+ const close = document.createElement('span');
36
+ close.className = 'close';
37
+ close.textContent = '×';
38
+ close.style.float = 'right';
39
+ close.style.marginLeft = '8px';
40
+ close.onclick = () => {
41
+ // immediate hide for manual close (will mirror show.hide behavior)
42
+ el.classList.remove('show');
43
+ setTimeout(() => {
44
+ try { el.remove(); } catch (e) {}
45
+ }, 220);
46
+ };
47
+
48
+ el.appendChild(close);
49
+ return el;
50
+ }
51
+
52
+ function show(options: ToastOptions) {
53
+ if (typeof document === 'undefined') return;
54
+ const { message, duration = DEFAULT_DURATION, level = 'info' } = options;
55
+ const cont = ensureContainer();
56
+ const el = createToastEl(message, level);
57
+ cont.appendChild(el);
58
+
59
+ // force layout then show
60
+ requestAnimationFrame(() => el.classList.add('show'));
61
+
62
+ const timeout = setTimeout(() => hide(el), duration);
63
+
64
+ function hide(target: HTMLElement) {
65
+ clearTimeout(timeout);
66
+ target.classList.remove('show');
67
+ setTimeout(() => {
68
+ try { target.remove(); } catch (e) {}
69
+ }, 220);
70
+ }
71
+
72
+ return {
73
+ dismiss: () => hide(el)
74
+ };
75
+ }
76
+
77
+ export const toast = {
78
+ info: (msg: string, duration?: number) => show({ message: msg, level: 'info', duration }),
79
+ success: (msg: string, duration?: number) => show({ message: msg, level: 'success', duration }),
80
+ error: (msg: string, duration?: number) => show({ message: msg, level: 'error', duration })
81
+ };
82
+
83
+ export default toast;
package/src/toast.css ADDED
@@ -0,0 +1,42 @@
1
+ /* Simple toast styles for editora-toast */
2
+ .editora-toast-container {
3
+ position: fixed;
4
+ z-index: 2147483647;
5
+ right: 16px;
6
+ bottom: 16px;
7
+ display: flex;
8
+ flex-direction: column;
9
+ gap: 8px;
10
+ pointer-events: none;
11
+ }
12
+
13
+ .editora-toast {
14
+ min-width: 200px;
15
+ max-width: 360px;
16
+ background: rgba(0,0,0,0.85);
17
+ color: #fff;
18
+ padding: 10px 14px;
19
+ border-radius: 6px;
20
+ box-shadow: 0 6px 18px rgba(0,0,0,0.25);
21
+ font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
22
+ font-size: 13px;
23
+ pointer-events: auto;
24
+ opacity: 0;
25
+ transform: translateY(8px) scale(0.98);
26
+ transition: opacity 160ms ease, transform 180ms cubic-bezier(.2,.9,.25,1);
27
+ }
28
+
29
+ .editora-toast.show {
30
+ opacity: 1;
31
+ transform: translateY(0) scale(1);
32
+ }
33
+
34
+ .editora-toast.success { background: #1b8c4a; }
35
+ .editora-toast.error { background: #b02a37; }
36
+ .editora-toast.info { background: #205081; }
37
+
38
+ .editora-toast .close {
39
+ margin-left: 8px;
40
+ opacity: 0.85;
41
+ cursor: pointer;
42
+ }