UNPKG

hyperscript.org

Version:

a small scripting language for the web

1 lines 79.1 kB
function e(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}function t(e,t){var n=e[t];if(n)return n;var r={};return e[t]=r,r}function n(e,t){return new(e.bind.apply(e,[e].concat(t)))}let r;var a=globalThis;class o{constructor(e,t,n){this._css=e,this.relativeToElement=t,this.escape=n}get css(){return this.escape?l.escapeSelector(this._css):this._css}get className(){return this._css.substr(1)}get id(){return this.className()}contains(e){for(let t of this)if(t.contains(e))return!0;return!1}get length(){return this.selectMatches().length}[Symbol.iterator](){return this.selectMatches()[Symbol.iterator]()}selectMatches(){return l.getRootNode(this.relativeToElement).querySelectorAll(this.css)}}var i=function(){var e={"+":"PLUS","-":"MINUS","*":"MULTIPLY","/":"DIVIDE",".":"PERIOD","..":"ELLIPSIS","\\":"BACKSLASH",":":"COLON","%":"PERCENT","|":"PIPE","!":"EXCLAMATION","?":"QUESTION","#":"POUND","&":"AMPERSAND",$:"DOLLAR",";":"SEMI",",":"COMMA","(":"L_PAREN",")":"R_PAREN","<":"L_ANG",">":"R_ANG","<=":"LTE_ANG",">=":"GTE_ANG","==":"EQ","===":"EQQ","!=":"NEQ","!==":"NEQQ","{":"L_BRACE","}":"R_BRACE","[":"L_BRACKET","]":"R_BRACKET","=":"EQUALS"};function t(e){return i(e)||o(e)||"-"===e||"_"===e||":"===e}function n(e){return i(e)||o(e)||"-"===e||"_"===e||":"===e}function r(e){return" "===e||"\t"===e||a(e)}function a(e){return"\r"===e||"\n"===e}function o(e){return e>="0"&&e<="9"}function i(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"}function s(e,t){return"_"===e||"$"===e}function l(e,t,n){a();var r=null;function a(){for(;"WHITESPACE"===m(0,!0).type;)t.push(e.shift())}function o(e,t){u.raiseParseError(e,t)}function i(e){if(p()&&p().op&&p().value===e)return c()}function s(e,t,n,r){if(p()&&p().type&&[e,t,n,r].indexOf(p().type)>=0)return c()}function l(e,t){if(-1===h.indexOf(e))return t=t||"IDENTIFIER",p()&&p().value===e&&p().type===t?c():void 0}function c(){var n=e.shift();return t.push(n),r=n,a(),n}function f(n,r){for(var o=[],i=m(0,!0);!(null!=r&&i.type===r||null!=n&&i.value===n||"EOF"===i.type);){var u=e.shift();t.push(u),o.push(i),i=m(0,!0)}return a(),o}function m(t,n){var r,a=0;do{if(!n)for(;e[a]&&"WHITESPACE"===e[a].type;)a++;r=e[a],t--,a++}while(t>-1);return r||{type:"EOF",value:"<<<EOF>>>"}}function p(){return m(0)}var h=[];return{pushFollow:function(e){h.push(e)},popFollow:function(){h.pop()},clearFollow:function(){var e=h;return h=[],e},restoreFollow:function(e){h=e},matchAnyToken:function(e,t,n){for(var r=0;r<arguments.length;r++){var a=arguments[r],o=l(a);if(o)return o}},matchAnyOpToken:function(e,t,n){for(var r=0;r<arguments.length;r++){var a=arguments[r],o=i(a);if(o)return o}},matchOpToken:i,requireOpToken:function(e){var t=i(e);if(t)return t;o(this,"Expected '"+e+"' but found '"+p().value+"'")},matchTokenType:s,requireTokenType:function(e,t,n,r){var a=s(e,t,n,r);if(a)return a;o(this,"Expected one of "+JSON.stringify([e,t,n]))},consumeToken:c,matchToken:l,requireToken:function(e,t){var n=l(e,t);if(n)return n;o(this,"Expected '"+e+"' but found '"+p().value+"'")},list:e,consumed:t,source:n,hasMore:function(){return e.length>0},currentToken:p,lastMatch:function(){return r},token:m,consumeUntil:f,consumeUntilWhitespace:function(){return f(null,"WHITESPACE")},lastWhitespace:function(){return t[t.length-1]&&"WHITESPACE"===t[t.length-1].type?t[t.length-1].value:""},sourceFor:function(){return n.substring(this.startToken.start,this.endToken.end)},lineFor:function(){return n.split("\n")[this.startToken.line-1]}}}function c(e){if(e.length>0){var t=e[e.length-1];if("IDENTIFIER"===t.type||"CLASS_REF"===t.type||"ID_REF"===t.type)return!1;if(t.op&&(">"===t.value||")"===t.value))return!1}return!0}return{tokenize:function(u,f){var m,p=[],h=u,d=0,v=0,E=1,T="<START>",k=0;function y(){return f&&0===k}for(;d<h.length;)if("-"!==R()||"-"!==S()||!r(A())&&""!==A())if(r(R()))p.push(M());else if(P()||"."!==R()||!i(S())&&"{"!==S())if(P()||"#"!==R()||!i(S())&&"{"!==S())if("["===R()&&"@"===S())p.push(q());else if("@"===R())p.push(N());else if(i(R())||!y()&&s(R()))p.push(w());else if(o(R()))p.push(O());else if(y()||'"'!==R()&&"`"!==R())if(y()||"'"!==R()){if(e[R()])"$"===T&&"{"===R()&&k++,"}"===R()&&k--,p.push(L());else if(y()||"`"===(m=R())||"^"===m)p.push(g("RESERVED",F()));else if(d<h.length)throw Error("Unknown token: "+R()+" ")}else c(p)?p.push(C()):p.push(L());else p.push(C());else p.push(I());else p.push(b());else x();return l(p,[],h);function g(e,t){return{type:e,value:t,start:d,end:d+1,column:v,line:E}}function x(){for(;R()&&!a(R());)F();F()}function b(){var e=g("CLASS_REF"),n=F();if("{"===R()){for(e.template=!0,n+=F();R()&&"}"!==R();)n+=F();if("}"!==R())throw Error("Unterminated class reference");n+=F()}else for(;t(R());)n+=F();return e.value=n,e.end=d,e}function q(){for(var e=g("ATTRIBUTE_REF"),t=F();d<h.length&&"]"!==R();)t+=F();return"]"===R()&&(t+=F()),e.value=t,e.end=d,e}function N(){for(var e=g("ATTRIBUTE_REF"),t=F();n(R());)t+=F();return e.value=t,e.end=d,e}function I(){var e=g("ID_REF"),t=F();if("{"===R()){for(e.template=!0,t+=F();R()&&"}"!==R();)t+=F();if("}"!==R())throw Error("Unterminated id reference");F()}else for(;n(R());)t+=F();return e.value=t,e.end=d,e}function w(){for(var e=g("IDENTIFIER"),t=F();i(R())||s(R());)t+=F();return e.value=t,e.end=d,e}function O(){for(var e=g("NUMBER"),t=F();o(R());)t+=F();for("."===R()&&o(S())&&(t+=F());o(R());)t+=F();return e.value=t,e.end=d,e}function L(){for(var t=(r=void 0,(r=g(void 0,void 0)).op=!0,r),n=F();R()&&e[n+R()];)n+=F();var r;return t.type=e[n],t.value=n,t.end=d,t}function C(){for(var e,t=g("STRING"),n=F(),r="";R()&&R()!==n;)"\\"===R()&&F(),r+=F();if(R()!==n)throw Error("Unterminated string at [Line: "+(e=t).line+", Column: "+e.column+"]");return F(),t.value=r,t.end=d,t.template="`"===n,t}function R(){return h.charAt(d)}function S(){return h.charAt(d+1)}function A(){return h.charAt(d+2)}function F(){return T=R(),d++,v++,T}function P(){return i(T)||o(T)||")"===T||'"'===T||"'"===T||"`"===T||"}"===T||"]"===T}function M(){for(var e=g("WHITESPACE"),t="";R()&&r(R());)a(R())&&(v=0,E++),t+=F();return e.value=t,e.end=d,e}},makeTokensObject:l}}(),u=function(){var e={},t={},n={},r=[],a=[];function o(e,t,n){e.startToken=t,e.sourceFor=n.sourceFor,e.lineFor=n.lineFor,e.programSource=n.source}function i(t,n,r){var a=e[t];if(a){var i=n.currentToken(),s=a(u,l,n,r);if(s)for(o(s,i,n),s.endToken=s.endToken||n.lastMatch(),r=s.root;null!=r;)o(r,i,n),r=r.root;return s}}function s(e,t,n,r){var a=i(e,t,r);return a||m(t,n||"Expected "+e),a}function c(e,t){for(var n=0;n<e.length;n++){var r=i(e[n],t);if(r)return r}}function f(t,n){e[t]=n}function m(e,t){t=(t||"Unexpected Token : "+e.currentToken().value)+"\n\n"+function(e){var t=e.currentToken(),n=e.source.split("\n"),r=n[t&&t.line?t.line-1:n.length-1];return r+"\n"+" ".repeat(t&&t.line?t.column:r.length-1)+"^^\n\n"}(e);var n=new Error(t);throw n.tokens=e,n}function p(e){return t[e.value]}function h(e){return n[e.value]}return f("feature",function(e,t,r){if(r.matchOpToken("(")){var a=e.requireElement("feature",r);return r.requireOpToken(")"),a}var o=n[r.currentToken().value];if(o)return o(e,t,r)}),f("command",function(e,n,r){if(r.matchOpToken("(")){const t=e.requireElement("command",r);return r.requireOpToken(")"),t}var a=t[r.currentToken().value];let o;return a?o=a(e,n,r):"IDENTIFIER"===r.currentToken().type&&"("===r.token(1).value&&(o=e.requireElement("pseudoCommand",r)),o?e.parseElement("indirectStatement",r,o):o}),f("commandList",function(e,t,n){var r=e.parseElement("command",n);if(r){n.matchToken("then");const t=e.parseElement("commandList",n);return t&&(r.next=t),r}}),f("leaf",function(e,t,n){var a=c(r,n);return null==a?i("symbol",n):a}),f("indirectExpression",function(e,t,n,r){for(var o=0;o<a.length;o++){var i=a[o];r.endToken=n.lastMatch();var u=e.parseElement(i,n,r);if(u)return u}return r}),f("indirectStatement",function(e,t,n,r){if(n.matchToken("unless")){r.endToken=n.lastMatch();var a={type:"unlessStatementModifier",args:[e.requireElement("expression",n)],op:function(e,t){return t?this.next:r},execute:function(e){return t.unifiedExec(this,e)}};return r.parent=a,a}return r}),f("primaryExpression",function(e,t,n){var r=e.parseElement("leaf",n);if(r)return e.parseElement("indirectExpression",n,r);e.raiseParseError(n,"Unexpected value: "+n.currentToken().value)}),{setParent:function e(t,n){"object"==typeof t&&(t.parent=n,"object"==typeof n&&(n.children=n.children||new Set,n.children.add(t)),e(t.next,n))},requireElement:s,parseElement:i,featureStart:h,commandStart:p,commandBoundary:function(e){return!("end"!=e.value&&"then"!=e.value&&"else"!=e.value&&"otherwise"!=e.value&&")"!=e.value&&!p(e)&&!h(e)&&"EOF"!=e.type)},parseAnyOf:c,parseHyperScript:function(e){var t=i("hyperscript",e);if(e.hasMore()&&m(e),t)return t},raiseParseError:m,addGrammarElement:f,addCommand:function(n,r){var a=n+"Command",o=function(e,t,n){const o=r(e,t,n);if(o)return o.type=a,o.execute=function(e){return e.meta.command=o,t.unifiedExec(this,e)},o};e[a]=o,t[n]=o},addFeature:function(t,r){var a=t+"Feature",o=function(e,n,o){var i=r(e,n,o);if(i)return i.isFeature=!0,i.keyword=t,i.type=a,i};e[a]=o,n[t]=o},addLeafExpression:function(e,t){r.push(e),f(e,t)},addIndirectExpression:function(e,t){a.push(e),f(e,t)},parseStringTemplate:function(e){var t=[""];do{if(t.push(e.lastWhitespace()),"$"===e.currentToken().value){e.consumeToken();var n=e.matchOpToken("{");t.push(s("expression",e)),n&&e.requireOpToken("}"),t.push("")}else if("\\"===e.currentToken().value)e.consumeToken(),e.consumeToken();else{var r=e.consumeToken();t[t.length-1]+=r?r.value:""}}while(e.hasMore());return t.push(e.lastWhitespace()),t},ensureTerminated:function(e){for(var t={type:"implicitReturn",op:function(e){return e.meta.returned=!0,e.meta.resolve&&e.meta.resolve(),l.HALT},execute:function(e){}},n=e;n.next;)n=n.next;n.next=t}}}(),s={dynamicResolvers:[function(e,t){if("Fixed"===e)return Number(t).toFixed();if(0===e.indexOf("Fixed:")){let n=e.split(":")[1];return Number(t).toFixed(parseInt(n))}}],String:function(e){return e.toString?e.toString():""+e},Int:function(e){return parseInt(e)},Float:function(e){return parseFloat(e)},Number:function(e){return Number(e)},Date:function(e){return new Date(e)},Array:function(e){return Array.from(e)},JSON:function(e){return JSON.stringify(e)},Object:function(t){return t instanceof String&&(t=t.toString()),"string"==typeof t?JSON.parse(t):e({},t)}},l=function(){function n(e,t){var n=e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector;return n&&n.call(e,t)}function c(e,t,n){(n=n||{}).sentBy=e;var r=function(e,t){var n;return a.Event&&"function"==typeof a.Event?(n=new Event(e,{bubbles:!0,cancelable:!0})).detail=t:(n=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,t),n}(t,n);return e.dispatchEvent(r)}function f(e){return Array.isArray(e)||"undefined"!=typeof NodeList&&(e instanceof NodeList||e instanceof HTMLCollection)}function m(e){return e instanceof o||f(e)}function p(e,t){if(null==e);else if(function(e){return"object"==typeof e&&Symbol.iterator in e&&"function"==typeof e[Symbol.iterator]}(e))for(const n of e)t(n);else if(f(e))for(var n=0;n<e.length;n++)t(e[n]);else t(e)}var h={array_sentinel:!0};function d(e){for(var t=0;t<e.length;t++){var n=e[t];if(n.asyncWrapper&&(e[t]=n.value),Array.isArray(n))for(var r=0;r<n.length;r++){var a=n[r];a.asyncWrapper&&(n[r]=a.value)}}}var v={};function E(e,t){var n=[t],r=!1,a=!1;if(e.args)for(var o=0;o<e.args.length;o++){var i=e.args[o];if(null==i)n.push(null);else if(Array.isArray(i)){for(var u=[],s=0;s<i.length;s++){var l=i[s];(c=l?l.evaluate(t):null)&&(c.then?r=!0:c.asyncWrapper&&(a=!0)),u.push(c)}n.push(u)}else if(i.evaluate){var c;(c=i.evaluate(t))&&(c.then?r=!0:c.asyncWrapper&&(a=!0)),n.push(c)}else n.push(i)}return r?new Promise(function(t,r){var o=function(e){for(var t=[],n=0;n<e.length;n++){var r=e[n];if(Array.isArray(r)){t.push(h);for(var a=0;a<r.length;a++)t.push(r[a]);t.push(h)}else t.push(r)}return t}(n);Promise.all(o).then(function(n){n=function(e){for(var t=[],n=0;n<e.length;n++){var r=e[n];if(r===h){r=e[++n];var a=[];for(t.push(a);r!==h;)a.push(r),r=e[++n]}else t.push(r)}return t}(n),a&&d(n);try{var o=e.op.apply(e,n);t(o)}catch(e){r(e)}}).catch(function(e){r(e)})}):(a&&d(n),e.op.apply(e,n))}let T=null;function k(){return null==T&&(T=r.config.attributes.replace(/ /g,"").split(",")),T}function y(e){for(var t=0;t<k().length;t++){var n=k()[t];if(e.hasAttribute&&e.hasAttribute(n))return e.getAttribute(n)}return e instanceof HTMLScriptElement&&"text/hyperscript"===e.type?e.innerText:null}var g=new WeakMap;function x(e){var t=g.get(e);return void 0===t&&g.set(e,t={}),t}function b(t,n){t&&(e(n,x(t)),b(t.parentElement,n))}function q(e,t,n,r){var o={meta:{parser:u,lexer:i,runtime:l,owner:e,feature:t,iterators:{}},me:n,event:r,target:r?r.target:null,detail:r?r.detail:null,body:"document"in a?document.body:null};return o.meta.ctx=o,b(e,o),o}function N(e){var t=i.tokenize(e);if(u.commandStart(t.currentToken())){for(var n=u.requireElement("commandList",t),r=n;r.next;)r=r.next;return r.next={op:function(){return v}},n}return u.featureStart(t.currentToken())?u.requireElement("hyperscript",t):u.requireElement("expression",t)}function I(e,t){if(!e.closest||!e.closest(r.config.disableSelector)){var n=O(e);if(!n.initialized){var a=y(e);if(a)try{n.initialized=!0,n.script=a;var o=i.tokenize(a),s=u.parseHyperScript(o);if(!s)return;s.apply(t||e,e),setTimeout(function(){c(t||e,"load",{hyperscript:!0})},1)}catch(t){l.triggerEvent(e,"exception",{error:t}),console.error("hyperscript errors were found on the following element:",e,"\n\n",t.message,t.stack)}}}}var w=new WeakMap;function O(e){var t=w.get(e);return void 0===t&&w.set(e,t={}),t}function L(e){var n=e.meta&&e.meta.owner;if(n){var r=O(n),a="elementScope";return e.meta.feature&&e.meta.feature.behavior&&(a=e.meta.feature.behavior+"Scope"),t(r,a)}return{}}return{typeCheck:function(e,t,n){return!(null!=e||!n)||Object.prototype.toString.call(e).slice(8,-1)===t},forEach:p,implicitLoop:function(e,t){if(m(e))for(const n of e)t(n);else t(e)},triggerEvent:c,matchesSelector:n,getScript:y,processNode:function(e){var t=l.getScriptSelector();n(e,t)&&I(e,e),e instanceof HTMLScriptElement&&"text/hyperscript"===e.type&&I(e,document.body),e.querySelectorAll&&p(e.querySelectorAll(t+", [type='text/hyperscript']"),function(e){I(e,e instanceof HTMLScriptElement&&"text/hyperscript"===e.type?document.body:e)})},evaluate:function(t,n,r){class o extends EventTarget{constructor(e){super(),this.module=e}toString(){return this.module.id}}var i="document"in a?a.document.body:new o(r&&r.module);n=e(q(i,null,i,null),n||{});var u=N(t);return u.execute?(u.execute(n),n.result):u.apply?(u.apply(i,i,r),x(i)):u.evaluate(n)},evaluateNoPromise:function(e,t){let n=e.evaluate(t);if(n.next)throw new Error(e.sourceFor()+" returned a Promise in a context that they are not allowed.");return n},parse:N,getScriptSelector:function(){return k().map(function(e){return"["+e+"]"}).join(", ")},resolveSymbol:function(e,t,n){if("me"===e||"my"===e||"I"===e)return t.me;if("it"===e||"its"===e)return t.result;if("you"===e||"your"===e||"yourself"===e)return t.beingTold;if("global"===n)return a[e];if("element"===n)return L(t)[e];if("local"===n)return t[e];if(t.meta&&t.meta.context){var r=t.meta.context[e];if(void 0!==r)return r}var o=t[e];return void 0!==o||void 0!==(o=L(t)[e])?o:a[e]},setSymbol:function(e,t,n,r){if("global"===n)a[e]=r;else if("element"===n)(o=L(t))[e]=r;else if("local"===n)t[e]=r;else{var o,i=t[e];void 0!==i?t[e]=r:void 0!==(i=(o=L(t))[e])?o[e]=r:t[e]=r}},makeContext:q,findNext:function e(t,n){if(t)return t.resolveNext?t.resolveNext(n):t.next?t.next:e(t.parent,n)},unifiedEval:E,convertValue:function(e,t){for(var n=s.dynamicResolvers,r=0;r<n.length;r++){var a=(0,n[r])(t,e);if(void 0!==a)return a}if(null==e)return null;var o=s[t];if(o)return o(e);throw"Unknown conversion : "+t},unifiedExec:function e(t,n){for(;;){try{var r=E(t,n)}catch(e){if(n.meta.handlingFinally)console.error(" Exception in finally block: ",e),r=v;else{if(l.registerHyperTrace(n,e),n.meta.errorHandler&&!n.meta.handlingError){n.meta.handlingError=!0,n[n.meta.errorSymbol]=e,t=n.meta.errorHandler;continue}n.meta.currentException=e,r=v}}if(null==r)return void console.error(t," did not return a next element to execute! context: ",n);if(r.then)return void r.then(function(t){e(t,n)}).catch(function(t){e({op:function(){throw t}},n)});if(r===v){if(!n.meta.finallyHandler||n.meta.handlingFinally){if(n.meta.onHalt&&n.meta.onHalt(),n.meta.currentException){if(n.meta.reject)return void n.meta.reject(n.meta.currentException);throw n.meta.currentException}return}n.meta.handlingFinally=!0,t=n.meta.finallyHandler}else t=r}},resolveProperty:function(e,t,n){if(null!=e){var r=n&&e.getAttribute?e.getAttribute(t):e[t];if(void 0!==r)return r;if(m(e)){var a=[];for(var o of e){var i=n?o.getAttribute(t):o[t];i&&a.push(i)}return a}}},assignToNamespace:function(e,t,n,r){let o;for(o="undefined"!=typeof document&&e===document.body?a:x(e);t.length>0;){var i=t.shift(),u=o[i];null==u&&(o[i]=u={}),o=u}o[n]=r},registerHyperTrace:function(e,t){for(var n=[],r=null;null!=e;)n.push(e),r=e,e=e.meta.caller;null==r.meta.traceMap&&(r.meta.traceMap=new Map),r.meta.traceMap.get(t)||r.meta.traceMap.set(t,{trace:n,print:function(e){(e=e||console.error)("hypertrace /// ");for(var t=0,r=0;r<n.length;r++)t=Math.max(t,n[r].meta.feature.displayName.length);for(r=0;r<n.length;r++){var a=n[r];e(" ->",a.meta.feature.displayName.padEnd(t+2),"-",a.meta.owner)}}})},getHyperTrace:function(e,t){for(var n=e;n.meta.caller;)n=n.meta.caller;if(n.meta.traceMap)return n.meta.traceMap.get(t,[])},getInternalData:O,getHyperscriptFeatures:x,escapeSelector:function(e){return e.replace(/:/g,function(e){return"\\"+e})},nullCheck:function(e,t){if(null==e)throw new Error("'"+t.sourceFor()+"' is null")},isEmpty:function(e){return null==e||0===e.length},doesExist:function(e){if(null==e)return!1;if(m(e))for(const t of e)return!0;return!1},getRootNode:function(e){if(e&&e instanceof Node){var t=e.getRootNode();if(t instanceof Document||t instanceof ShadowRoot)return t}return document},getEventQueueFor:function(e,t){let n=O(e);var r=n.eventQueues;null==r&&(r=new Map,n.eventQueues=r);var a=r.get(t);return null==a&&r.set(t,a={queue:[],executing:!1}),a},hyperscriptUrl:"document"in a?import.meta.url:null,HALT:v}}();{u.addLeafExpression("parenthesized",function(e,t,n){if(n.matchOpToken("(")){var r=n.clearFollow();try{var a=e.requireElement("expression",n)}finally{n.restoreFollow(r)}return n.requireOpToken(")"),a}}),u.addLeafExpression("string",function(e,t,n){var r=n.matchTokenType("STRING");if(r){var a,o=r.value;if(r.template){var u=i.tokenize(o,!0);a=e.parseStringTemplate(u)}else a=[];return{type:"string",token:r,args:a,op:function(e){for(var t="",n=1;n<arguments.length;n++){var r=arguments[n];void 0!==r&&(t+=r)}return t},evaluate:function(e){return 0===a.length?o:t.unifiedEval(this,e)}}}}),u.addGrammarElement("nakedString",function(e,t,n){if(n.hasMore()){var r=n.consumeUntilWhitespace();return n.matchTokenType("WHITESPACE"),{type:"nakedString",tokens:r,evaluate:function(e){return r.map(function(e){return e.value}).join("")}}}}),u.addLeafExpression("number",function(e,t,n){var r=n.matchTokenType("NUMBER");if(r){var a=r,o=parseFloat(r.value);return{type:"number",value:o,numberToken:a,evaluate:function(){return o}}}}),u.addLeafExpression("idRef",function(e,t,n){var r=n.matchTokenType("ID_REF");if(r){if(r.template){var a=r.value.substr(2,r.value.length-2),o=i.tokenize(a);return{type:"idRefTemplate",args:[e.requireElement("expression",o)],op:function(e,n){return t.getRootNode(e.me).getElementById(n)},evaluate:function(e){return t.unifiedEval(this,e)}}}{const e=r.value.substr(1);return{type:"idRef",css:r.value,value:e,evaluate:function(n){return t.getRootNode(n.me).getElementById(e)}}}}}),u.addLeafExpression("classRef",function(e,t,n){var r=n.matchTokenType("CLASS_REF");if(r){if(r.template){var a=r.value.substr(2,r.value.length-2),u=i.tokenize(a);return{type:"classRefTemplate",args:[e.requireElement("expression",u)],op:function(e,t){return new o("."+t,e.me,!0)},evaluate:function(e){return t.unifiedEval(this,e)}}}{const e=r.value;return{type:"classRef",css:e,evaluate:function(t){return new o(e,t.me,!0)}}}}});class r extends o{constructor(e,t,n){super(e,t),this.templateParts=n,this.elements=n.filter(e=>e instanceof Element)}get css(){let e="",t=0;for(const n of this.templateParts)n instanceof Element?e+="[data-hs-query-id='"+t+++"']":e+=n;return e}[Symbol.iterator](){this.elements.forEach((e,t)=>e.dataset.hsQueryId=t);const e=super[Symbol.iterator]();return this.elements.forEach(e=>e.removeAttribute("data-hs-query-id")),e}}u.addLeafExpression("queryRef",function(e,t,n){if(n.matchOpToken("<")){var a=n.consumeUntil("/");n.requireOpToken("/"),n.requireOpToken(">");var u=a.map(function(e){return"STRING"===e.type?'"'+e.value+'"':e.value}).join("");if(u.indexOf("$")>=0)var s=!0,l=i.tokenize(u,!0),c=e.parseStringTemplate(l);return{type:"queryRef",css:u,args:c,op:function(e,...t){return s?new r(u,e.me,t):new o(u,e.me)},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addLeafExpression("attributeRef",function(e,t,n){var r=n.matchTokenType("ATTRIBUTE_REF");if(r){var a=r.value;if(0===a.indexOf("["))var o=a.substring(2,a.length-1);else o=a.substring(1);var i="["+o+"]",u=o.split("="),s=u[0],l=u[1];return l&&0===l.indexOf('"')&&(l=l.substring(1,l.length-1)),{type:"attributeRef",name:s,css:i,value:l,op:function(e){var t=e.beingTold||e.me;if(t)return t.getAttribute(s)},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("objectKey",function(e,t,n){var r;if(r=n.matchTokenType("STRING"))return{type:"objectKey",key:r.value,evaluate:function(){return r.value}};if(n.matchOpToken("[")){var a=e.parseElement("expression",n);return n.requireOpToken("]"),{type:"objectKey",expr:a,args:[a],op:function(e,t){return t},evaluate:function(e){return t.unifiedEval(this,e)}}}var o="";do{(r=n.matchTokenType("IDENTIFIER")||n.matchOpToken("-"))&&(o+=r.value)}while(r);return{type:"objectKey",key:o,evaluate:function(){return o}}}),u.addLeafExpression("objectLiteral",function(e,t,n){if(n.matchOpToken("{")){var r=[],a=[];if(!n.matchOpToken("}")){do{var o=e.requireElement("objectKey",n);n.requireOpToken(":");var i=e.requireElement("expression",n);a.push(i),r.push(o)}while(n.matchOpToken(","));n.requireOpToken("}")}return{type:"objectLiteral",args:[r,a],op:function(e,t,n){for(var r={},a=0;a<t.length;a++)r[t[a]]=n[a];return r},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("nakedNamedArgumentList",function(e,t,n){var r=[],a=[];if("IDENTIFIER"===n.currentToken().type)do{var o=n.requireTokenType("IDENTIFIER");n.requireOpToken(":");var i=e.requireElement("expression",n);a.push(i),r.push({name:o,value:i})}while(n.matchOpToken(","));return{type:"namedArgumentList",fields:r,args:[a],op:function(e,t){for(var n={_namedArgList_:!0},a=0;a<t.length;a++)n[r[a].name.value]=t[a];return n},evaluate:function(e){return t.unifiedEval(this,e)}}}),u.addGrammarElement("namedArgumentList",function(e,t,n){if(n.matchOpToken("(")){var r=e.requireElement("nakedNamedArgumentList",n);return n.requireOpToken(")"),r}}),u.addGrammarElement("symbol",function(e,t,n){var r="default";n.matchToken("global")?r="global":n.matchToken("element")||n.matchToken("module")?(r="element",n.matchOpToken("'")&&n.requireToken("s")):n.matchToken("local")&&(r="local");let a=n.matchOpToken(":"),o=n.matchTokenType("IDENTIFIER");if(o){var i=o.value;return a&&(i=":"+i),"default"===r&&(0===i.indexOf("$")&&(r="global"),0===i.indexOf(":")&&(r="element")),{type:"symbol",token:o,scope:r,name:i,evaluate:function(e){return t.resolveSymbol(i,e,r)}}}}),u.addGrammarElement("implicitMeTarget",function(e,t,n){return{type:"implicitMeTarget",evaluate:function(e){return e.beingTold||e.me}}}),u.addLeafExpression("boolean",function(e,t,n){var r=n.matchToken("true")||n.matchToken("false");if(!r)return;const a="true"===r.value;return{type:"boolean",evaluate:function(e){return a}}}),u.addLeafExpression("null",function(e,t,n){if(n.matchToken("null"))return{type:"null",evaluate:function(e){return null}}}),u.addLeafExpression("arrayLiteral",function(e,t,n){if(n.matchOpToken("[")){var r=[];if(!n.matchOpToken("]")){do{var a=e.requireElement("expression",n);r.push(a)}while(n.matchOpToken(","));n.requireOpToken("]")}return{type:"arrayLiteral",values:r,args:[r],op:function(e,t){return t},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addLeafExpression("blockLiteral",function(e,t,n){if(n.matchOpToken("\\")){var r=[],a=n.matchTokenType("IDENTIFIER");if(a)for(r.push(a);n.matchOpToken(",");)r.push(n.requireTokenType("IDENTIFIER"));n.requireOpToken("-"),n.requireOpToken(">");var o=e.requireElement("expression",n);return{type:"blockLiteral",args:r,expr:o,evaluate:function(e){return function(){for(var t=0;t<r.length;t++)e[r[t].value]=arguments[t];return o.evaluate(e)}}}}}),u.addGrammarElement("timeExpression",function(e,t,n){var r=e.requireElement("expression",n),a=1;return n.matchToken("s")||n.matchToken("seconds")?a=1e3:n.matchToken("ms")||n.matchToken("milliseconds"),{type:"timeExpression",time:r,factor:a,args:[r],op:function(e,t){return t*a},evaluate:function(e){return t.unifiedEval(this,e)}}}),u.addIndirectExpression("propertyAccess",function(e,t,n,r){if(n.matchOpToken(".")){var a=n.requireTokenType("IDENTIFIER");return e.parseElement("indirectExpression",n,{type:"propertyAccess",root:r,prop:a,args:[r],op:function(e,n){return t.resolveProperty(n,a.value,!1)},evaluate:function(e){return t.unifiedEval(this,e)}})}}),u.addIndirectExpression("of",function(e,t,n,r){if(n.matchToken("of")){for(var a=e.requireElement("expression",n),o=null,i=r;i.root;)o=i,i=i.root;"symbol"!==i.type&&"attributeRef"!==i.type&&e.raiseParseError(n,"Cannot take a property of a non-symbol: "+i.type);var u="attributeRef"===i.type,s=i.name,l={type:"ofExpression",prop:i.token,root:a,attribute:u,expression:r,args:[a],op:function(e,n){return t.resolveProperty(n,s,u)},evaluate:function(e){return t.unifiedEval(this,e)}};return"attributeRef"===i.type&&(l.attribute=i),o?(o.root=l,o.args=[l]):r=l,e.parseElement("indirectExpression",n,r)}}),u.addIndirectExpression("possessive",function(e,t,n,r){if(!e.possessivesDisabled){var a=n.matchOpToken("'");if(a||"symbol"===r.type&&("my"===r.name||"its"===r.name||"your"===r.name)&&"IDENTIFIER"===n.currentToken().type){a&&n.requireToken("s");var o=e.parseElement("attributeRef",n);if(null==o)var i=n.requireTokenType("IDENTIFIER");return e.parseElement("indirectExpression",n,{type:"possessive",root:r,attribute:o,prop:i,args:[r],op:function(e,n){if(o)var r=t.resolveProperty(n,o.name,!0);else r=t.resolveProperty(n,i.value,!1);return r},evaluate:function(e){return t.unifiedEval(this,e)}})}}}),u.addIndirectExpression("inExpression",function(e,t,n,r){if(n.matchToken("in")){if("idRef"!==r.type&&"queryRef"===r.type||"classRef"===r.type)var a=!0;var o=e.requireElement("expression",n),i={type:"inExpression",root:r,args:[a?null:r,o],op:function(e,n,o){var i=[];return a?t.implicitLoop(o,function(e){for(var t=e.querySelectorAll(r.css),n=0;n<t.length;n++)i.push(t[n])}):t.implicitLoop(n,function(e){t.implicitLoop(o,function(t){e===t&&i.push(e)})}),i.length>0?i:null},evaluate:function(e){return t.unifiedEval(this,e)}};return e.parseElement("indirectExpression",n,i)}}),u.addIndirectExpression("asExpression",function(e,t,n,r){if(n.matchToken("as")){n.matchToken("a")||n.matchToken("an");var a=e.requireElement("dotOrColonPath",n).evaluate();return e.parseElement("indirectExpression",n,{type:"asExpression",root:r,args:[r],op:function(e,n){return t.convertValue(n,a)},evaluate:function(e){return t.unifiedEval(this,e)}})}}),u.addIndirectExpression("functionCall",function(e,t,n,r){if(n.matchOpToken("(")){var a=[];if(!n.matchOpToken(")")){do{a.push(e.requireElement("expression",n))}while(n.matchOpToken(","));n.requireOpToken(")")}if(r.root)var o={type:"functionCall",root:r,argExressions:a,args:[r.root,a],op:function(e,n,a){t.nullCheck(n,r.root);var o=n[r.prop.value];return t.nullCheck(o,r),o.hyperfunc&&a.push(e),o.apply(n,a)},evaluate:function(e){return t.unifiedEval(this,e)}};else o={type:"functionCall",root:r,argExressions:a,args:[r,a],op:function(e,n,a){return t.nullCheck(n,r),n.hyperfunc&&a.push(e),n.apply(null,a)},evaluate:function(e){return t.unifiedEval(this,e)}};return e.parseElement("indirectExpression",n,o)}}),u.addIndirectExpression("attributeRefAccess",function(e,t,n,r){var a=e.parseElement("attributeRef",n);if(a)return{type:"attributeRefAccess",root:r,attribute:a,args:[r],op:function(e,n){return t.resolveProperty(n,a.name,!0)},evaluate:function(e){return l.unifiedEval(this,e)}}}),u.addIndirectExpression("arrayIndex",function(e,t,n,r){if(n.matchOpToken("[")){var a=!1,o=!1,i=null,s=null;n.matchOpToken("..")?(a=!0,i=e.requireElement("expression",n)):(i=e.requireElement("expression",n),n.matchOpToken("..")&&(o=!0,"R_BRACKET"!==n.currentToken().type&&(s=e.parseElement("expression",n)))),n.requireOpToken("]");var c={type:"arrayIndex",root:r,firstIndex:i,secondIndex:s,args:[r,i,s],op:function(e,t,n,r){return a?t.slice(0,n+1):o?null!=r?t.slice(n,r+1):t.slice(n):t[n]},evaluate:function(e){return l.unifiedEval(this,e)}};return u.parseElement("indirectExpression",n,c)}}),u.addGrammarElement("postfixExpression",function(e,t,n){var r=e.parseElement("primaryExpression",n);if(n.matchOpToken(":")){var a=n.requireTokenType("IDENTIFIER"),o=!n.matchOpToken("!");return{type:"typeCheck",typeName:a,nullOk:o,args:[r],op:function(e,n){if(t.typeCheck(n,a.value,o))return n;throw new Error("Typecheck failed! Expected: "+a.value)},evaluate:function(e){return t.unifiedEval(this,e)}}}return r}),u.addGrammarElement("logicalNot",function(e,t,n){if(n.matchToken("not")){var r=e.requireElement("unaryExpression",n);return{type:"logicalNot",root:r,args:[r],op:function(e,t){return!t},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("noExpression",function(e,t,n){if(n.matchToken("no")){var r=e.requireElement("unaryExpression",n);return{type:"noExpression",root:r,args:[r],op:function(e,n){return t.isEmpty(n)},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addLeafExpression("some",function(e,t,n){if(n.matchToken("some")){var r=e.requireElement("expression",n);return{type:"noExpression",root:r,args:[r],op:function(e,n){return!t.isEmpty(n)},evaluate(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("negativeNumber",function(e,t,n){if(n.matchOpToken("-")){var r=e.requireElement("unaryExpression",n);return{type:"negativeNumber",root:r,args:[r],op:function(e,t){return-1*t},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("unaryExpression",function(e,t,n){return e.parseAnyOf(["logicalNot","relativePositionalExpression","positionalExpression","noExpression","negativeNumber","postfixExpression"],n)});var c=function(e,t,n,r){var a=[];l.forEach(t,function(t){(t.matches(n)||t===e)&&a.push(t)});for(var o=0;o<a.length-1;o++)if(a[o]===e)return a[o+1];if(r){var i=a[0];if(i&&i.matches(n))return i}};function f(e,t,n){if(t.contains)return t.contains(n);if(t.includes)return t.includes(n);throw Error("The value of "+e.sourceFor()+" does not have a contains or includes method on it")}function m(e,t,n){if(t.match)return!!t.match(n);if(t.matches)return t.matches(n);throw Error("The value of "+e.sourceFor()+" does not have a match or matches method on it")}u.addGrammarElement("relativePositionalExpression",function(e,t,n){var r=n.matchAnyToken("next","previous");if(r){if("next"===r.value)var a=!0;var o=e.parseElement("expression",n);if(n.matchToken("from")){n.pushFollow("in");try{var i=e.requireElement("expression",n)}finally{n.popFollow()}}else i=e.requireElement("implicitMeTarget",n);var u,s=!1;if(n.matchToken("in")){s=!0;var l=e.requireElement("expression",n)}else u=n.matchToken("within")?e.requireElement("expression",n):document.body;var f=!1;return n.matchToken("with")&&(n.requireToken("wrapping"),f=!0),{type:"relativePositionalExpression",from:i,forwardSearch:a,inSearch:s,wrapping:f,inElt:l,withinElt:u,operator:r.value,args:[o,i,l,u],op:function(e,t,n,r,o){var i,u,l=t.css;if(null==l)throw"Expected a CSS value";if(s){if(r)return a?c(n,r,l,f):(i=l,u=f,c(n,Array.from(r).reverse(),i,u))}else if(o)return a?function(e,t,n,r){for(var a=t.querySelectorAll(n),o=0;o<a.length;o++){var i=a[o];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_PRECEDING)return i}if(r)return a[0]}(n,o,l,f):function(e,t,n,r){for(var a=t.querySelectorAll(n),o=a.length-1;o>=0;o--){var i=a[o];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_FOLLOWING)return i}if(r)return a[a.length-1]}(n,o,l,f)},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("positionalExpression",function(e,t,n){var r=n.matchAnyToken("first","last","random");if(!r)return;n.matchAnyToken("in","from","of");var a=e.requireElement("unaryExpression",n);const o=r.value;return{type:"positionalExpression",rhs:a,operator:r.value,args:[a],op:function(e,t){if(t&&!Array.isArray(t)&&(t=t.children?t.children:Array.from(t)),t){if("first"===o)return t[0];if("last"===o)return t[t.length-1];if("random"===o)return t[Math.floor(Math.random()*t.length)]}},evaluate:function(e){return t.unifiedEval(this,e)}}}),u.addGrammarElement("mathOperator",function(e,t,n){var r,a=e.parseElement("unaryExpression",n),o=null;for(r=n.matchAnyOpToken("+","-","*","/","%");r;){var i=r.value;(o=o||r).value!==i&&e.raiseParseError(n,"You must parenthesize math operations with different operators");var u=e.parseElement("unaryExpression",n);a={type:"mathOperator",lhs:a,rhs:u,operator:i,args:[a,u],op:function(e,t,n){return"+"===i?t+n:"-"===i?t-n:"*"===i?t*n:"/"===i?t/n:"%"===i?t%n:void 0},evaluate:function(e){return t.unifiedEval(this,e)}},r=n.matchAnyOpToken("+","-","*","/","%")}return a}),u.addGrammarElement("mathExpression",function(e,t,n){return e.parseAnyOf(["mathOperator","unaryExpression"],n)}),u.addGrammarElement("comparisonOperator",function(e,t,n){var r=e.parseElement("mathExpression",n),a=n.matchAnyOpToken("<",">","<=",">=","==","===","!=","!=="),o=a?a.value:null,i=!0,u=!1;if(null==o&&(n.matchToken("is")||n.matchToken("am")?n.matchToken("not")?n.matchToken("in")?o="not in":n.matchToken("a")?(o="not a",u=!0):n.matchToken("empty")?(o="not empty",i=!1):o="!=":n.matchToken("in")?o="in":n.matchToken("a")?(o="a",u=!0):n.matchToken("empty")?(o="empty",i=!1):n.matchToken("less")?(n.requireToken("than"),n.matchToken("or")?(n.requireToken("equal"),n.requireToken("to"),o="<="):o="<"):n.matchToken("greater")?(n.requireToken("than"),n.matchToken("or")?(n.requireToken("equal"),n.requireToken("to"),o=">="):o=">"):o="==":n.matchToken("exist")||n.matchToken("exists")?(o="exist",i=!1):n.matchToken("matches")||n.matchToken("match")?o="match":n.matchToken("contains")||n.matchToken("contain")?o="contain":n.matchToken("includes")||n.matchToken("include")?o="include":(n.matchToken("do")||n.matchToken("does"))&&(n.requireToken("not"),n.matchToken("matches")||n.matchToken("match")?o="not match":n.matchToken("contains")||n.matchToken("contain")?o="not contain":n.matchToken("exist")||n.matchToken("exist")?(o="not exist",i=!1):n.matchToken("include")?o="not include":e.raiseParseError(n,"Expected matches or contains"))),o){if(u)var s=n.requireTokenType("IDENTIFIER"),l=!n.matchOpToken("!");else if(i){var c=e.requireElement("mathExpression",n);"match"!==o&&"not match"!==o||(c=c.css?c.css:c)}var p=r;r={type:"comparisonOperator",operator:o,typeName:s,nullOk:l,lhs:r,rhs:c,args:[r,c],op:function(e,n,r){if("=="===o)return n==r;if("!="===o)return n!=r;if("match"===o)return null!=n&&m(p,n,r);if("not match"===o)return null==n||!m(p,n,r);if("in"===o)return null!=r&&f(c,r,n);if("not in"===o)return null==r||!f(c,r,n);if("contain"===o)return null!=n&&f(p,n,r);if("not contain"===o)return null==n||!f(p,n,r);if("include"===o)return null!=n&&f(p,n,r);if("not include"===o)return null==n||!f(p,n,r);if("==="===o)return n===r;if("!=="===o)return n!==r;if("<"===o)return n<r;if(">"===o)return n>r;if("<="===o)return n<=r;if(">="===o)return n>=r;if("empty"===o)return t.isEmpty(n);if("not empty"===o)return!t.isEmpty(n);if("exist"===o)return t.doesExist(n);if("not exist"===o)return!t.doesExist(n);if("a"===o)return t.typeCheck(n,s.value,l);if("not a"===o)return!t.typeCheck(n,s.value,l);throw"Unknown comparison : "+o},evaluate:function(e){return t.unifiedEval(this,e)}}}return r}),u.addGrammarElement("comparisonExpression",function(e,t,n){return e.parseAnyOf(["comparisonOperator","mathExpression"],n)}),u.addGrammarElement("logicalOperator",function(e,t,n){var r,a=e.parseElement("comparisonExpression",n),o=null;for(r=n.matchToken("and")||n.matchToken("or");r;){(o=o||r).value!==r.value&&e.raiseParseError(n,"You must parenthesize logical operations with different operators");var i=e.requireElement("comparisonExpression",n);const u=r.value;a={type:"logicalOperator",operator:u,lhs:a,rhs:i,args:[a,i],op:function(e,t,n){return"and"===u?t&&n:t||n},evaluate:function(e){return t.unifiedEval(this,e)}},r=n.matchToken("and")||n.matchToken("or")}return a}),u.addGrammarElement("logicalExpression",function(e,t,n){return e.parseAnyOf(["logicalOperator","mathExpression"],n)}),u.addGrammarElement("asyncExpression",function(e,t,n){return n.matchToken("async")?{type:"asyncExpression",value:e.requireElement("logicalExpression",n),evaluate:function(e){return{asyncWrapper:!0,value:this.value.evaluate(e)}}}:e.parseElement("logicalExpression",n)}),u.addGrammarElement("expression",function(e,t,n){return n.matchToken("the"),e.parseElement("asyncExpression",n)}),u.addGrammarElement("assignableExpression",function(e,t,n){n.matchToken("the");var r=e.parseElement("primaryExpression",n);return!r||"symbol"!==r.type&&"ofExpression"!==r.type&&"propertyAccess"!==r.type&&"attributeRefAccess"!==r.type&&"attributeRef"!==r.type&&"possessive"!==r.type?(u.raiseParseError(n,"A target expression must be writable. The expression type '"+(r&&r.type)+"' is not."),r):r}),u.addGrammarElement("hyperscript",function(e,t,n){var r=[];if(n.hasMore())for(;e.featureStart(n.currentToken())||"("===n.currentToken().value;){var a=e.requireElement("feature",n);r.push(a),n.matchToken("end")}return{type:"hyperscript",features:r,apply:function(e,t,n){for(const a of r)a.install(e,t,n)}}});var p=function(e){var t=[];if("("===e.token(0).value&&(")"===e.token(1).value||","===e.token(2).value||")"===e.token(2).value)){e.matchOpToken("(");do{t.push(e.requireTokenType("IDENTIFIER"))}while(e.matchOpToken(","));e.requireOpToken(")")}return t};function h(e,t,n,r){var a=t.requireElement("eventName",r),o=t.parseElement("namedArgumentList",r);if("send"===e&&r.matchToken("to")||"trigger"===e&&r.matchToken("on"))var i=t.requireElement("expression",r);else i=t.requireElement("implicitMeTarget",r);var u={eventName:a,details:o,to:i,args:[i,a,o],op:function(e,t,r,a){return n.nullCheck(t,i),n.forEach(t,function(e){n.triggerEvent(e,r,a||{})}),n.findNext(u,e)}};return u}u.addFeature("on",function(t,n,r){if(r.matchToken("on")){var a=!1;r.matchToken("every")&&(a=!0);var o=[],i=null;do{var u=t.requireElement("eventName",r,"Expected event name").evaluate();i=i?i+" or "+u:"on "+u;var s=p(r),c=null;if(r.matchOpToken("[")&&(c=t.requireElement("expression",r),r.requireOpToken("]")),"NUMBER"===r.currentToken().type){var f=r.consumeToken(),m=parseInt(f.value);if(r.matchToken("to"))var h=r.consumeToken(),d=parseInt(h.value);else if(r.matchToken("and")){var v=!0;r.requireToken("on")}}if("intersection"===u){var E={};if(r.matchToken("with")&&(E.with=t.requireElement("expression",r).evaluate()),r.matchToken("having"))do{r.matchToken("margin")?E.rootMargin=t.requireElement("stringLike",r).evaluate():r.matchToken("threshold")?E.threshold=t.requireElement("expression",r).evaluate():t.raiseParseError(r,"Unknown intersection config specification")}while(r.matchToken("and"))}else if("mutation"===u){var T={};if(r.matchToken("of"))do{if(r.matchToken("anything"))T.attributes=!0,T.subtree=!0,T.characterData=!0,T.childList=!0;else if(r.matchToken("childList"))T.childList=!0;else if(r.matchToken("attributes"))T.attributes=!0,T.attributeOldValue=!0;else if(r.matchToken("subtree"))T.subtree=!0;else if(r.matchToken("characterData"))T.characterData=!0,T.characterDataOldValue=!0;else if("ATTRIBUTE_REF"===r.currentToken().type){var k=r.consumeToken();null==T.attributeFilter&&(T.attributeFilter=[]),0==k.value.indexOf("@")?T.attributeFilter.push(k.value.substring(1)):t.raiseParseError(r,"Only shorthand attribute references are allowed here")}else t.raiseParseError(r,"Unknown mutation config specification")}while(r.matchToken("or"));else T.attributes=!0,T.characterData=!0,T.childList=!0}var y=null,g=!1;if(r.matchToken("from")&&(r.matchToken("elsewhere")?g=!0:(y=t.parseElement("expression",r))||t.raiseParseError(r,'Expected either target value or "elsewhere".')),null===y&&!1===g&&r.matchToken("elsewhere")&&(g=!0),r.matchToken("in"))var x=t.parseAnyOf(["idRef","queryRef","classRef"],r);if(r.matchToken("debounced")){r.requireToken("at");var b=t.requireElement("timeExpression",r).evaluate({})}else if(r.matchToken("throttled")){r.requireToken("at");var q=t.requireElement("timeExpression",r).evaluate({})}o.push({execCount:0,every:a,on:u,args:s,filter:c,from:y,inExpr:x,elsewhere:g,startCount:m,endCount:d,unbounded:v,debounceTime:b,throttleTime:q,mutationSpec:T,intersectionSpec:E,debounced:void 0,lastExec:void 0})}while(r.matchToken("or"));var N=!0;if(!a&&r.matchToken("queue"))if(r.matchToken("all"))N=!1;else if(r.matchToken("first"))var I=!0;else if(r.matchToken("none"))var w=!0;else r.requireToken("last");var O=t.requireElement("commandList",r);if(t.ensureTerminated(O),r.matchToken("catch")){var L=r.requireTokenType("IDENTIFIER").value,C=t.requireElement("commandList",r);t.ensureTerminated(C)}if(r.matchToken("finally")){var R=t.requireElement("commandList",r);t.ensureTerminated(R)}var S={displayName:i,events:o,start:O,every:a,execCount:0,errorHandler:C,errorSymbol:L,execute:function(e){let t=n.getEventQueueFor(e.me,S);if(t.executing&&!1===a){if(w||I&&t.queue.length>0)return;return N&&(t.queue.length=0),void t.queue.push(e)}S.execCount++,t.executing=!0,e.meta.onHalt=function(){t.executing=!1;var e=t.queue.shift();e&&setTimeout(function(){S.execute(e)},1)},e.meta.reject=function(t){console.error(t.message?t.message:t);var r=n.getHyperTrace(e,t);r&&r.print(),n.triggerEvent(e.me,"exception",{error:t})},O.execute(e)},install:function(t,r){for(const r of S.events){var a;a=r.elsewhere?[document]:r.from?r.from.evaluate(n.makeContext(t,S,t,null)):[t],n.forEach(a,function(a){var o=r.on;if(r.mutationSpec&&(o="hyperscript:mutation",new MutationObserver(function(e,t){S.executing||l.triggerEvent(a,o,{mutationList:e,observer:t})}).observe(a,r.mutationSpec)),r.intersectionSpec){o="hyperscript:insersection";const t=new IntersectionObserver(function(n){for(const i of n){var r={observer:t};(r=e(r,i)).intersecting=i.isIntersecting,l.triggerEvent(a,o,r)}},r.intersectionSpec);t.observe(a)}(a.addEventListener||a.on).call(a,o,function e(i){if("undefined"!=typeof Node&&t instanceof Node&&a!==t&&!t.isConnected)a.removeEventListener(o,e);else{var u=n.makeContext(t,S,t,i);if(!r.elsewhere||!t.contains(i.target)){r.from&&(u.result=a);for(const e of r.args)u[e.value]=u.event[e.value]||("detail"in u.event?u.event.detail[e.value]:null);if(u.meta.errorHandler=C,u.meta.errorSymbol=L,u.meta.finallyHandler=R,r.filter){var s=u.meta.context;u.meta.context=u.event;try{if(!r.filter.evaluate(u))return}finally{u.meta.context=s}}if(r.inExpr)for(var l=i.target;;){if(l.matches&&l.matches(r.inExpr.css)){u.result=l;break}if(null==(l=l.parentElement))return}if(r.execCount++,r.startCount)if(r.endCount){if(r.execCount<r.startCount||r.execCount>r.endCount)return}else if(r.unbounded){if(r.execCount<r.startCount)return}else if(r.execCount!==r.startCount)return;if(r.debounceTime)return r.debounced&&clearTimeout(r.debounced),void(r.debounced=setTimeout(function(){S.execute(u)},r.debounceTime));if(r.throttleTime){if(r.lastExec&&Date.now()<r.lastExec+r.throttleTime)return;r.lastExec=Date.now()}S.execute(u)}}})})}}};return t.setParent(O,S),S}}),u.addFeature("def",function(e,t,n){if(n.matchToken("def")){var r=e.requireElement("dotOrColonPath",n).evaluate(),a=r.split("."),o=a.pop(),i=[];if(n.matchOpToken("("))if(n.matchOpToken(")"));else{do{i.push(n.requireTokenType("IDENTIFIER"))}while(n.matchOpToken(","));n.requireOpToken(")")}var u=e.requireElement("commandList",n);if(n.matchToken("catch"))var s=n.requireTokenType("IDENTIFIER").value,l=e.parseElement("commandList",n);if(n.matchToken("finally")){var c=e.requireElement("commandList",n);e.ensureTerminated(c)}var f={displayName:o+"("+i.map(function(e){return e.value}).join(", ")+")",name:o,args:i,start:u,errorHandler:l,errorSymbol:s,finallyHandler:c,install:function(e,n){var m=function(){var r=t.makeContext(n,f,e,null);r.meta.errorHandler=l,r.meta.errorSymbol=s,r.meta.finallyHandler=c;for(var a=0;a<i.length;a++){var o=i[a],m=arguments[a];o&&(r[o.value]=m)}r.meta.caller=arguments[i.length],r.meta.caller&&(r.meta.callingCommand=r.meta.caller.meta.command);var p,h=null,d=new Promise(function(e,t){p=e,h=t});return u.execute(r),r.meta.returned?r.meta.returnValue:(r.meta.resolve=p,r.meta.reject=h,d)};m.hyperfunc=!0,m.hypername=r,t.assignToNamespace(e,a,o,m)}};return e.ensureTerminated(u),l&&e.ensureTerminated(l),e.setParent(u,f),f}}),u.addFeature("set",function(e,t,n){let r=e.parseElement("setCommand",n);if(r){"element"!==r.target.scope&&e.raiseParseError(n,"variables declared at the feature level must be element scoped.");let a={start:r,install:function(e,n){r&&r.execute(t.makeContext(e,a,e,null))}};return e.ensureTerminated(r),a}}),u.addFeature("init",function(e,t,n){if(n.matchToken("init")){var r=e.requireElement("commandList",n),a={start:r,install:function(e,n){setTimeout(function(){r&&r.execute(t.makeContext(e,a,e,null))},0)}};return e.ensureTerminated(r),e.setParent(r,a),a}}),u.addFeature("worker",function(e,t,n){n.matchToken("worker")&&e.raiseParseError(n,"In order to use the 'worker' feature, include the _hyperscript worker plugin. See https://hyperscript.org/features/worker/ for more info.")}),u.addFeature("behavior",function(e,n,r){if(r.matchToken("behavior")){var o=e.requireElement("dotOrColonPath",r).evaluate(),i=o.split("."),u=i.pop(),s=[];if(r.matchOpToken("(")&&!r.matchOpToken(")")){do{s.push(r.requireTokenType("IDENTIFIER").value)}while(r.matchOpToken(","));r.requireOpToken(")")}for(var l=e.requireElement("hyperscript",r),c=0;c<l.features.length;c++)l.features[c].behavior=o;return{install:function(e,r){n.assignToNamespace(a.document&&a.document.body,i,u,function(e,r,a){for(var i=t(n.getInternalData(e),o+"Scope"),u=0;u<s.length;u++)i[s[u]]=a[s[u]];l.apply(e,r)})}}}}),u.addFeature("install",function(e,t,n){if(n.matchToken("install")){var r,o=e.requireElement("dotOrColonPath",n).evaluate(),i=o.split("."),u=e.parseElement("namedArgumentList",n);return r={install:function(e,n){t.unifiedEval({args:[u],op:function(t,r){for(var u=a,s=0;s<i.length;s++)if("object"!=typeof(u=u[i[s]])&&"function"!=typeof u)throw new Error("No such behavior defined as "+o);if(!(u instanceof Function))throw new Error(o+" is not a behavior");u(e,n,r)}},t.makeContext(e,r,e))}}}}),u.addGrammarElement("jsBody",function(e,t,n){for(var r=n.currentToken().start,a=n.currentToken(),o=[],i="",u=!1;n.hasMore();){a=n.consumeToken();var s=n.token(0,!0);if("IDENTIFIER"===s.type&&"end"===s.value)break;u?"IDENTIFIER"===a.type||"NUMBER"===a.type?i+=a.value:(""!==i&&o.push(i),i="",u=!1):"IDENTIFIER"===a.type&&"function"===a.value&&(u=!0)}return{type:"jsBody",exposedFunctionNames:o,jsSource:n.source.substring(r,a.end+1)}}),u.addFeature("js",function(t,n,r){if(r.matchToken("js")){var o=t.requireElement("jsBody",r),i=o.jsSource+"\nreturn { "+o.exposedFunctionNames.map(function(e){return e+":"+e}).join(",")+" } ",u=new Function(i);return{jsSource:i,function:u,exposedFunctionNames:o.exposedFunctionNames,install:function(){e(a,u())}}}}),u.addCommand("js",function(e,t,r){if(r.matchToken("js")){var o=[];if(r.matchOpToken("("))if(r.matchOpToken(")"));else{do{var i=r.requireTokenType("IDENTIFIER");o.push(i.value)}while(r.matchOpToken(","));r.requireOpToken(")")}var u=e.requireElement("jsBody",r);r.matchToken("end");var s=n(Function,o.concat([u.jsSource]));return{jsSource:u.jsSource,function:s,inputs:o,op:function(e){var n=[];o.forEach(function(r){n.push(t.resolveSymbol(r,e,"default"))});var r=s.apply(a,n);return r&&"function"==typeof r.then?new Promise(function(n){r.then(function(r){e.result=r,n(t.findNext(this,e))})}):(e.result=r,t.findNext(this,e))}}}}),u.addCommand("async",function(e,t,n){if(n.matchToken("async")){if(n.matchToken("do")){for(var r=a=e.requireElement("commandList",n);r.next;)r=r.next;r.next=t.HALT,n.requireToken("end")}else var a=e.requireElement("command",n);var o={body:a,op:function(e){return setTimeout(function(){a.execute(e)}),t.findNext(this,e)}};return e.setParent(a,o),o}}),u.addCommand("tell",function(e,t,n){var r=n.currentToken();if(n.matchToken("tell")){var a=e.requireElement("expression",n),o=e.requireElement("commandList",n);n.hasMore()&&!e.featureStart(n.currentToken())&&n.requireToken("end");var i="tell_"+r.start,u={value:a,body:o,args:[a],resolveNext:function(e){var n=e.meta.iterators[i];return n.index<n.value.length?(e.beingTold=n.value[n.index++],o):(e.beingTold=n.originalBeingTold,this.next?this.next:t.findNext(this.parent,e))},op:function(e,t){return null==t?t=[]:Array.isArray(t)||t instanceof NodeList||(t=[t]),e.meta.iterators[i]={originalBeingTold:e.beingTold,index:0,value:t},this.resolveNext(e)}};return e.setParent(o,u),u}}),u.addCommand("wait",function(e,t,n){if(n.matchToken("wait")){var r,a;if(n.matchToken("for")){n.matchToken("a");var o=[];do{var i=n.token(0);o.push("NUMBER"===i.type||"L_PAREN"===i.type?{time:e.requireElement("