Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested regex and strings in expressions #508

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Datastar helps you build reactive web applications with the simplicity of server-side rendering and the power of a full-stack SPA framework.

Getting started is as easy as adding a single 12.9 KiB script tag to your HTML.
Getting started is as easy as adding a single 13.0 KiB script tag to your HTML.

```html
<script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/[email protected]/bundles/datastar.js"></script>
Expand Down
10 changes: 5 additions & 5 deletions bundles/datastar-core.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bundles/datastar-core.js.map

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions bundles/datastar.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bundles/datastar.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Datastar helps you build reactive web applications with the simplicity of server-side rendering and the power of a full-stack SPA framework.

Getting started is as easy as adding a single 12.9 KiB script tag to your HTML.
Getting started is as easy as adding a single 13.0 KiB script tag to your HTML.

```html
<script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/[email protected]/bundles/datastar.js"></script>
Expand Down
25 changes: 19 additions & 6 deletions library/src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,29 @@ export class Engine {
ctx: RuntimeContext,
...argNames: string[]
): RuntimeExpressionFunction {
const stmts = ctx.value
.split(/;|\n/)
.map((s) => s.trim())
.filter((s) => s !== '')
// This regex allows Datastar expressions to support nested
// regex and strings that contain ; and/or \n without breaking.
//
// Each of these regex defines a block type we want to match
// (importantly we ignore the content within these blocks):
//
// regex \/(\\\/|[^\/])*\/
// double quotes "(\\"|[^\"])*"
// single quotes '(\\'|[^'])*'
// ticks `(\\`|[^`])*`
//
// We also want to match the non delimiter part of statements:
//
// [^;\n]
//
const statementRe = /(\/(\\\/|[^\/])*\/|"(\\"|[^\"])*"|'(\\'|[^'])*'|`(\\`|[^`])*`|[^;\n])+/gm
const stmts = ctx.value.trim().match(statementRe)
const lastIdx = stmts.length - 1
const last = stmts[lastIdx]
if (!last.startsWith('return')) {
stmts[lastIdx] = `return (${stmts[lastIdx]});`
stmts[lastIdx] = `return (${last});`
}
let userExpression = stmts.join(';\n').trim()
let userExpression = stmts.join('\n')

// Ingore any escaped values
const escaped = new Map<string, string>()
Expand Down
4 changes: 2 additions & 2 deletions sdk/go/consts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading