namespace XFESystem.Browser;
/// <summary>Autonomous HTML elements with real DOM identity and bounded upgrade/lifecycle reactions.
/// Customized built-ins, shadow roots and element internals remain unsupported.</summary>
internal static class BrowserDomCustomElements
{
internal const string Source = "\n" + """
const customDefinitions=new Map(),customConstructors=new Map(),customStates=new WeakMap(),customConstruction=[],customWaiters=new Map();let creatingCustom=false;
const reservedCustomNames=new Set(['annotation-xml','color-profile','font-face','font-face-src','font-face-uri','font-face-format','font-face-name','missing-glyph']);
function validCustomName(name){return name.length<=128&&/^[a-z][a-z0-9._-]*-[a-z0-9._-]*$/.test(name)&&!reservedCustomNames.has(name);}
function customReaction(node,name,args=[]){const callback=node[name];if(typeof callback!=='function')return;queueMicrotask(function(){try{const result=callback.apply(node,args);if(result&&typeof result.catch==='function')result.catch(error=>eventError(String(error)));}catch(error){eventError(String(error));}});}
function customConnection(node){const state=customStates.get(node);if(!state||state.failed||state.constructing)return;const connected=!!node.isConnected;if(connected===state.connected)return;state.connected=connected;customReaction(node,connected?'connectedCallback':'disconnectedCallback');}
function upgradeCustomElement(node){if(creatingCustom||!node||node.nodeType!==1||node.namespaceURI!=='http://www.w3.org/1999/xhtml'||customStates.has(node))return;
const definition=customDefinitions.get(node.localName);if(!definition)return;const state={definition,connected:false,constructing:true,failed:false};customStates.set(node,state);Object.setPrototypeOf(node,definition.constructor.prototype);customConstruction.push({node,definition});
try{if(Reflect.construct(definition.constructor,[])!==node)throw new TypeError('A custom element constructor returned another object');}
catch(error){state.failed=true;eventError(String(error));}finally{customConstruction.pop();state.constructing=false;}
if(state.failed)return;for(const name of definition.attributes)if(node.hasAttribute(name))customReaction(node,'attributeChangedCallback',[name,null,node.getAttribute(name),null]);customConnection(node);
}
function customWalk(root,callback){if(!root)return;if(root.nodeType===1)callback(root);for(const child of Array.from(root.children||[]))customWalk(child,callback);}
function customMutation(target,type,name,oldValue,added,removed){if(!customDefinitions.size)return;
if(type==='attributes'){const state=customStates.get(target);if(state&&!state.failed&&!state.constructing&&state.definition.attributes.includes(name))customReaction(target,'attributeChangedCallback',[name,oldValue,target.getAttribute(name),null]);}
else if(type==='childList'){for(const node of removed||[])customWalk(node,child=>{const state=customStates.get(child);if(state&&state.connected){state.connected=false;customReaction(child,'disconnectedCallback');}});
for(const node of added||[])customWalk(node,child=>{upgradeCustomElement(child);customConnection(child);});}
}
class HTMLElement extends Element {
constructor(){const current=customConstruction[customConstruction.length-1];if(current&¤t.definition.constructor===new.target)return current.node;
const definition=customConstructors.get(new.target);if(!definition)throw new TypeError('Illegal constructor');let node;creatingCustom=true;try{node=document.createElement(definition.name);}finally{creatingCustom=false;}
Object.setPrototypeOf(node,new.target.prototype);customStates.set(node,{definition,connected:false,constructing:false,failed:false});return node;}
}
const registryToken={};
class CustomElementRegistry {
constructor(token){if(token!==registryToken)throw new TypeError('Illegal constructor');}
get(name){return customDefinitions.get(String(name))?.constructor;}
getName(constructor){return customConstructors.get(constructor)?.name??null;}
define(name,constructor,options={}){name=String(name);if(!validCustomName(name))throw new DOMException('Invalid custom element name','SyntaxError');
if(options?.extends!==undefined)throw new DOMException('Customized built-in elements are unavailable','NotSupportedError');
if(typeof constructor!=='function'||!constructor.prototype||!HTMLElement.prototype.isPrototypeOf(constructor.prototype))throw new TypeError('Expected an HTMLElement subclass');
if(constructor.formAssociated===true)throw new DOMException('Form-associated custom elements are unavailable','NotSupportedError');
if(customDefinitions.has(name)||customConstructors.has(constructor))throw new DOMException('Custom element is already defined','NotSupportedError');if(customDefinitions.size>=128)throw new RangeError('Custom element definition limit reached');
const attributes=typeof constructor.prototype.attributeChangedCallback==='function'?Array.from(constructor.observedAttributes||[],String):[];if(attributes.length>128||attributes.some(name=>name.length>128))throw new RangeError('Observed attribute limit reached');
const definition={name,constructor,attributes};customDefinitions.set(name,definition);customConstructors.set(constructor,definition);
for(const node of document.querySelectorAll(name))upgradeCustomElement(node);const waiter=customWaiters.get(name);if(waiter){customWaiters.delete(name);waiter.resolve(constructor);}}
whenDefined(name){name=String(name);if(!validCustomName(name))return Promise.reject(new DOMException('Invalid custom element name','SyntaxError'));const definition=customDefinitions.get(name);if(definition)return Promise.resolve(definition.constructor);
if(customWaiters.has(name))return customWaiters.get(name).promise;if(customWaiters.size>=128)return Promise.reject(new RangeError('Custom element waiter limit reached'));let resolve;const promise=new Promise(callback=>resolve=callback);customWaiters.set(name,{promise,resolve});return promise;}
upgrade(root){if(!root||typeof root.nodeType!=='number')throw new TypeError('Expected Node');customWalk(root,upgradeCustomElement);}
}
window.CustomElementRegistry=CustomElementRegistry;Object.defineProperty(window,'customElements',{value:new CustomElementRegistry(registryToken),enumerable:true});
""" + "\n";
}
namespace XFESystem.Browser;
/// <summary>Autonomous HTML elements with real DOM identity and bounded upgrade/lifecycle reactions.
/// Customized built-ins, shadow roots and element internals remain unsupported.</summary>
internal static class BrowserDomCustomElements
{
internal const string Source = "\n" + """
const customDefinitions=new Map(),customConstructors=new Map(),customStates=new WeakMap(),customConstruction=[],customWaiters=new Map();let creatingCustom=false;
const reservedCustomNames=new Set(['annotation-xml','color-profile','font-face','font-face-src','font-face-uri','font-face-format','font-face-name','missing-glyph']);
function validCustomName(name){return name.length<=128&&/^[a-z][a-z0-9._-]*-[a-z0-9._-]*$/.test(name)&&!reservedCustomNames.has(name);}
function customReaction(node,name,args=[]){const callback=node[name];if(typeof callback!=='function')return;queueMicrotask(function(){try{const result=callback.apply(node,args);if(result&&typeof result.catch==='function')result.catch(error=>eventError(String(error)));}catch(error){eventError(String(error));}});}
function customConnection(node){const state=customStates.get(node);if(!state||state.failed||state.constructing)return;const connected=!!node.isConnected;if(connected===state.connected)return;state.connected=connected;customReaction(node,connected?'connectedCallback':'disconnectedCallback');}
function upgradeCustomElement(node){if(creatingCustom||!node||node.nodeType!==1||node.namespaceURI!=='http://www.w3.org/1999/xhtml'||customStates.has(node))return;
const definition=customDefinitions.get(node.localName);if(!definition)return;const state={definition,connected:false,constructing:true,failed:false};customStates.set(node,state);Object.setPrototypeOf(node,definition.constructor.prototype);customConstruction.push({node,definition});
try{if(Reflect.construct(definition.constructor,[])!==node)throw new TypeError('A custom element constructor returned another object');}
catch(error){state.failed=true;eventError(String(error));}finally{customConstruction.pop();state.constructing=false;}
if(state.failed)return;for(const name of definition.attributes)if(node.hasAttribute(name))customReaction(node,'attributeChangedCallback',[name,null,node.getAttribute(name),null]);customConnection(node);
}
function customWalk(root,callback){if(!root)return;if(root.nodeType===1)callback(root);for(const child of Array.from(root.children||[]))customWalk(child,callback);}
function customMutation(target,type,name,oldValue,added,removed){if(!customDefinitions.size)return;
if(type==='attributes'){const state=customStates.get(target);if(state&&!state.failed&&!state.constructing&&state.definition.attributes.includes(name))customReaction(target,'attributeChangedCallback',[name,oldValue,target.getAttribute(name),null]);}
else if(type==='childList'){for(const node of removed||[])customWalk(node,child=>{const state=customStates.get(child);if(state&&state.connected){state.connected=false;customReaction(child,'disconnectedCallback');}});
for(const node of added||[])customWalk(node,child=>{upgradeCustomElement(child);customConnection(child);});}
}
class HTMLElement extends Element {
constructor(){const current=customConstruction[customConstruction.length-1];if(current&¤t.definition.constructor===new.target)return current.node;
const definition=customConstructors.get(new.target);if(!definition)throw new TypeError('Illegal constructor');let node;creatingCustom=true;try{node=document.createElement(definition.name);}finally{creatingCustom=false;}
Object.setPrototypeOf(node,new.target.prototype);customStates.set(node,{definition,connected:false,constructing:false,failed:false});return node;}
}
const registryToken={};
class CustomElementRegistry {
constructor(token){if(token!==registryToken)throw new TypeError('Illegal constructor');}
get(name){return customDefinitions.get(String(name))?.constructor;}
getName(constructor){return customConstructors.get(constructor)?.name??null;}
define(name,constructor,options={}){name=String(name);if(!validCustomName(name))throw new DOMException('Invalid custom element name','SyntaxError');
if(options?.extends!==undefined)throw new DOMException('Customized built-in elements are unavailable','NotSupportedError');
if(typeof constructor!=='function'||!constructor.prototype||!HTMLElement.prototype.isPrototypeOf(constructor.prototype))throw new TypeError('Expected an HTMLElement subclass');
if(constructor.formAssociated===true)throw new DOMException('Form-associated custom elements are unavailable','NotSupportedError');
if(customDefinitions.has(name)||customConstructors.has(constructor))throw new DOMException('Custom element is already defined','NotSupportedError');if(customDefinitions.size>=128)throw new RangeError('Custom element definition limit reached');
const attributes=typeof constructor.prototype.attributeChangedCallback==='function'?Array.from(constructor.observedAttributes||[],String):[];if(attributes.length>128||attributes.some(name=>name.length>128))throw new RangeError('Observed attribute limit reached');
const definition={name,constructor,attributes};customDefinitions.set(name,definition);customConstructors.set(constructor,definition);
for(const node of document.querySelectorAll(name))upgradeCustomElement(node);const waiter=customWaiters.get(name);if(waiter){customWaiters.delete(name);waiter.resolve(constructor);}}
whenDefined(name){name=String(name);if(!validCustomName(name))return Promise.reject(new DOMException('Invalid custom element name','SyntaxError'));const definition=customDefinitions.get(name);if(definition)return Promise.resolve(definition.constructor);
if(customWaiters.has(name))return customWaiters.get(name).promise;if(customWaiters.size>=128)return Promise.reject(new RangeError('Custom element waiter limit reached'));let resolve;const promise=new Promise(callback=>resolve=callback);customWaiters.set(name,{promise,resolve});return promise;}
upgrade(root){if(!root||typeof root.nodeType!=='number')throw new TypeError('Expected Node');customWalk(root,upgradeCustomElement);}
}
window.CustomElementRegistry=CustomElementRegistry;Object.defineProperty(window,'customElements',{value:new CustomElementRegistry(registryToken),enumerable:true});
""" + "\n";
}