hyperscript.org
Version:
a small scripting language for the web
1 lines • 84.3 kB
JavaScript
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 o=globalThis;class a{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)||a(e)||"-"===e||"_"===e||":"===e}function n(e){return i(e)||a(e)||"-"===e||"_"===e||":"===e}function r(e){return" "===e||"\t"===e||o(e)}function o(e){return"\r"===e||"\n"===e}function a(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){o();var r=null;function o(){for(;"WHITESPACE"===m(0,!0).type;)t.push(e.shift())}function a(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===d.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,o(),n}function f(n,r){for(var a=[],i=m(0,!0);!(null!=r&&i.type===r||null!=n&&i.value===n||"EOF"===i.type);){var u=e.shift();t.push(u),a.push(i),i=m(0,!0)}return o(),a}function m(t,n){var r,o=0;do{if(!n)for(;e[o]&&"WHITESPACE"===e[o].type;)o++;r=e[o],t--,o++}while(t>-1);return r||{type:"EOF",value:"<<<EOF>>>"}}function p(){return m(0)}var d=[];return{pushFollow:function(e){d.push(e)},popFollow:function(){d.pop()},clearFollow:function(){var e=d;return d=[],e},restoreFollow:function(e){d=e},matchAnyToken:function(e,t,n){for(var r=0;r<arguments.length;r++){var o=arguments[r],a=l(o);if(a)return a}},matchAnyOpToken:function(e,t,n){for(var r=0;r<arguments.length;r++){var o=arguments[r],a=i(o);if(a)return a}},matchOpToken:i,requireOpToken:function(e){var t=i(e);if(t)return t;a(this,"Expected '"+e+"' but found '"+p().value+"'")},matchTokenType:s,requireTokenType:function(e,t,n,r){var o=s(e,t,n,r);if(o)return o;a(this,"Expected one of "+JSON.stringify([e,t,n]))},consumeToken:c,peekToken:function(t,n,r){return e[n]&&e[n].value===t&&e[n].type===r},matchToken:l,requireToken:function(e,t){var n=l(e,t);if(n)return n;a(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=[],d=u,h=0,v=0,E=1,T="<START>",y=0;function k(){return f&&0===y}for(;h<d.length;)if("-"!==O()||"-"!==A()||!r(F())&&""!==F())if(r(O()))p.push(D());else if(M()||"."!==O()||!i(A())&&"{"!==A())if(M()||"#"!==O()||!i(A())&&"{"!==A())if("["===O()&&"@"===A())p.push(q());else if("@"===O())p.push(N());else if("*"===O()&&i(A()))p.push(w());else if(i(O())||!k()&&s(O()))p.push(I());else if(a(O()))p.push(R());else if(k()||'"'!==O()&&"`"!==O())if(k()||"'"!==O()){if(e[O()])"$"===T&&"{"===O()&&y++,"}"===O()&&y--,p.push(L());else if(k()||"`"===(m=O())||"^"===m)p.push(g("RESERVED",P()));else if(h<d.length)throw Error("Unknown token: "+O()+" ")}else c(p)?p.push(C()):p.push(L());else p.push(C());else p.push(S());else p.push(b());else x();return l(p,[],d);function g(e,t){return{type:e,value:t,start:h,end:h+1,column:v,line:E}}function x(){for(;O()&&!o(O());)P();P()}function b(){var e=g("CLASS_REF"),n=P();if("{"===O()){for(e.template=!0,n+=P();O()&&"}"!==O();)n+=P();if("}"!==O())throw Error("Unterminated class reference");n+=P()}else for(;t(O());)n+=P();return e.value=n,e.end=h,e}function q(){for(var e=g("ATTRIBUTE_REF"),t=P();h<d.length&&"]"!==O();)t+=P();return"]"===O()&&(t+=P()),e.value=t,e.end=h,e}function N(){for(var e=g("ATTRIBUTE_REF"),t=P();n(O());)t+=P();return e.value=t,e.end=h,e}function w(){for(var e=g("STYLE_REF"),t=P();i(O())||"-"===O();)t+=P();return e.value=t,e.end=h,e}function S(){var e=g("ID_REF"),t=P();if("{"===O()){for(e.template=!0,t+=P();O()&&"}"!==O();)t+=P();if("}"!==O())throw Error("Unterminated id reference");P()}else for(;n(O());)t+=P();return e.value=t,e.end=h,e}function I(){for(var e=g("IDENTIFIER"),t=P();i(O())||a(O())||s(O());)t+=P();return"!"===O()&&"beep"===t&&(t+=P()),e.value=t,e.end=h,e}function R(){for(var e=g("NUMBER"),t=P();a(O());)t+=P();for("."===O()&&a(A())&&(t+=P());a(O());)t+=P();return e.value=t,e.end=h,e}function L(){for(var t=(r=void 0,(r=g(void 0,void 0)).op=!0,r),n=P();O()&&e[n+O()];)n+=P();var r;return t.type=e[n],t.value=n,t.end=h,t}function C(){for(var e,t=g("STRING"),n=P(),r="";O()&&O()!==n;)if("\\"===O()){P();let e=P();r+="b"===e?"\b":"f"===e?"\f":"n"===e?"\n":"r"===e?"\r":"t"===e?"\t":"v"===e?"\v":e}else r+=P();if(O()!==n)throw Error("Unterminated string at [Line: "+(e=t).line+", Column: "+e.column+"]");return P(),t.value=r,t.end=h,t.template="`"===n,t}function O(){return d.charAt(h)}function A(){return d.charAt(h+1)}function F(){return d.charAt(h+2)}function P(){return T=O(),h++,v++,T}function M(){return i(T)||a(T)||")"===T||'"'===T||"'"===T||"`"===T||"}"===T||"]"===T}function D(){for(var e=g("WHITESPACE"),t="";O()&&r(O());)o(O())&&(v=0,E++),t+=P();return e.value=t,e.end=h,e}},makeTokensObject:l}}(),u=function(){var e={},t={},n={},r=[],o=[];function a(e,t,n){e.startToken=t,e.sourceFor=n.sourceFor,e.lineFor=n.lineFor,e.programSource=n.source}function i(t,n,r){var o=e[t];if(o){var i=n.currentToken(),s=o(u,l,n,r);if(s)for(a(s,i,n),s.endToken=s.endToken||n.lastMatch(),r=s.root;null!=r;)a(r,i,n),r=r.root;return s}}function s(e,t,n,r){var o=i(e,t,r);return o||m(t,n||"Expected "+e),o}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 d(e){return n[e.value]}return f("feature",function(e,t,r){if(r.matchOpToken("(")){var o=e.requireElement("feature",r);return r.requireOpToken(")"),o}var a=n[r.currentToken().value];if(a)return a(e,t,r)}),f("command",function(e,n,r){if(r.matchOpToken("(")){const t=e.requireElement("command",r);return r.requireOpToken(")"),t}var o=t[r.currentToken().value];let a;return o?a=o(e,n,r):"IDENTIFIER"===r.currentToken().type&&(a=e.parseElement("pseudoCommand",r)),a?e.parseElement("indirectStatement",r,a):a}),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 o=c(r,n);return null==o?i("symbol",n):o}),f("indirectExpression",function(e,t,n,r){for(var a=0;a<o.length;a++){var i=o[a];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 o={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=o,o}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:d,commandStart:p,commandBoundary:function(e){return!("end"!=e.value&&"then"!=e.value&&"else"!=e.value&&"otherwise"!=e.value&&")"!=e.value&&!p(e)&&!d(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 o=n+"Command",a=function(e,t,n){const a=r(e,t,n);if(a)return a.type=o,a.execute=function(e){return e.meta.command=a,t.unifiedExec(this,e)},a};e[o]=a,t[n]=a},addFeature:function(t,r){var o=t+"Feature",a=function(e,n,a){var i=r(e,n,a);if(i)return i.isFeature=!0,i.keyword=t,i.type=o,i};e[o]=a,n[t]=a},addLeafExpression:function(e,t){r.push(e),f(e,t)},addIndirectExpression:function(e,t){o.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,r){(n=n||{}).sender=r;var a=function(e,t){var n;return o.Event&&"function"==typeof o.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(a)}function f(e){return Array.isArray(e)||"undefined"!=typeof NodeList&&(e instanceof NodeList||e instanceof HTMLCollection)}function m(e){return e instanceof a||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)}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 o=n[r];o.asyncWrapper&&(n[r]=o.value)}}}var h={};function v(e,t){var n=[t],r=!1,o=!1;if(e.args)for(var a=0;a<e.args.length;a++){var i=e.args[a];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&&(o=!0)),u.push(c)}n.push(u)}else if(i.evaluate){var c;(c=i.evaluate(t))&&(c.then?r=!0:c.asyncWrapper&&(o=!0)),n.push(c)}else n.push(i)}return r?new Promise(function(t,r){n=function(e){for(var t=[],n=0;n<e.length;n++){var r=e[n];Array.isArray(r)?t.push(Promise.all(r)):t.push(r)}return t}(n),Promise.all(n).then(function(n){o&&d(n);try{var a=e.op.apply(e,n);t(a)}catch(e){r(e)}}).catch(function(e){r(e)})}):(o&&d(n),e.op.apply(e,n))}let E=null;function T(){return null==E&&(E=r.config.attributes.replace(/ /g,"").split(",")),E}function y(e){for(var t=0;t<T().length;t++){var n=T()[t];if(e.hasAttribute&&e.hasAttribute(n))return e.getAttribute(n)}return e instanceof HTMLScriptElement&&"text/hyperscript"===e.type?e.innerText:null}var k=new WeakMap;function g(e){var t=k.get(e);return void 0===t&&k.set(e,t={}),t}function x(t,n){t&&(e(n,g(t)),x(t.parentElement,n))}function b(e,t,n,r){var a={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,sender:r&&r.detail?r.detail.sender:null,body:"document"in o?document.body:null};return a.meta.ctx=a,x(e,a),a}function q(e){var t=i.tokenize(e);if(u.commandStart(t.currentToken())){var n=u.requireElement("commandList",t);return t.hasMore()&&u.raiseParseError(t),u.ensureTerminated(n),n}if(u.featureStart(t.currentToken())){var r=u.requireElement("hyperscript",t);return t.hasMore()&&u.raiseParseError(t),r}var o=u.requireElement("expression",t);return t.hasMore()&&u.raiseParseError(t),o}function N(e,t){if(!e.closest||!e.closest(r.config.disableSelector)){var n=S(e);if(!n.initialized){var o=y(e);if(o)try{n.initialized=!0,n.script=o;var a=i.tokenize(o),s=u.parseHyperScript(a);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 S(e){var t=w.get(e);return void 0===t&&w.set(e,t={}),t}function I(e){var n=e.meta&&e.meta.owner;if(n){var r=S(n),o="elementScope";return e.meta.feature&&e.meta.feature.behavior&&(o=e.meta.feature.behavior+"Scope"),t(r,o)}return{}}function R(e,t,n){if(null!=e){var r=n(e,t);if(void 0!==r)return r;if(m(e)){var o=[];for(var a of e){var i=n(a,t);i&&o.push(i)}return o}}}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)},shouldAutoIterate:m,triggerEvent:c,matchesSelector:n,getScript:y,processNode:function(e){var t=l.getScriptSelector();n(e,t)&&N(e,e),e instanceof HTMLScriptElement&&"text/hyperscript"===e.type&&N(e,document.body),e.querySelectorAll&&p(e.querySelectorAll(t+", [type='text/hyperscript']"),function(e){N(e,e instanceof HTMLScriptElement&&"text/hyperscript"===e.type?document.body:e)})},evaluate:function(t,n,r){class a extends EventTarget{constructor(e){super(),this.module=e}toString(){return this.module.id}}var i="document"in o?o.document.body:new a(r&&r.module);n=e(b(i,null,i,null),n||{});var u=q(t);return u.execute?(u.execute(n),n.result):u.apply?(u.apply(i,i,r),g(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:q,getScriptSelector:function(){return T().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 o[e];if("element"===n)return I(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 a=t[e];return void 0!==a||void 0!==(a=I(t)[e])?a:o[e]},setSymbol:function(e,t,n,r){if("global"===n)o[e]=r;else if("element"===n)(a=I(t))[e]=r;else if("local"===n)t[e]=r;else{var a,i=t[e];void 0!==i?t[e]=r:void 0!==(i=(a=I(t))[e])?a[e]=r:t[e]=r}},makeContext:b,findNext:function e(t,n){if(t)return t.resolveNext?t.resolveNext(n):t.next?t.next:e(t.parent,n)},unifiedEval:v,convertValue:function(e,t){for(var n=s.dynamicResolvers,r=0;r<n.length;r++){var o=(0,n[r])(t,e);if(void 0!==o)return o}if(null==e)return null;var a=s[t];if(a)return a(e);throw"Unknown conversion : "+t},unifiedExec:function e(t,n){for(;;){try{var r=v(t,n)}catch(e){if(n.meta.handlingFinally)console.error(" Exception in finally block: ",e),r=h;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=h}}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===h){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){return R(e,t,(e,t)=>e[t])},resolveAttribute:function(e,t){return R(e,t,(e,t)=>e.getAttribute&&e.getAttribute(t))},resolveStyle:function(e,t){return R(e,t,(e,t)=>e.style&&e.style[t])},resolveComputedStyle:function(e,t){return R(e,t,(e,t)=>getComputedStyle(e).getPropertyValue(t))},assignToNamespace:function(e,t,n,r){let a;for(a="undefined"!=typeof document&&e===document.body?o:g(e);t.length>0;){var i=t.shift(),u=a[i];null==u&&(a[i]=u={}),a=u}a[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 o=n[r];e(" ->",o.meta.feature.displayName.padEnd(t+2),"-",o.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:S,getHyperscriptFeatures:g,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=S(e);var r=n.eventQueues;null==r&&(r=new Map,n.eventQueues=r);var o=r.get(t);return null==o&&r.set(t,o={queue:[],executing:!1}),o},hyperscriptUrl:"document"in o?import.meta.url:null,HALT:h}}();{u.addLeafExpression("parenthesized",function(e,t,n){if(n.matchOpToken("(")){var r=n.clearFollow();try{var o=e.requireElement("expression",n)}finally{n.restoreFollow(r)}return n.requireOpToken(")"),o}}),u.addLeafExpression("string",function(e,t,n){var r=n.matchTokenType("STRING");if(r){var o,a=r.value;if(r.template){var u=i.tokenize(a,!0);o=e.parseStringTemplate(u)}else o=[];return{type:"string",token:r,args:o,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===o.length?a: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 o=r,a=parseFloat(r.value);return{type:"number",value:a,numberToken:o,evaluate:function(){return a}}}}),u.addLeafExpression("idRef",function(e,t,n){var r=n.matchTokenType("ID_REF");if(r){if(r.template){var o=r.value.substr(2,r.value.length-2),a=i.tokenize(o);return{type:"idRefTemplate",args:[e.requireElement("expression",a)],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 o=r.value.substr(2,r.value.length-2),u=i.tokenize(o);return{type:"classRefTemplate",args:[e.requireElement("expression",u)],op:function(e,t){return new a("."+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 a(e,t.me,!0)}}}}});class r extends a{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 o=n.consumeUntil("/");n.requireOpToken("/"),n.requireOpToken(">");var u=o.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 a(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 o=r.value;if(0===o.indexOf("["))var a=o.substring(2,o.length-1);else a=o.substring(1);var i="["+a+"]",u=a.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.addLeafExpression("styleRef",function(e,t,n){var r=n.matchTokenType("STYLE_REF");if(r){var o=r.value.substr(1);return o.startsWith("computed-")?{type:"computedStyleRef",name:o=o.substr("computed-".length),op:function(e){var n=e.beingTold||e.me;if(n)return t.resolveComputedStyle(n,o)},evaluate:function(e){return t.unifiedEval(this,e)}}:{type:"styleRef",name:o,op:function(e){var n=e.beingTold||e.me;if(n)return t.resolveStyle(n,o)},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 o=e.parseElement("expression",n);return n.requireOpToken("]"),{type:"objectKey",expr:o,args:[o],op:function(e,t){return t},evaluate:function(e){return t.unifiedEval(this,e)}}}var a="";do{(r=n.matchTokenType("IDENTIFIER")||n.matchOpToken("-"))&&(a+=r.value)}while(r);return{type:"objectKey",key:a,evaluate:function(){return a}}}),u.addLeafExpression("objectLiteral",function(e,t,n){if(n.matchOpToken("{")){var r=[],o=[];if(!n.matchOpToken("}")){do{var a=e.requireElement("objectKey",n);n.requireOpToken(":");var i=e.requireElement("expression",n);o.push(i),r.push(a)}while(n.matchOpToken(","));n.requireOpToken("}")}return{type:"objectLiteral",args:[r,o],op:function(e,t,n){for(var r={},o=0;o<t.length;o++)r[t[o]]=n[o];return r},evaluate:function(e){return t.unifiedEval(this,e)}}}}),u.addGrammarElement("nakedNamedArgumentList",function(e,t,n){var r=[],o=[];if("IDENTIFIER"===n.currentToken().type)do{var a=n.requireTokenType("IDENTIFIER");n.requireOpToken(":");var i=e.requireElement("expression",n);o.push(i),r.push({name:a,value:i})}while(n.matchOpToken(","));return{type:"namedArgumentList",fields:r,args:[o],op:function(e,t){for(var n={_namedArgList_:!0},o=0;o<t.length;o++)n[r[o].name.value]=t[o];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 o=n.matchOpToken(":"),a=n.matchTokenType("IDENTIFIER");if(a){var i=a.value;return o&&(i=":"+i),"default"===r&&(0===i.indexOf("$")&&(r="global"),0===i.indexOf(":")&&(r="element")),{type:"symbol",token:a,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 o="true"===r.value;return{type:"boolean",evaluate:function(e){return o}}}),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 o=e.requireElement("expression",n);r.push(o)}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=[],o=n.matchTokenType("IDENTIFIER");if(o)for(r.push(o);n.matchOpToken(",");)r.push(n.requireTokenType("IDENTIFIER"));n.requireOpToken("-"),n.requireOpToken(">");var a=e.requireElement("expression",n);return{type:"blockLiteral",args:r,expr:a,evaluate:function(e){return function(){for(var t=0;t<r.length;t++)e[r[t].value]=arguments[t];return a.evaluate(e)}}}}}),u.addIndirectExpression("propertyAccess",function(e,t,n,r){if(n.matchOpToken(".")){var o=n.requireTokenType("IDENTIFIER");return e.parseElement("indirectExpression",n,{type:"propertyAccess",root:r,prop:o,args:[r],op:function(e,n){return t.resolveProperty(n,o.value)},evaluate:function(e){return t.unifiedEval(this,e)}})}}),u.addIndirectExpression("of",function(e,t,n,r){if(n.matchToken("of")){for(var o=e.requireElement("unaryExpression",n),a=null,i=r;i.root;)a=i,i=i.root;"symbol"!==i.type&&"attributeRef"!==i.type&&"styleRef"!==i.type&&"computedStyleRef"!==i.type&&e.raiseParseError(n,"Cannot take a property of a non-symbol: "+i.type);var u="attributeRef"===i.type,s="styleRef"===i.type||"computedStyleRef"===i.type;if(u||s)var l=i;var c=i.name,f={type:"ofExpression",prop:i.token,root:o,attribute:l,expression:r,args:[o],op:function(e,n){return u?t.resolveAttribute(n,c):s?"computedStyleRef"===i.type?t.resolveComputedStyle(n,c):t.resolveStyle(n,c):t.resolveProperty(n,c)},evaluate:function(e){return t.unifiedEval(this,e)}};return"attributeRef"===i.type&&(f.attribute=i),a?(a.root=f,a.args=[f]):r=f,e.parseElement("indirectExpression",n,r)}}),u.addIndirectExpression("possessive",function(e,t,n,r){if(!e.possessivesDisabled){var o=n.matchOpToken("'");if(o||"symbol"===r.type&&("my"===r.name||"its"===r.name||"your"===r.name)&&("IDENTIFIER"===n.currentToken().type||"ATTRIBUTE_REF"===n.currentToken().type||"STYLE_REF"===n.currentToken().type)){o&&n.requireToken("s");var a=e.parseElement("attributeRef",n);if(null==a){var i=e.parseElement("styleRef",n);if(null==i)var u=n.requireTokenType("IDENTIFIER")}return e.parseElement("indirectExpression",n,{type:"possessive",root:r,attribute:a||i,prop:u,args:[r],op:function(e,n){if(a)var r=t.resolveAttribute(n,a.name);else r=i?"computedStyleRef"===i.type?t.resolveComputedStyle(n,i.name):t.resolveStyle(n,i.name):t.resolveProperty(n,u.value);return r},evaluate:function(e){return t.unifiedEval(this,e)}})}}}),u.addIndirectExpression("inExpression",function(e,t,n,r){if(n.matchToken("in")){var o={type:"inExpression",root:r,args:[r,e.requireElement("unaryExpression",n)],op:function(e,n,r){var o=[];if(n.css)t.implicitLoop(r,function(e){for(var t=e.querySelectorAll(n.css),r=0;r<t.length;r++)o.push(t[r])});else if(n instanceof Element){var a=!1;if(t.implicitLoop(r,function(e){e.contains(n)&&(a=!0)}),a)return n}else t.implicitLoop(n,function(e){t.implicitLoop(r,function(t){e===t&&o.push(e)})});return o},evaluate:function(e){return t.unifiedEval(this,e)}};return e.parseElement("indirectExpression",n,o)}}),u.addIndirectExpression("asExpression",function(e,t,n,r){if(n.matchToken("as")){n.matchToken("a")||n.matchToken("an");var o=e.requireElement("dotOrColonPath",n).evaluate();return e.parseElement("indirectExpression",n,{type:"asExpression",root:r,args:[r],op:function(e,n){return t.convertValue(n,o)},evaluate:function(e){return t.unifiedEval(this,e)}})}}),u.addIndirectExpression("functionCall",function(e,t,n,r){if(n.matchOpToken("(")){var o=[];if(!n.matchOpToken(")")){do{o.push(e.requireElement("expression",n))}while(n.matchOpToken(","));n.requireOpToken(")")}if(r.root)var a={type:"functionCall",root:r,argExressions:o,args:[r.root,o],op:function(e,n,o){t.nullCheck(n,r.root);var a=n[r.prop.value];return t.nullCheck(a,r),a.hyperfunc&&o.push(e),a.apply(n,o)},evaluate:function(e){return t.unifiedEval(this,e)}};else a={type:"functionCall",root:r,argExressions:o,args:[r,o],op:function(e,n,o){return t.nullCheck(n,r),n.hyperfunc&&o.push(e),n.apply(null,o)},evaluate:function(e){return t.unifiedEval(this,e)}};return e.parseElement("indirectExpression",n,a)}}),u.addIndirectExpression("attributeRefAccess",function(e,t,n,r){var o=e.parseElement("attributeRef",n);if(o)return{type:"attributeRefAccess",root:r,attribute:o,args:[r],op:function(e,n){return t.resolveAttribute(n,o.name)},evaluate:function(e){return l.unifiedEval(this,e)}}}),u.addIndirectExpression("arrayIndex",function(e,t,n,r){if(n.matchOpToken("[")){var o=!1,a=!1,i=null,s=null;n.matchOpToken("..")?(o=!0,i=e.requireElement("expression",n)):(i=e.requireElement("expression",n),n.matchOpToken("..")&&(a=!0,"R_BRACKET"!==n.currentToken().type&&(s=e.parseElement("expression",n)))),n.requireOpToken("]");var c={type:"arrayIndex",root:r,prop:i,firstIndex:i,secondIndex:s,args:[r,i,s],op:function(e,t,n,r){return null==t?null:o?(n<0&&(n=t.length+n),t.slice(0,n+1)):a?null!=r?(r<0&&(r=t.length+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)}});var c=["em","ex","cap","ch","ic","rem","lh","rlh","vw","vh","vi","vb","vmin","vmax","cm","mm","Q","pc","pt","px"];u.addGrammarElement("postfixExpression",function(e,t,n){var r=e.parseElement("primaryExpression",n);let o=n.matchAnyToken.apply(n,c)||n.matchOpToken("%");if(o)return{type:"stringPostfix",postfix:o.value,args:[r],op:function(e,t){return""+t+o.value},evaluate:function(e){return t.unifiedEval(this,e)}};var a=null;if(n.matchToken("s")||n.matchToken("seconds")?a=1e3:(n.matchToken("ms")||n.matchToken("milliseconds"))&&(a=1),a)return{type:"timeExpression",time:r,factor:a,args:[r],op:function(e,t){return t*a},evaluate:function(e){return t.unifiedEval(this,e)}};if(n.matchOpToken(":")){var i=n.requireTokenType("IDENTIFIER"),u=!n.matchOpToken("!");return{type:"typeCheck",typeName:i,nullOk:u,args:[r],op:function(e,n){if(t.typeCheck(n,i.value,u))return n;throw new Error("Typecheck failed! Expected: "+i.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 n.matchToken("the"),e.parseAnyOf(["beepExpression","logicalNot","relativePositionalExpression","positionalExpression","noExpression","negativeNumber","postfixExpression"],n)}),u.addGrammarElement("beepExpression",function(e,t,n){if(n.matchToken("beep!")){var r=e.parseElement("unaryExpression",n);if(r){r.booped=!0;var o=r.evaluate;return r.evaluate=function(e){let n=o.apply(r,arguments),i=e.me;if(t.triggerEvent(i,"hyperscript:beep",{element:i,expression:r,value:n})){var u,s=n;"String"===(u=n?n instanceof a?"ElementCollection":n.constructor?n.constructor.name:"unknown":"object (null)")?s='"'+s+'"':n instanceof a&&(s=Array.from(n)),console.log("///_ BEEP! The expression ("+r.sourceFor().substr(6)+") evaluates to:",s,"of type "+u)}return n},r}}});var f=function(e,t,n,r){var o=[];l.forEach(t,function(t){(t.matches(n)||t===e)&&o.push(t)});for(var a=0;a<o.length-1;a++)if(o[a]===e)return o[a+1];if(r){var i=o[0];if(i&&i.matches(n))return i}};function m(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 p(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 o=!0;var a=e.parseElement("expression",n);if(n.matchToken("from")){n.pushFollow("in");try{var i=e.requireElement("unaryExpression",n)}finally{n.popFollow()}}else i=e.requireElement("implicitMeTarget",n);var u,s=!1;if(n.matchToken("in")){s=!0;var l=e.requireElement("unaryExpression",n)}else u=n.matchToken("within")?e.requireElement("unaryExpression",n):document.body;var c=!1;return n.matchToken("with")&&(n.requireToken("wrapping"),c=!0),{type:"relativePositionalExpression",from:i,forwardSearch:o,inSearch:s,wrapping:c,inElt:l,withinElt:u,operator:r.value,args:[a,i,l,u],op:function(e,t,n,r,a){var i,u,l=t.css;if(null==l)throw"Expected a CSS value";if(s){if(r)return o?f(n,r,l,c):(i=l,u=c,f(n,Array.from(r).reverse(),i,u))}else if(a)return o?function(e,t,n,r){for(var o=t.querySelectorAll(n),a=0;a<o.length;a++){var i=o[a];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_PRECEDING)return i}if(r)return o[0]}(n,a,l,c):function(e,t,n,r){for(var o=t.querySelectorAll(n),a=o.length-1;a>=0;a--){var i=o[a];if(i.compareDocumentPosition(e)===Node.DOCUMENT_POSITION_FOLLOWING)return i}if(r)return o[o.length-1]}(n,a,l,c)},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 o=e.requireElement("unaryExpression",n);const a=r.value;return{type:"positionalExpression",rhs:o,operator:r.value,args:[o],op:function(e,t){if(t&&!Array.isArray(t)&&(t=t.children?t.children:Array.from(t)),t){if("first"===a)return t[0];if("last"===a)return t[t.length-1];if("random"===a)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,o=e.parseElement("unaryExpression",n),a=null;for(r=n.matchAnyOpToken("+","-","*","/","%");r;){var i=r.value;(a=a||r).value!==i&&e.raiseParseError(n,"You must parenthesize math operations with different operators");var u=e.parseElement("unaryExpression",n);o={type:"mathOperator",lhs:o,rhs:u,operator:i,args:[o,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 o}),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),o=n.matchAnyOpToken("<",">","<=",">=","==","===","!=","!=="),a=o?o.value:null,i=!0,u=!1;if(null==a&&(n.matchToken("is")||n.matchToken("am")?n.matchToken("not")?n.matchToken("in")?a="not in":n.matchToken("a")?(a="not a",u=!0):n.matchToken("empty")?(a="not empty",i=!1):a="!=":n.matchToken("in")?a="in":n.matchToken("a")?(a="a",u=!0):n.matchToken("empty")?(a="empty",i=!1):n.matchToken("less")?(n.requireToken("than"),n.matchToken("or")?(n.requireToken("equal"),n.requireToken("to"),a="<="):a="<"):n.matchToken("greater")?(n.requireToken("than"),n.matchToken("or")?(n.requireToken("equal"),n.requireToken("to"),a=">="):a=">"):a="==":n.matchToken("exist")||n.matchToken("exists")?(a="exist",i=!1):n.matchToken("matches")||n.matchToken("match")?a="match":n.matchToken("contains")||n.matchToken("contain")?a="contain":n.matchToken("includes")||n.matchToken("include")?a="include":(n.matchToken("do")||n.matchToken("does"))&&(n.requireToken("not"),n.matchToken("matches")||n.matchToken("match")?a="not match":n.matchToken("contains")||n.matchToken("contain")?a="not contain":n.matchToken("exist")||n.matchToken("exist")?(a="not exist",i=!1):n.matchToken("include")?a="not include":e.raiseParseError(n,"Expected matches or contains"))),a){if(u)var s=n.requireTokenType("IDENTIFIER"),l=!n.matchOpToken("!");else if(i){var c=e.requireElement("mathExpression",n);"match"!==a&&"not match"!==a||(c=c.css?c.css:c)}var f=r;r={type:"comparisonOperator",operator:a,typeName:s,nullOk:l,lhs:r,rhs:c,args:[r,c],op:function(e,n,r){if("=="===a)return n==r;if("!="===a)return n!=r;if("match"===a)return null!=n&&p(f,n,r);if("not match"===a)return null==n||!p(f,n,r);if("in"===a)return null!=r&&m(c,r,n);if("not in"===a)return null==r||!m(c,r,n);if("contain"===a)return null!=n&&m(f,n,r);if("not contain"===a)return null==n||!m(f,n,r);if("include"===a)return null!=n&&m(f,n,r);if("not include"===a)return null==n||!m(f,n,r);if("==="===a)return n===r;if("!=="===a)return n!==r;if("<"===a)return n<r;if(">"===a)return n>r;if("<="===a)return n<=r;if(">="===a)return n>=r;if("empty"===a)return t.isEmpty(n);if("not empty"===a)return!t.isEmpty(n);if("exist"===a)return t.doesExist(n);if("not exist"===a)return!t.doesExist(n);if("a"===a)return t.typeCheck(n,s.value,l);if("not a"===a)return!t.typeCheck(n,s.value,l);throw"Unknown comparison : "+a},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,o=e.parseElement("comparisonExpression",n),a=null;for(r=n.matchToken("and")||n.matchToken("or");r;){(a=a||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;o={type:"logicalOperator",operator:u,lhs:o,rhs:i,args:[o,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 o}),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&&"styleRef"!==r.type&&"arrayIndex"!==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 o=e.requireElement("feature",n);r.push(o),n.matchToken("end")}return{type:"hyperscript",features:r,apply:function(e,t,n){for(const o of r)o.install(e,t,n)}}});var d=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 o=t.requireElement("eventName",r),a=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:o,details:a,to:i,args:[i,o,a],op:function(e,t,r,o){return n.nullCheck(t,i),n.forEach(t,function(t){n.triggerEvent(t,r,o,e.me)}),n.findNext(u,e)}};return u}u.addFeature("on",function(t,n,r){if(r.matchToken("on")){var o=!1;r.matchToken("every")&&(o=!0);var a=[],i=null;do{var u=t.requireElement("eventName",r,"Expected event name").evaluate();i=i?i+" or "+u:"on "+u;var s=d(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 p=r.consumeToken(),h=parseInt(p.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 y=r.consumeToken();null==T.attributeFilter&&(T.attributeFilter=[]),0==y.value.indexOf("@")?T.attributeFilter.push(y.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 k=null,g=!1;if(r.matchToken("from")&&(r.matchToken("elsewhere")?g=!0:(k=t.parseElement("expression",r))||t.raiseParseError(r,'Expected either target value or "elsewhere".')),null===k&&!1===g&&r.matchToken("elsewhere")&&(g=!0),r.matchToken("in"))var x=t.parseElement("unaryExpression",r);if(r.matchToken("debounced")){r.requireToken("at");var b=t.requireElement("expression",r).evaluate({})}else if(r.matchToken("throttled")){r.requireToken("at");var q=t.requireElement("expression",r).evaluate({})}a.push({execCount:0,every:o,on:u,args:s,filter:c,from:k,inExpr:x,elsewhere:g,startCount:m,endCount:h,unbounded:v,debounceTime:b,throttleTime:q,mutationSpec:T,intersectionSpec:E,debounced:void 0,lastExec:void 0})}while(r.matchToken("or"));var N=!0;if(!o&&r.matchToken("queue"))if(r.matchToken("all"))N=!1;else if(r.matchToken("first"))var w=!0;else if(r.matchToken("none"))var S=!0;else r.requireToken("last");var I=t.requireElement("commandList",r);if(t.ensureTerminated(I),r.matchToken("catch")){var R=r.requireTokenType("IDENTIFIER").value,L=t.requireElement("commandList",r);t.ensureTerminated(L)}if(r.matchToken("finally")){var C=t.requireElement("commandList",r);t.ensureTerminated(C)}var O={displayName:i,events:a,start:I,every:o,execCount:0,errorHandler:L,errorSymbol:R,execute:function(e){let t=n.getEventQueueFor(e.me,O);if(t.executing&&!1===o){if(S||w&&t.queue.length>0)return;return N&&(t.queue.length=0),void t.queue.push(e)}O.execCount++,t.executing=!0,e.meta.onHalt=function(){t.executing=!1;var e=t.queue.shift();e&&setTimeout(function(){O.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})},I.execute(e)},install:function(t,r){for(const r of O.events){var o;o=r.elsewhere?[document]:r.from?r.from.evaluate(n.makeContext(t,O,t,null)):[t],n.implicitLoop(o,function(o){var a=r.on;if(r.mutationSpec&&(a="hyperscript:mutation",new MutationObserver(function(e,t){O.executing||l.triggerEvent(o,a,{mutationList:e,observer:t})}).observe(o,r.mutationSpec)),r.intersectionSpec){a="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(o,a,r)}},r.intersectionSpec);t.observe(o)}(o.addEventListener||o.on).call(o,a,function e(i){if("undefined"!=typeof Node&&t instanceof Node&&o!==t&&!t.isConnected)o.removeEventListener(a,e);else{var u=n.makeContext(t,O,t,i);if(!r.elsewhere||!t.contains(i.target)){r.from&&(u.result=o);for(const e of r.args){let t=u.event[e.value];void 0!==t?u[e.value]=t:"detail"in u.event&&(u[e.value]=u.event.detail[e.value])}if(u.meta.errorHandler=L,u.meta.errorSymbol=R,u.meta.finallyHandler=C,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(){O.execute(u)},r.debounceTime));if(r.throttleTime){if(r.lastExec&&Date.now()<r.lastExec+r.throttleTime)return;r.lastExec=Date.now()}O.execute(u)}}})})}}};return t.setParent(I,O),O}}),u.addFeature("def",function(e,t,n){if(n.matchToken("def")){var r=e.requireElement("dotOrColonPath",n).evaluate(),o=r.split("."),a=o.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:a+"("+i.map(function(e){return e.value}).join(", ")+")",name:a,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 o=0;o<i.length;o++){var a=i[o],m=arguments[o];a&&(r[a.value]=m)}r.meta.caller=arguments[i.length],r.meta.caller&&(r.meta.callingCommand=r.meta.caller.meta.command);var p,d=null,h=new Promise(function(e,t){p=e,d=t});return u.execute(r),r.meta.returned?r.meta.returnValue:(r.meta.resolve=p,r.meta.reject=d,h)};m.hyperfunc=!0,m.hypername=r,t.assignToNamespace(e,o,a,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 o={start:r,install:function(e,n){r&&r.execute(t.makeContext(e,o,e,null))}};return e.ensureTerminated(r),o}}),u.addFeature("init",function(e,t,n){if(n.matchToken("init")){var r=e.requireElement("commandList",n),o={start:r,install:function(e,n){setTimeout(function(){r&&r.execute(t.makeContext(e,o,e,null))},0)}};return e.ensureTerminated(r),e.setParent(r,o),o}}),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 a=e.requireElement("dotOrColonPath",r).evaluate(),i=a.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=a;return{install:function(e,r){n.assignToNamespace(o.document&&o.document.body,i,u,function(e,r,o){for(var i=t(n.getInternalData(e),a+"Scope"),u=0;u<s.length;u++)i[s[u]]=o[s[u]];l.apply(e,r)})}}}}),u.addFeature("install",function(e,t,n){if(n.matchToken("install")){var r,a=e.requireElement("dotOrColonPath",n).evaluate(),i=a.split("."),u=e.parseElement("namedArgumentList",n);return r={install:function(e,n){t.unifiedEval({args:[u],op:function(t,r){for(var u=o,s=0;s<i.length;s++)if("object"!=typeof(u=u[i[s]])&&"function"!=typeof u)throw new Error("No such behavior defined as "+a);if(!(u instanceof Function))throw new Error(a+" 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,o=n.currentToken(),a=[],i="",u=!1;n.hasMore();){o=n.consumeToken();var s=n.token(0,!0);if("IDENTIFIER"===s.t