Skip to content

Commit

Permalink
hyperscript 0.9.9 release prep
Browse files Browse the repository at this point in the history
  • Loading branch information
1cg committed Jun 30, 2023
1 parent f997266 commit 1a3a5fb
Show file tree
Hide file tree
Showing 132 changed files with 75,612 additions and 53 deletions.
267 changes: 219 additions & 48 deletions dist/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2329,11 +2329,115 @@
return eventQueueForFeature;
}

beepValueToConsole(element, expression, value) {
if (this.triggerEvent(element, "hyperscript:beep", {element, expression, value})) {
var typeName;
if (value) {
if (value instanceof ElementCollection) {
typeName = "ElementCollection";
} else if (value.constructor) {
typeName = value.constructor.name;
} else {
typeName = "unknown";
}
} else {
typeName = "object (null)"
}
var logValue = value;
if (typeName === "String") {
logValue = '"' + logValue + '"';
} else if (value instanceof ElementCollection) {
logValue = Array.from(value);
}
console.log("///_ BEEP! The expression (" + Tokens.sourceFor.call(expression).replace("beep! ", "") + ") evaluates to:", logValue, "of type " + typeName);
}
}


/** @type string | null */
// @ts-ignore
hyperscriptUrl = "document" in globalScope && document.currentScript ? document.currentScript.src : null;
}


function getCookiesAsArray() {
let cookiesAsArray = document.cookie
.split("; ")
.map(cookieEntry => {
let strings = cookieEntry.split("=");
return {name: strings[0], value: decodeURIComponent(strings[1])}
});
return cookiesAsArray;
}

function clearCookie(name) {
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

function clearAllCookies() {
for (const cookie of getCookiesAsArray()) {
clearCookie(cookie.name);
}
}

const CookieJar = new Proxy({}, {
get(target, prop) {
if (prop === 'then' || prop === 'asyncWrapper') { // ignore special symbols
return null;
} else if (prop === 'length') {
return getCookiesAsArray().length
} else if (prop === 'clear') {
return clearCookie;
} else if (prop === 'clearAll') {
return clearAllCookies;
} else if (typeof prop === "string") {
if (!isNaN(prop)) {
return getCookiesAsArray()[parseInt(prop)];

} else {
let value = document.cookie
.split("; ")
.find((row) => row.startsWith(prop + "="))
?.split("=")[1];
if(value) {
return decodeURIComponent(value);
}
}
} else if (prop === Symbol.iterator) {
return getCookiesAsArray()[prop];
}
},
set(target, prop, value) {
var finalValue = null;
if ('string' === typeof value) {
finalValue = encodeURIComponent(value)
finalValue += ";samesite=lax"
} else {
finalValue = encodeURIComponent(value.value);
if (value.expires) {
finalValue+=";expires=" + value.maxAge;
}
if (value.maxAge) {
finalValue+=";max-age=" + value.maxAge;
}
if (value.partitioned) {
finalValue+=";partitioned=" + value.partitioned;
}
if (value.path) {
finalValue+=";path=" + value.path;
}
if (value.samesite) {
finalValue+=";samesite=" + value.path;
}
if (value.secure) {
finalValue+=";secure=" + value.path;
}
}
document.cookie= prop + "=" + value;
return true;
}
})

class Context {
/**
* @param {*} owner
Expand All @@ -2351,7 +2455,9 @@
iterators: {},
ctx: this
}
this.locals = {};
this.locals = {
cookies:CookieJar
};
this.me = hyperscriptTarget,
this.you = undefined
this.result = undefined
Expand Down Expand Up @@ -3465,27 +3571,7 @@
expression.evaluate = function(ctx){
let value = originalEvaluate.apply(expression, arguments);
let element = ctx.me;
if (runtime.triggerEvent(element, "hyperscript:beep", {element, expression, value})) {
var typeName;
if (value) {
if (value instanceof ElementCollection){
typeName = "ElementCollection";
} else if (value.constructor) {
typeName = value.constructor.name;
} else {
typeName = "unknown";
}
} else {
typeName = "object (null)"
}
var logValue = value;
if (typeName === "String") {
logValue = '"' + logValue + '"';
} else if (value instanceof ElementCollection) {
logValue = Array.from(value);
}
console.log("///_ BEEP! The expression (" + Tokens.sourceFor.call(expression).substr(6) + ") evaluates to:", logValue, "of type " + typeName);
}
runtime.beepValueToConsole(element, expression, value);
return value;
}
return expression;
Expand Down Expand Up @@ -3548,7 +3634,7 @@
if (!op) return;
var forwardSearch = op.value === "next";

var thing = parser.parseElement("expression", tokens);
var thingElt = parser.parseElement("expression", tokens);

if (tokens.matchToken("from")) {
tokens.pushFollow("in");
Expand Down Expand Up @@ -3587,12 +3673,12 @@
inElt: inElt,
withinElt: withinElt,
operator: op.value,
args: [thing, from, inElt, withinElt],
args: [thingElt, from, inElt, withinElt],
op: function (context, thing, from, inElt, withinElt) {

var css = thing.css;
if (css == null) {
throw "Expected a CSS value";
throw "Expected a CSS value to be returned by " + Tokens.sourceFor.apply(thingElt);
}

if(inSearch) {
Expand Down Expand Up @@ -3737,7 +3823,15 @@
operator = "not empty";
hasRightValue = false;
} else {
operator = "!=";
if (tokens.matchToken("really")) {
operator = "!==";
} else {
operator = "!=";
}
// consume additional optional syntax
if (tokens.matchToken("equal")) {
tokens.matchToken("to");
}
}
} else if (tokens.matchToken("in")) {
operator = "in";
Expand Down Expand Up @@ -3766,8 +3860,20 @@
operator = ">";
}
} else {
operator = "==";
if (tokens.matchToken("really")) {
operator = "===";
} else {
operator = "==";
}
if (tokens.matchToken("equal")) {
tokens.matchToken("to");
}
}
} else if (tokens.matchToken("equals")) {
operator = "==";
} else if (tokens.matchToken("really")) {
tokens.requireToken("equals")
operator = "===";
} else if (tokens.matchToken("exist") || tokens.matchToken("exists")) {
operator = "exist";
hasRightValue = false;
Expand Down Expand Up @@ -3821,6 +3927,11 @@
} else if (operator === "!=") {
return lhsVal != rhsVal;
}
if (operator === "===") {
return lhsVal === rhsVal;
} else if (operator === "!==") {
return lhsVal !== rhsVal;
}
if (operator === "match") {
return lhsVal != null && sloppyMatches(lhs, lhsVal, rhsVal);
}
Expand Down Expand Up @@ -4956,7 +5067,7 @@
args: [toExpr, eventName, details],
op: function (context, to, eventName, details) {
runtime.nullCheck(to, toExpr);
runtime.forEach(to, function (target) {
runtime.implicitLoop(to, function (target) {
runtime.triggerEvent(target, eventName, details, context.me);
});
return runtime.findNext(sendCmd, context);
Expand Down Expand Up @@ -5087,6 +5198,27 @@
return logCmd;
});

parser.addCommand("beep!", function (parser, runtime, tokens) {
if (!tokens.matchToken("beep!")) return;
var exprs = [parser.parseElement("expression", tokens)];
while (tokens.matchOpToken(",")) {
exprs.push(parser.requireElement("expression", tokens));
}
var beepCmd = {
exprs: exprs,
args: [exprs],
op: function (ctx, values) {
for (let i = 0; i < exprs.length; i++) {
const expr = exprs[i];
const val = values[i];
runtime.beepValueToConsole(ctx.me, expr, val);
}
return runtime.findNext(this, ctx);
},
};
return beepCmd;
});

parser.addCommand("throw", function (parser, runtime, tokens) {
if (!tokens.matchToken("throw")) return;
var expr = parser.requireElement("expression", tokens);
Expand Down Expand Up @@ -6559,7 +6691,21 @@

parser.addCommand("take", function (parser, runtime, tokens) {
if (tokens.matchToken("take")) {
var classRef = parser.requireElement("classRef", tokens);
var classRef = parser.parseElement("classRef", tokens);

var attributeRef = null;
var replacementValue = null;

if (classRef == null) {
attributeRef = parser.parseElement("attributeRef", tokens);
if (attributeRef == null) {
parser.raiseParseError(tokens, "Expected either a class reference or attribute expression");
}

if (tokens.matchToken("with")) {
replacementValue = parser.requireElement("expression", tokens);
}
}

if (tokens.matchToken("from")) {
var fromExpr = parser.requireElement("expression", tokens);
Expand All @@ -6573,25 +6719,50 @@
var forExpr = parser.requireElement("implicitMeTarget", tokens);
}

var takeCmd = {
classRef: classRef,
from: fromExpr,
forElt: forExpr,
args: [classRef, fromExpr, forExpr],
op: function (context, eltColl, from, forElt) {
runtime.nullCheck(from, fromExpr);
runtime.nullCheck(forElt, forExpr);
var clazz = eltColl.className;
runtime.implicitLoop(from, function (target) {
target.classList.remove(clazz);
});
runtime.implicitLoop(forElt, function (target) {
target.classList.add(clazz);
});
return runtime.findNext(this, context);
},
};
return takeCmd;
if (classRef) {
var takeCmd = {
classRef: classRef,
from: fromExpr,
forElt: forExpr,
args: [classRef, fromExpr, forExpr],
op: function (context, eltColl, from, forElt) {
runtime.nullCheck(from, fromExpr);
runtime.nullCheck(forElt, forExpr);
var clazz = eltColl.className;
runtime.implicitLoop(from, function (target) {
target.classList.remove(clazz);
});
runtime.implicitLoop(forElt, function (target) {
target.classList.add(clazz);
});
return runtime.findNext(this, context);
},
};
return takeCmd;
} else {
var takeCmd = {
attributeRef: attributeRef,
from: fromExpr,
forElt: forExpr,
args: [fromExpr, forExpr, replacementValue],
op: function (context, from, forElt, replacementValue) {
runtime.nullCheck(from, fromExpr);
runtime.nullCheck(forElt, forExpr);
runtime.implicitLoop(from, function (target) {
if (!replacementValue) {
target.removeAttribute(attributeRef.name);
} else {
target.setAttribute(attributeRef.name, replacementValue)
}
});
runtime.implicitLoop(forElt, function (target) {
target.setAttribute(attributeRef.name, attributeRef.value || "")
});
return runtime.findNext(this, context);
},
};
return takeCmd;
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion dist/_hyperscript.min.js

Large diffs are not rendered by default.

Binary file modified dist/_hyperscript.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripting",
"HTML"
],
"version": "0.9.8",
"version": "0.9.9",
"homepage": "https://hyperscript.org/",
"bugs": {
"url": "https://github.com/bigskysoftware/_hyperscript/issues"
Expand Down
2 changes: 1 addition & 1 deletion www/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ OK, let's get started with hyperscript!
Hyperscript is a dependency-free JavaScript library that can be included in a web page without any build step:

~~~ html
<script src="https://unpkg.com/hyperscript.org@0.9.8"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.9"></script>
~~~

After you've done this, you can begin adding hyperscript to elements:
Expand Down
2 changes: 1 addition & 1 deletion www/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ on pointerdown
</div>


<div style="flex-basis: 100%; text-align: center;"><span id="install"><strong>Install:</strong> <code style="border: 2px dotted #00000055; margin: 0 10px; padding: 4px 6px; border-radius: 4px">&lt;script src="https://unpkg.com/hyperscript.org@0.9.8"&gt;&lt;/script&gt;</code>
<div style="flex-basis: 100%; text-align: center;"><span id="install"><strong>Install:</strong> <code style="border: 2px dotted #00000055; margin: 0 10px; padding: 4px 6px; border-radius: 4px">&lt;script src="https://unpkg.com/hyperscript.org@0.9.9"&gt;&lt;/script&gt;</code>
<button style="font:inherit;font-size:.8em;background:#3465a4;color:white;border:none;padding: 0 .4em; border-radius: .4em" _="on click
writeText(my previousElementSibling's innerText) on navigator.clipboard
put 'copied!' into me
Expand Down
2 changes: 1 addition & 1 deletion www/js/_hyperscript.min.js

Large diffs are not rendered by default.

0 comments on commit 1a3a5fb

Please sign in to comment.