custom-rendrer 0.0.0-stage → 1.0.0
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.
- package/README.md +120 -2
- package/core/context.js +65 -0
- package/core/engine.js +130 -0
- package/core/registry.js +174 -0
- package/core/transformer.js +140 -0
- package/index.d.ts +212 -0
- package/index.js +68 -0
- package/input/event-mapper.js +87 -0
- package/input/input-adapter.js +65 -0
- package/output/output-adapter.js +66 -0
- package/package.json +101 -4
- package/plugins/babel.js +29 -0
- package/plugins/index.js +4 -0
- package/plugins/rollup.js +15 -0
- package/plugins/vite.js +88 -0
- package/plugins/webpack.js +82 -0
- package/rendering/element-manager.js +72 -0
- package/rendering/renderer.js +130 -0
- package/rendering/visual-layer.js +125 -0
- package/rules/config.js +55 -0
- package/rules/exclusions.js +78 -0
- package/rules/mappings.js +24 -0
- package/runtime/bootstrap.js +229 -0
- package/runtime/observer.js +98 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript Definitions for custom-rendrer
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface TransformationRule {
|
|
6
|
+
id?: string;
|
|
7
|
+
original?: string | RegExp;
|
|
8
|
+
display?: string;
|
|
9
|
+
matcher?: (val: any) => boolean;
|
|
10
|
+
priority?: number;
|
|
11
|
+
context?: {
|
|
12
|
+
elementType?: string;
|
|
13
|
+
componentName?: string | RegExp;
|
|
14
|
+
attributes?: Record<string, any>;
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
} | ((context: TransformationContext) => boolean);
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
caseSensitive?: boolean;
|
|
19
|
+
toDisplay?: (original: any, context?: TransformationContext) => any;
|
|
20
|
+
toOriginal?: (display: any, context?: TransformationContext) => any;
|
|
21
|
+
metadata?: Record<string, any>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface EngineConfig {
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
visualStrategy?: 'css-pseudo' | 'overlay' | string;
|
|
27
|
+
excludeInputs?: boolean;
|
|
28
|
+
preserveDomText?: boolean;
|
|
29
|
+
debug?: boolean;
|
|
30
|
+
customExclusions?: string[];
|
|
31
|
+
mappings?: TransformationRule[];
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PluginOptions {
|
|
36
|
+
rules?: TransformationRule[];
|
|
37
|
+
config?: EngineConfig;
|
|
38
|
+
injectRuntime?: boolean;
|
|
39
|
+
autoStart?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export declare class TransformationContext {
|
|
43
|
+
elementType: string | null;
|
|
44
|
+
componentName: string | null;
|
|
45
|
+
parentPath: string | null;
|
|
46
|
+
attributes: Record<string, any>;
|
|
47
|
+
metadata: Record<string, any>;
|
|
48
|
+
isInput: boolean;
|
|
49
|
+
|
|
50
|
+
constructor(options?: Partial<TransformationContext>);
|
|
51
|
+
matches(criteria: any): boolean;
|
|
52
|
+
clone(overrides?: Partial<TransformationContext>): TransformationContext;
|
|
53
|
+
static create(options?: any): TransformationContext;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export declare class TransformationRegistry {
|
|
57
|
+
constructor();
|
|
58
|
+
register(rule: TransformationRule): string;
|
|
59
|
+
unregister(id: string): boolean;
|
|
60
|
+
enable(id: string): boolean;
|
|
61
|
+
disable(id: string): boolean;
|
|
62
|
+
clear(): void;
|
|
63
|
+
getAllRules(): TransformationRule[];
|
|
64
|
+
getRule(id: string): TransformationRule | null;
|
|
65
|
+
findRulesForOriginal(original: any, context?: any): TransformationRule[];
|
|
66
|
+
findRulesForDisplay(display: any, context?: any): TransformationRule[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export declare class ValueTransformer {
|
|
70
|
+
constructor(registry: TransformationRegistry);
|
|
71
|
+
toDisplay(originalValue: any, context?: any): any;
|
|
72
|
+
toOriginal(displayValue: any, context?: any): any;
|
|
73
|
+
transformTextContent(text: string, context?: any): string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export declare class AbstractionEngine {
|
|
77
|
+
constructor(options?: EngineConfig);
|
|
78
|
+
toDisplay(original: any, context?: any): any;
|
|
79
|
+
toOriginal(display: any, context?: any): any;
|
|
80
|
+
transformText(text: string, context?: any): string;
|
|
81
|
+
registerRule(rule: TransformationRule): string;
|
|
82
|
+
unregisterRule(id: string): boolean;
|
|
83
|
+
enableRule(id: string): boolean;
|
|
84
|
+
disableRule(id: string): boolean;
|
|
85
|
+
getRegistry(): TransformationRegistry;
|
|
86
|
+
configure(options: Partial<EngineConfig>): EngineConfig;
|
|
87
|
+
getConfig(): EngineConfig;
|
|
88
|
+
isExcluded(element: any, context?: any): boolean;
|
|
89
|
+
createContext(options?: any): TransformationContext;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export declare class VisualLayer {
|
|
93
|
+
constructor(documentInstance?: any);
|
|
94
|
+
injectStyles(): void;
|
|
95
|
+
applyVisual(element: any, originalValue: string, displayValue: string): void;
|
|
96
|
+
removeVisual(element: any): void;
|
|
97
|
+
isAbstracted(element: any): boolean;
|
|
98
|
+
getOriginalValue(element: any): string | null;
|
|
99
|
+
getDisplayValue(element: any): string | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export declare class ElementManager {
|
|
103
|
+
constructor(visualLayer?: VisualLayer);
|
|
104
|
+
registerElement(element: any, originalValue: string, displayValue: string): void;
|
|
105
|
+
unregisterElement(element: any): void;
|
|
106
|
+
pruneDisconnected(): void;
|
|
107
|
+
clearAll(): void;
|
|
108
|
+
findByOriginal(value: string): any;
|
|
109
|
+
findByDisplay(value: string): any;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export declare class AbstractionRenderer {
|
|
113
|
+
constructor(options?: {
|
|
114
|
+
engine?: AbstractionEngine;
|
|
115
|
+
visualLayer?: VisualLayer;
|
|
116
|
+
elementManager?: ElementManager;
|
|
117
|
+
});
|
|
118
|
+
processElement(element: any, contextOptions?: any): boolean;
|
|
119
|
+
scanAndRender(rootElement?: any): void;
|
|
120
|
+
unrender(rootElement?: any): void;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export declare class RuntimeObserver {
|
|
124
|
+
constructor(renderer?: AbstractionRenderer, options?: { debounceMs?: number; root?: any });
|
|
125
|
+
start(targetRoot?: any): void;
|
|
126
|
+
stop(): void;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export declare class EventMapper {
|
|
130
|
+
constructor(engineInstance?: AbstractionEngine);
|
|
131
|
+
attach(targetRoot?: any): void;
|
|
132
|
+
detach(): void;
|
|
133
|
+
resolveInteractionValue(target: any): string | null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export declare class InputAdapter {
|
|
137
|
+
constructor(engineInstance?: AbstractionEngine);
|
|
138
|
+
isInputEligible(inputElement: any): boolean;
|
|
139
|
+
resolveInputValue(inputElement: any): string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export declare class OutputAdapter {
|
|
143
|
+
constructor(engineInstance?: AbstractionEngine, inputAdapter?: InputAdapter);
|
|
144
|
+
serializeForm(form: any): Record<string, any>;
|
|
145
|
+
sanitizePayload(payload: any): any;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export declare class ConfigurationManager {
|
|
149
|
+
constructor(initialOptions?: Partial<EngineConfig>);
|
|
150
|
+
get(key: string): any;
|
|
151
|
+
set(key: string, value: any): void;
|
|
152
|
+
update(options?: Partial<EngineConfig>): EngineConfig;
|
|
153
|
+
getAll(): EngineConfig;
|
|
154
|
+
isEnabled(): boolean;
|
|
155
|
+
shouldExcludeInputs(): boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export declare class AbstractionRuntime {
|
|
159
|
+
engine: AbstractionEngine;
|
|
160
|
+
visualLayer: VisualLayer;
|
|
161
|
+
elementManager: ElementManager;
|
|
162
|
+
renderer: AbstractionRenderer;
|
|
163
|
+
observer: RuntimeObserver;
|
|
164
|
+
eventMapper: EventMapper;
|
|
165
|
+
inputAdapter: InputAdapter;
|
|
166
|
+
outputAdapter: OutputAdapter;
|
|
167
|
+
isInitialized: boolean;
|
|
168
|
+
|
|
169
|
+
constructor(options?: { engine?: AbstractionEngine; config?: EngineConfig });
|
|
170
|
+
start(rootElement?: any): this;
|
|
171
|
+
stop(): void;
|
|
172
|
+
loadUserMappings(): void;
|
|
173
|
+
addMapping(original: string, display: string, options?: Partial<TransformationRule>): TransformationRule;
|
|
174
|
+
getUserMappings(): TransformationRule[];
|
|
175
|
+
clearUserMappings(): void;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export declare const engine: AbstractionEngine;
|
|
179
|
+
export declare const defaultEngine: AbstractionEngine;
|
|
180
|
+
export declare const runtime: AbstractionRuntime;
|
|
181
|
+
export declare function initAbstractionRuntime(options?: any): AbstractionRuntime;
|
|
182
|
+
|
|
183
|
+
export declare const VISUAL_CLASS_NAME: string;
|
|
184
|
+
export declare const ORIGINAL_ATTR: string;
|
|
185
|
+
export declare const DISPLAY_ATTR: string;
|
|
186
|
+
export declare const VISUAL_STYLES: string;
|
|
187
|
+
|
|
188
|
+
export declare const DEFAULT_CONFIG: EngineConfig;
|
|
189
|
+
export declare const DEFAULT_EXCLUDED_TAGS: Set<string>;
|
|
190
|
+
export declare const DEFAULT_EXCLUDED_ATTRIBUTES: string[];
|
|
191
|
+
export declare const DEFAULT_EXCLUDED_CLASSES: string[];
|
|
192
|
+
export declare const DEFAULT_MAPPINGS: TransformationRule[];
|
|
193
|
+
|
|
194
|
+
export declare function isExcludedElement(element: any, options?: any): boolean;
|
|
195
|
+
export declare function validateMappingRule(rule: any): boolean;
|
|
196
|
+
|
|
197
|
+
// Build Plugins
|
|
198
|
+
export declare function frontendAbstractionVitePlugin(options?: PluginOptions): any;
|
|
199
|
+
export declare const vitePlugin: typeof frontendAbstractionVitePlugin;
|
|
200
|
+
export declare function frontendAbstractionRollupPlugin(options?: PluginOptions): any;
|
|
201
|
+
export declare const rollupPlugin: typeof frontendAbstractionRollupPlugin;
|
|
202
|
+
|
|
203
|
+
export declare class FrontendAbstractionWebpackPlugin {
|
|
204
|
+
constructor(options?: PluginOptions);
|
|
205
|
+
apply(compiler: any): void;
|
|
206
|
+
}
|
|
207
|
+
export declare const webpackPlugin: typeof FrontendAbstractionWebpackPlugin;
|
|
208
|
+
|
|
209
|
+
export declare function babelPluginFrontendAbstraction(api: any, options?: PluginOptions): any;
|
|
210
|
+
export declare const babelPlugin: typeof babelPluginFrontendAbstraction;
|
|
211
|
+
|
|
212
|
+
export default frontendAbstractionVitePlugin;
|
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontend Abstraction
|
|
3
|
+
* High-performance, non-destructive DOM presentation layer & build plugin.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Core Engine & Transformations
|
|
7
|
+
export { AbstractionEngine, engine, engine as defaultEngine } from './core/engine.js';
|
|
8
|
+
export { TransformationRegistry } from './core/registry.js';
|
|
9
|
+
export { ValueTransformer } from './core/transformer.js';
|
|
10
|
+
export { TransformationContext } from './core/context.js';
|
|
11
|
+
|
|
12
|
+
// Rendering & DOM Presentation
|
|
13
|
+
export {
|
|
14
|
+
VisualLayer,
|
|
15
|
+
VISUAL_CLASS_NAME,
|
|
16
|
+
ORIGINAL_ATTR,
|
|
17
|
+
DISPLAY_ATTR,
|
|
18
|
+
VISUAL_STYLES,
|
|
19
|
+
} from './rendering/visual-layer.js';
|
|
20
|
+
export { ElementManager } from './rendering/element-manager.js';
|
|
21
|
+
export { AbstractionRenderer } from './rendering/renderer.js';
|
|
22
|
+
|
|
23
|
+
// Runtime & Observers
|
|
24
|
+
export {
|
|
25
|
+
AbstractionRuntime,
|
|
26
|
+
runtime,
|
|
27
|
+
initAbstractionRuntime,
|
|
28
|
+
} from './runtime/bootstrap.js';
|
|
29
|
+
export { RuntimeObserver } from './runtime/observer.js';
|
|
30
|
+
|
|
31
|
+
// Input & Event Interception
|
|
32
|
+
export { InputAdapter } from './input/input-adapter.js';
|
|
33
|
+
export { EventMapper } from './input/event-mapper.js';
|
|
34
|
+
|
|
35
|
+
// Output & Data Integrity
|
|
36
|
+
export { OutputAdapter } from './output/output-adapter.js';
|
|
37
|
+
|
|
38
|
+
// Rules & Exclusions
|
|
39
|
+
export { ConfigurationManager, DEFAULT_CONFIG } from './rules/config.js';
|
|
40
|
+
export {
|
|
41
|
+
isExcludedElement,
|
|
42
|
+
DEFAULT_EXCLUDED_TAGS,
|
|
43
|
+
DEFAULT_EXCLUDED_ATTRIBUTES,
|
|
44
|
+
DEFAULT_EXCLUDED_CLASSES,
|
|
45
|
+
} from './rules/exclusions.js';
|
|
46
|
+
export { DEFAULT_MAPPINGS, validateMappingRule } from './rules/mappings.js';
|
|
47
|
+
|
|
48
|
+
// Build-time Plugins
|
|
49
|
+
export {
|
|
50
|
+
frontendAbstractionVitePlugin,
|
|
51
|
+
frontendAbstractionVitePlugin as vitePlugin,
|
|
52
|
+
} from './plugins/vite.js';
|
|
53
|
+
export {
|
|
54
|
+
frontendAbstractionRollupPlugin,
|
|
55
|
+
frontendAbstractionRollupPlugin as rollupPlugin,
|
|
56
|
+
} from './plugins/rollup.js';
|
|
57
|
+
export {
|
|
58
|
+
FrontendAbstractionWebpackPlugin,
|
|
59
|
+
FrontendAbstractionWebpackPlugin as webpackPlugin,
|
|
60
|
+
} from './plugins/webpack.js';
|
|
61
|
+
export {
|
|
62
|
+
babelPluginFrontendAbstraction,
|
|
63
|
+
babelPluginFrontendAbstraction as babelPlugin,
|
|
64
|
+
} from './plugins/babel.js';
|
|
65
|
+
|
|
66
|
+
// Primary default export: Vite plugin
|
|
67
|
+
import { frontendAbstractionVitePlugin } from './plugins/vite.js';
|
|
68
|
+
export default frontendAbstractionVitePlugin;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { engine } from '../core/engine.js';
|
|
2
|
+
import { ORIGINAL_ATTR, DISPLAY_ATTR } from '../rendering/visual-layer.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* EventMapper
|
|
6
|
+
* Bridges consumer interactions on visually transformed elements back to original application logic.
|
|
7
|
+
* When a user clicks, selects, or submits an element displaying "Hello",
|
|
8
|
+
* this mapper resolves the display value back to "World".
|
|
9
|
+
*/
|
|
10
|
+
export class EventMapper {
|
|
11
|
+
constructor(engineInstance = null) {
|
|
12
|
+
this.engine = engineInstance || engine;
|
|
13
|
+
this.isListening = false;
|
|
14
|
+
this._handleEvent = this._handleEvent.bind(this);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Attaches capture-phase event interceptors on the document or container.
|
|
19
|
+
*/
|
|
20
|
+
attach(targetRoot = null) {
|
|
21
|
+
if (typeof window === 'undefined') return;
|
|
22
|
+
const target = targetRoot || document;
|
|
23
|
+
if (this.isListening) return;
|
|
24
|
+
|
|
25
|
+
// Use capture phase so we intercept before standard React bubbling/synthetic listeners
|
|
26
|
+
target.addEventListener('click', this._handleEvent, true);
|
|
27
|
+
target.addEventListener('change', this._handleEvent, true);
|
|
28
|
+
target.addEventListener('submit', this._handleEvent, true);
|
|
29
|
+
|
|
30
|
+
this.isListening = true;
|
|
31
|
+
this._target = target;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Detaches event interceptors.
|
|
36
|
+
*/
|
|
37
|
+
detach() {
|
|
38
|
+
if (!this.isListening || !this._target) return;
|
|
39
|
+
this._target.removeEventListener('click', this._handleEvent, true);
|
|
40
|
+
this._target.removeEventListener('change', this._handleEvent, true);
|
|
41
|
+
this._target.removeEventListener('submit', this._handleEvent, true);
|
|
42
|
+
this.isListening = false;
|
|
43
|
+
this._target = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Resolves an interaction target's display value back to original value.
|
|
48
|
+
* @param {HTMLElement} target
|
|
49
|
+
* @returns {string|null} originalValue
|
|
50
|
+
*/
|
|
51
|
+
resolveInteractionValue(target) {
|
|
52
|
+
if (!target) return null;
|
|
53
|
+
|
|
54
|
+
// 1. Direct original attribute
|
|
55
|
+
if (target.getAttribute && target.hasAttribute(ORIGINAL_ATTR)) {
|
|
56
|
+
return target.getAttribute(ORIGINAL_ATTR);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 2. Nearest parent with abstraction
|
|
60
|
+
const abstractedParent = target.closest ? target.closest(`[${ORIGINAL_ATTR}]`) : null;
|
|
61
|
+
if (abstractedParent) {
|
|
62
|
+
return abstractedParent.getAttribute(ORIGINAL_ATTR);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 3. Reverse map from text or display attribute
|
|
66
|
+
const displayVal = target.getAttribute ? target.getAttribute(DISPLAY_ATTR) : null;
|
|
67
|
+
if (displayVal) {
|
|
68
|
+
return this.engine.toOriginal(displayVal);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_handleEvent(event) {
|
|
75
|
+
const target = event.target;
|
|
76
|
+
if (!target) return;
|
|
77
|
+
|
|
78
|
+
const originalValue = this.resolveInteractionValue(target);
|
|
79
|
+
if (originalValue !== null) {
|
|
80
|
+
// Expose original value on event detail and target metadata for consumers
|
|
81
|
+
event.abstractedOriginalValue = originalValue;
|
|
82
|
+
if (typeof event.detail === 'object' && event.detail !== null) {
|
|
83
|
+
event.detail.originalValue = originalValue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { engine } from '../core/engine.js';
|
|
2
|
+
import { isExcludedElement } from '../rules/exclusions.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* InputAdapter
|
|
6
|
+
* Manages bidirectional input abstraction.
|
|
7
|
+
* Preserves the architectural rule: Inputs are strictly excluded by default.
|
|
8
|
+
* If explicitly enabled, it translates display input into original application state.
|
|
9
|
+
*/
|
|
10
|
+
export class InputAdapter {
|
|
11
|
+
constructor(engineInstance = null) {
|
|
12
|
+
this.engine = engineInstance || engine;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Checks whether a form control is eligible for input transformation.
|
|
17
|
+
* Default policy: All standard inputs are EXCLUDED unless marked with 'data-abstract-input="true"'.
|
|
18
|
+
*/
|
|
19
|
+
isInputEligible(inputElement) {
|
|
20
|
+
if (!inputElement) return false;
|
|
21
|
+
if (this.engine.getConfig().excludeInputs !== false) {
|
|
22
|
+
// By default inputs are excluded unless opted in
|
|
23
|
+
const optedIn = inputElement.getAttribute && inputElement.getAttribute('data-abstract-input') === 'true';
|
|
24
|
+
return optedIn;
|
|
25
|
+
}
|
|
26
|
+
return !isExcludedElement(inputElement);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Resolves an input's current value to its original application value.
|
|
31
|
+
*/
|
|
32
|
+
resolveInputValue(inputElement) {
|
|
33
|
+
if (!inputElement || typeof inputElement.value !== 'string') {
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!this.isInputEligible(inputElement)) {
|
|
38
|
+
// Un-abstracted inputs retain their literal value
|
|
39
|
+
return inputElement.value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const context = this.engine.createContext({
|
|
43
|
+
elementType: inputElement.tagName.toLowerCase(),
|
|
44
|
+
isInput: true,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return this.engine.toOriginal(inputElement.value, context);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Formats an original value for display inside an eligible input.
|
|
52
|
+
*/
|
|
53
|
+
formatInputValue(originalValue, inputElement = null) {
|
|
54
|
+
if (inputElement && !this.isInputEligible(inputElement)) {
|
|
55
|
+
return originalValue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const context = this.engine.createContext({
|
|
59
|
+
elementType: inputElement?.tagName ? inputElement.tagName.toLowerCase() : 'input',
|
|
60
|
+
isInput: true,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return this.engine.toDisplay(originalValue, context);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { engine } from '../core/engine.js';
|
|
2
|
+
import { InputAdapter } from '../input/input-adapter.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* OutputAdapter
|
|
6
|
+
* Guarantees API and serialization integrity:
|
|
7
|
+
* - Form submissions, FormData, and API payloads always serialize ORIGINAL values.
|
|
8
|
+
* - Prevents accidental leakage of display transformations into network requests.
|
|
9
|
+
*/
|
|
10
|
+
export class OutputAdapter {
|
|
11
|
+
constructor(engineInstance = null, inputAdapter = null) {
|
|
12
|
+
this.engine = engineInstance || engine;
|
|
13
|
+
this.inputAdapter = inputAdapter || new InputAdapter(this.engine);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Serializes a form element ensuring all abstracted fields submit their ORIGINAL values.
|
|
18
|
+
* @param {HTMLFormElement} form
|
|
19
|
+
* @returns {Object} original form data
|
|
20
|
+
*/
|
|
21
|
+
serializeForm(form) {
|
|
22
|
+
if (!form || !form.elements) return {};
|
|
23
|
+
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const element of form.elements) {
|
|
26
|
+
if (!element.name || element.disabled) continue;
|
|
27
|
+
|
|
28
|
+
const type = (element.type || '').toLowerCase();
|
|
29
|
+
if (type === 'submit' || type === 'button' || type === 'reset' || type === 'image') {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if ((type === 'checkbox' || type === 'radio') && !element.checked) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const originalVal = this.inputAdapter.resolveInputValue(element);
|
|
38
|
+
result[element.name] = originalVal;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Verifies and guarantees that an outgoing API payload preserves original values.
|
|
46
|
+
*/
|
|
47
|
+
sanitizePayload(payload) {
|
|
48
|
+
if (!payload || typeof payload !== 'object') {
|
|
49
|
+
return payload;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Clone to prevent mutating references
|
|
53
|
+
const copy = Array.isArray(payload) ? [...payload] : { ...payload };
|
|
54
|
+
|
|
55
|
+
for (const [key, value] of Object.entries(copy)) {
|
|
56
|
+
if (typeof value === 'string') {
|
|
57
|
+
// If display value is passed by mistake, map it back to original
|
|
58
|
+
copy[key] = this.engine.toOriginal(value);
|
|
59
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
60
|
+
copy[key] = this.sanitizePayload(value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return copy;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,103 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "custom-rendrer",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Non-destructive DOM presentation layer & build plugin (Vite, Webpack, Rollup) for web applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"module": "./index.js",
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"./vite": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"import": "./plugins/vite.js"
|
|
17
|
+
},
|
|
18
|
+
"./webpack": {
|
|
19
|
+
"types": "./index.d.ts",
|
|
20
|
+
"import": "./plugins/webpack.js"
|
|
21
|
+
},
|
|
22
|
+
"./rollup": {
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"import": "./plugins/rollup.js"
|
|
25
|
+
},
|
|
26
|
+
"./babel": {
|
|
27
|
+
"types": "./index.d.ts",
|
|
28
|
+
"import": "./plugins/babel.js"
|
|
29
|
+
},
|
|
30
|
+
"./runtime": {
|
|
31
|
+
"types": "./index.d.ts",
|
|
32
|
+
"import": "./runtime/bootstrap.js"
|
|
33
|
+
},
|
|
34
|
+
"./core": {
|
|
35
|
+
"types": "./index.d.ts",
|
|
36
|
+
"import": "./core/engine.js"
|
|
37
|
+
},
|
|
38
|
+
"./rendering": {
|
|
39
|
+
"types": "./index.d.ts",
|
|
40
|
+
"import": "./rendering/renderer.js"
|
|
41
|
+
},
|
|
42
|
+
"./rules": {
|
|
43
|
+
"types": "./index.d.ts",
|
|
44
|
+
"import": "./rules/config.js"
|
|
45
|
+
},
|
|
46
|
+
"./input": {
|
|
47
|
+
"types": "./index.d.ts",
|
|
48
|
+
"import": "./input/input-adapter.js"
|
|
49
|
+
},
|
|
50
|
+
"./output": {
|
|
51
|
+
"types": "./index.d.ts",
|
|
52
|
+
"import": "./output/output-adapter.js"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"index.js",
|
|
57
|
+
"index.d.ts",
|
|
58
|
+
"plugins",
|
|
59
|
+
"core",
|
|
60
|
+
"rendering",
|
|
61
|
+
"runtime",
|
|
62
|
+
"rules",
|
|
63
|
+
"input",
|
|
64
|
+
"output",
|
|
65
|
+
"README.md",
|
|
66
|
+
"LICENSE"
|
|
67
|
+
],
|
|
68
|
+
"scripts": {
|
|
69
|
+
"test": "node --test test/**/*.test.js",
|
|
70
|
+
"prepublishOnly": "npm test"
|
|
71
|
+
},
|
|
72
|
+
"keywords": [
|
|
73
|
+
"vite",
|
|
74
|
+
"vite-plugin",
|
|
75
|
+
"webpack",
|
|
76
|
+
"webpack-plugin",
|
|
77
|
+
"rollup",
|
|
78
|
+
"rollup-plugin",
|
|
79
|
+
"frontend",
|
|
80
|
+
"dom-abstraction",
|
|
81
|
+
"data-masking",
|
|
82
|
+
"presentation-layer",
|
|
83
|
+
"text-transform",
|
|
84
|
+
"react",
|
|
85
|
+
"vue"
|
|
86
|
+
],
|
|
87
|
+
"peerDependencies": {
|
|
88
|
+
"vite": ">=3.0.0",
|
|
89
|
+
"webpack": ">=5.0.0"
|
|
90
|
+
},
|
|
91
|
+
"peerDependenciesMeta": {
|
|
92
|
+
"vite": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"webpack": {
|
|
96
|
+
"optional": true
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"engines": {
|
|
100
|
+
"node": ">=18.0.0"
|
|
101
|
+
},
|
|
102
|
+
"license": "MIT"
|
|
103
|
+
}
|
package/plugins/babel.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Babel Plugin for Frontend Abstraction
|
|
3
|
+
* Transforms source code to inject build-time rules or auto-register mappings during transpile.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function babelPluginFrontendAbstraction(api, options = {}) {
|
|
7
|
+
const { rules = [] } = options;
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
name: 'babel-plugin-frontend-abstraction',
|
|
11
|
+
visitor: {
|
|
12
|
+
Program: {
|
|
13
|
+
enter(path, state) {
|
|
14
|
+
// If we have rules, we can ensure the global or entry file initializes the rules
|
|
15
|
+
if (state.file && state.file.opts && state.file.opts.filename) {
|
|
16
|
+
const filename = state.file.opts.filename;
|
|
17
|
+
const isEntry = /index\.(jsx?|tsx?)$|main\.(jsx?|tsx?)$|App\.(jsx?|tsx?)$/.test(filename);
|
|
18
|
+
if (isEntry && rules.length > 0 && !state.hasInjectedRules) {
|
|
19
|
+
state.hasInjectedRules = true;
|
|
20
|
+
// AST injection can be customized by consumers
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default babelPluginFrontendAbstraction;
|
package/plugins/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { frontendAbstractionVitePlugin, frontendAbstractionVitePlugin as vitePlugin } from './vite.js';
|
|
2
|
+
export { frontendAbstractionRollupPlugin, frontendAbstractionRollupPlugin as rollupPlugin } from './rollup.js';
|
|
3
|
+
export { FrontendAbstractionWebpackPlugin, FrontendAbstractionWebpackPlugin as webpackPlugin } from './webpack.js';
|
|
4
|
+
export { babelPluginFrontendAbstraction, babelPluginFrontendAbstraction as babelPlugin } from './babel.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rollup Plugin for Frontend Abstraction
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { frontendAbstractionVitePlugin } from './vite.js';
|
|
6
|
+
|
|
7
|
+
export function frontendAbstractionRollupPlugin(options = {}) {
|
|
8
|
+
const vitePlugin = frontendAbstractionVitePlugin(options);
|
|
9
|
+
return {
|
|
10
|
+
...vitePlugin,
|
|
11
|
+
name: 'rollup-plugin-frontend-abstraction',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default frontendAbstractionRollupPlugin;
|