@barefootjs/blade 0.18.4 → 0.18.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/build.js CHANGED
@@ -187308,7 +187308,13 @@ import {
187308
187308
  prepareLoweringMatchers,
187309
187309
  queryHrefArgs,
187310
187310
  sortComparatorFromArrow as sortComparatorFromArrow2,
187311
- isValidHelperId
187311
+ isValidHelperId,
187312
+ isDangerousInnerHtmlAttr,
187313
+ resolveDangerousInnerHtml,
187314
+ dangerousInnerHtmlMetacharViolation,
187315
+ dangerousInnerHtmlDiagnostic,
187316
+ resolveStaticLoopSource,
187317
+ collectLoopBoundNames as collectLoopBoundNames2
187312
187318
  } from "@barefootjs/jsx";
187313
187319
 
187314
187320
  // src/adapter/boolean-result.ts
@@ -187369,7 +187375,7 @@ function isAriaBooleanAttr(name) {
187369
187375
  }
187370
187376
 
187371
187377
  // src/adapter/blade-adapter.ts
187372
- import { BF_SLOT, BF_COND, BF_REGION } from "@barefootjs/shared";
187378
+ import { BF_SLOT, BF_COND, BF_REGION, escapeHtml } from "@barefootjs/shared";
187373
187379
 
187374
187380
  // src/adapter/lib/constants.ts
187375
187381
  var BLADE_TEMPLATE_PRIMITIVES = {
@@ -187378,7 +187384,10 @@ var BLADE_TEMPLATE_PRIMITIVES = {
187378
187384
  Number: { arity: 1, emit: (args) => `$bf->number(${args[0]})` },
187379
187385
  "Math.floor": { arity: 1, emit: (args) => `$bf->floor(${args[0]})` },
187380
187386
  "Math.ceil": { arity: 1, emit: (args) => `$bf->ceil(${args[0]})` },
187381
- "Math.round": { arity: 1, emit: (args) => `$bf->round(${args[0]})` }
187387
+ "Math.round": { arity: 1, emit: (args) => `$bf->round(${args[0]})` },
187388
+ "Math.min": { arity: 2, emit: (args) => `$bf->min(${args[0]}, ${args[1]})` },
187389
+ "Math.max": { arity: 2, emit: (args) => `$bf->max(${args[0]}, ${args[1]})` },
187390
+ "Math.abs": { arity: 1, emit: (args) => `$bf->abs(${args[0]})` }
187382
187391
  };
187383
187392
  var BLADE_PRIMITIVE_EMIT_MAP = Object.fromEntries(Object.entries(BLADE_TEMPLATE_PRIMITIVES).map(([k, v]) => [k, v.emit]));
187384
187393
 
@@ -187512,6 +187521,12 @@ function renderArrayMethod(method, object, args, emit) {
187512
187521
  const recv = emit(object);
187513
187522
  return `$bf->trim(${recv})`;
187514
187523
  }
187524
+ case "trimStart":
187525
+ case "trimEnd": {
187526
+ const fn = method === "trimStart" ? "trim_start" : "trim_end";
187527
+ const recv = emit(object);
187528
+ return `$bf->${fn}(${recv})`;
187529
+ }
187515
187530
  case "toFixed": {
187516
187531
  const recv = emit(object);
187517
187532
  const digits = args.length >= 1 ? emit(args[0]) : "0";
@@ -187545,6 +187560,12 @@ function renderArrayMethod(method, object, args, emit) {
187545
187560
  const newS = emit(args[1]);
187546
187561
  return `$bf->replace(${recv}, ${oldS}, ${newS})`;
187547
187562
  }
187563
+ case "replaceAll": {
187564
+ const recv = emit(object);
187565
+ const oldS = emit(args[0]);
187566
+ const newS = emit(args[1]);
187567
+ return `$bf->replace_all(${recv}, ${oldS}, ${newS})`;
187568
+ }
187548
187569
  case "repeat": {
187549
187570
  const recv = emit(object);
187550
187571
  const count = args.length === 0 ? "0" : emit(args[0]);
@@ -187639,6 +187660,39 @@ function renderFlatMethod(recv, depth, emit) {
187639
187660
  return `$bf->flat(${recv}, ${d})`;
187640
187661
  }
187641
187662
 
187663
+ // src/adapter/lib/static-value.ts
187664
+ function staticValueToBlade(value) {
187665
+ if (value === null || value === undefined)
187666
+ return "null";
187667
+ if (typeof value === "boolean")
187668
+ return value ? "true" : "false";
187669
+ if (typeof value === "number")
187670
+ return String(value);
187671
+ if (typeof value === "string")
187672
+ return `'${escapeBladeSingleQuoted(value)}'`;
187673
+ if (Array.isArray(value)) {
187674
+ const items = [];
187675
+ for (const el of value) {
187676
+ const serialized = staticValueToBlade(el);
187677
+ if (serialized === null)
187678
+ return null;
187679
+ items.push(serialized);
187680
+ }
187681
+ return `[${items.join(", ")}]`;
187682
+ }
187683
+ if (typeof value === "object") {
187684
+ const entries = [];
187685
+ for (const [key, val] of Object.entries(value)) {
187686
+ const serialized = staticValueToBlade(val);
187687
+ if (serialized === null)
187688
+ return null;
187689
+ entries.push(`${bladeHashKey(key)} => ${serialized}`);
187690
+ }
187691
+ return `[${entries.join(", ")}]`;
187692
+ }
187693
+ return null;
187694
+ }
187695
+
187642
187696
  // src/adapter/expr/emitters.ts
187643
187697
  import {
187644
187698
  groupBinaryOperand,
@@ -187688,7 +187742,7 @@ class BladeFilterEmitter {
187688
187742
  return "null";
187689
187743
  return String(value);
187690
187744
  }
187691
- member(object, property, _computed, emit) {
187745
+ member(object, property, _computed, _optional, emit) {
187692
187746
  if (property === "length") {
187693
187747
  return `$bf->length(${emit(object)})`;
187694
187748
  }
@@ -187799,7 +187853,7 @@ class BladeTopLevelEmitter {
187799
187853
  return "null";
187800
187854
  return String(value);
187801
187855
  }
187802
- member(object, property, _computed, emit) {
187856
+ member(object, property, _computed, _optional, emit) {
187803
187857
  if (object.kind === "identifier" && object.name === "props") {
187804
187858
  return bladeVar(property);
187805
187859
  }
@@ -188178,6 +188232,9 @@ function generateDerivedMemoSeed(ctx, ir) {
188178
188232
  ` : "";
188179
188233
  }
188180
188234
 
188235
+ // src/adapter/props/prop-classes.ts
188236
+ import { collectLoopBoundNames } from "@barefootjs/jsx";
188237
+
188181
188238
  // src/adapter/value/parsed-literal.ts
188182
188239
  import { evalStringArrayJoin } from "@barefootjs/jsx";
188183
188240
  function isStringTypeInfo(type2) {
@@ -188208,6 +188265,12 @@ function collectStringValueNames(ir) {
188208
188265
  if (isStringTypeInfo(p.type))
188209
188266
  names.add(p.name);
188210
188267
  }
188268
+ for (const c of ir.metadata.localConstants) {
188269
+ if (isStringTypeInfo(c.type ?? undefined) || isBareStringLiteral(c.value))
188270
+ names.add(c.name);
188271
+ }
188272
+ for (const bound of collectLoopBoundNames(ir))
188273
+ names.delete(bound);
188211
188274
  return names;
188212
188275
  }
188213
188276
 
@@ -188223,6 +188286,7 @@ class BladeAdapter extends BaseAdapter {
188223
188286
  options;
188224
188287
  errors = [];
188225
188288
  inLoop = false;
188289
+ currentLoopKeyDepth = 0;
188226
188290
  propsObjectName = null;
188227
188291
  propsParams = [];
188228
188292
  booleanTypedProps = new Set;
@@ -188231,6 +188295,7 @@ class BladeAdapter extends BaseAdapter {
188231
188295
  _searchParamsLocals = new Set;
188232
188296
  _loweringMatchers = [];
188233
188297
  localConstants = [];
188298
+ staticLoopSourceBoundNames = new Set;
188234
188299
  nullableOptionalProps = new Set;
188235
188300
  constructor(options = {}) {
188236
188301
  super();
@@ -188246,6 +188311,7 @@ class BladeAdapter extends BaseAdapter {
188246
188311
  this.propsParams = ir.metadata.propsParams.map((p) => ({ name: p.name }));
188247
188312
  this.booleanTypedProps = collectBooleanTypedProps(ir);
188248
188313
  this.localConstants = ir.metadata.localConstants ?? [];
188314
+ this.staticLoopSourceBoundNames = collectLoopBoundNames2(ir);
188249
188315
  this.nullableOptionalProps = collectNullableOptionalProps(ir);
188250
188316
  this.stringValueNames = collectStringValueNames(ir);
188251
188317
  this.moduleStringConsts = collectModuleStringConsts(ir.metadata.localConstants);
@@ -188299,7 +188365,7 @@ class BladeAdapter extends BaseAdapter {
188299
188365
  return this.renderElement(node);
188300
188366
  }
188301
188367
  emitText(node) {
188302
- return node.value;
188368
+ return escapeHtml(node.value);
188303
188369
  }
188304
188370
  emitExpression(node) {
188305
188371
  return this.renderExpression(node);
@@ -188367,7 +188433,8 @@ class BladeAdapter extends BaseAdapter {
188367
188433
  renderElement(element) {
188368
188434
  const tag = element.tag;
188369
188435
  const attrs = this.renderAttributes(element);
188370
- const children = this.renderChildren(element.children);
188436
+ const dangerousHtml = this.renderDangerousInnerHtml(element);
188437
+ const children = dangerousHtml !== null ? dangerousHtml : this.renderChildren(element.children);
188371
188438
  let hydrationAttrs = "";
188372
188439
  if (element.needsScope) {
188373
188440
  hydrationAttrs += ` ${this.renderScopeMarker("")}`;
@@ -188402,6 +188469,22 @@ class BladeAdapter extends BaseAdapter {
188402
188469
  }
188403
188470
  return `<${tag}${attrs}${hydrationAttrs}>${children}</${tag}>`;
188404
188471
  }
188472
+ renderDangerousInnerHtml(element) {
188473
+ const resolution = resolveDangerousInnerHtml(element);
188474
+ if (!resolution)
188475
+ return null;
188476
+ if (resolution.kind === "dynamic") {
188477
+ this.errors.push(dangerousInnerHtmlDiagnostic(resolution.expr, resolution.loc));
188478
+ return "";
188479
+ }
188480
+ const violation = dangerousInnerHtmlMetacharViolation(resolution.html, this.name);
188481
+ if (violation) {
188482
+ const attr = element.attrs.find(isDangerousInnerHtmlAttr);
188483
+ this.errors.push(dangerousInnerHtmlDiagnostic(`{ __html: ${JSON.stringify(resolution.html)} }`, attr.loc, violation));
188484
+ return "";
188485
+ }
188486
+ return resolution.html;
188487
+ }
188405
188488
  renderExpression(expr) {
188406
188489
  if (expr.clientOnly) {
188407
188490
  if (expr.slotId) {
@@ -188409,7 +188492,7 @@ class BladeAdapter extends BaseAdapter {
188409
188492
  }
188410
188493
  return "";
188411
188494
  }
188412
- const bladeExpr = `$bf->string(${this.convertExpressionToBlade(expr.expr)})`;
188495
+ const bladeExpr = `$bf->string(${this.convertExpressionToBlade(expr.expr, expr.parsed)})`;
188413
188496
  if (expr.slotId) {
188414
188497
  return `{!! $bf->text_start("${expr.slotId}") !!}{!! e(${bladeExpr}) !!}{!! $bf->text_end() !!}`;
188415
188498
  }
@@ -188500,8 +188583,12 @@ ${whenTrue}
188500
188583
  }
188501
188584
  });
188502
188585
  }
188586
+ const staticItems = resolveStaticLoopSource(loop.arrayParsed, this.localConstants, {
188587
+ isNameShadowed: (name) => this.staticLoopSourceBoundNames.has(name)
188588
+ });
188589
+ const staticArray = staticItems !== null ? staticValueToBlade(staticItems) : null;
188503
188590
  const arrayName = loop.array.trim();
188504
- if (/^[A-Za-z_$][\w$]*$/.test(arrayName)) {
188591
+ if (staticArray === null && /^[A-Za-z_$][\w$]*$/.test(arrayName)) {
188505
188592
  const arrayConst = (this.localConstants ?? []).find((c) => c.name === arrayName);
188506
188593
  if (arrayConst && !arrayConst.isModule && this._resolveLiteralConst(arrayName) === null) {
188507
188594
  this.errors.push({
@@ -188515,7 +188602,7 @@ ${whenTrue}
188515
188602
  });
188516
188603
  }
188517
188604
  }
188518
- const rawArray = this.convertExpressionToBlade(loop.array);
188605
+ const rawArray = staticArray ?? this.convertExpressionToBlade(loop.array);
188519
188606
  let array = rawArray;
188520
188607
  if (loop.sortComparator) {
188521
188608
  const sort = loop.sortComparator;
@@ -188529,7 +188616,7 @@ ${whenTrue}
188529
188616
  const renderedChildren = this.renderChildren(loop.children);
188530
188617
  const loopVar = loop.iterationShape === "keys" ? "__bf_item" : supportableDestructure ? "__bf_item" : param;
188531
188618
  const indexLocalLines = [];
188532
- if (loop.iterationShape === "keys") {
188619
+ if (loop.objectIteration) {} else if (loop.iterationShape === "keys") {
188533
188620
  indexLocalLines.push(`@php(${bladeVar(param)} = $loop->index)`);
188534
188621
  } else if (loop.index) {
188535
188622
  indexLocalLines.push(`@php(${bladeVar(loop.index)} = $loop->index)`);
@@ -188550,13 +188637,17 @@ ${whenTrue}
188550
188637
  }
188551
188638
  const prevInLoop = this.inLoop;
188552
188639
  this.inLoop = true;
188640
+ const prevLoopKeyDepth = this.currentLoopKeyDepth;
188641
+ this.currentLoopKeyDepth = loop.depth;
188553
188642
  const childrenUnderLoop = this.renderChildren(loop.children);
188643
+ this.currentLoopKeyDepth = prevLoopKeyDepth;
188554
188644
  this.inLoop = prevInLoop;
188555
188645
  const bodyChildren = loop.bodyIsItemConditional && loop.key ? `{!! $bf->comment('loop-i:' . $bf->string(${this.convertExpressionToBlade(loop.key)})) !!}
188556
188646
  ${childrenUnderLoop}` : childrenUnderLoop;
188557
188647
  const lines = [];
188558
188648
  lines.push(`{!! $bf->comment("loop:${loop.markerId}") !!}`);
188559
- lines.push(`@foreach((${array} ?? []) as ${bladeVar(loopVar)})`);
188649
+ const forHeader = loop.objectIteration === "entries" ? `@foreach($bf->entries(${array} ?? []) as ${bladeVar(loop.index ?? param)} => ${bladeVar(param)})` : loop.objectIteration === "keys" ? `@foreach($bf->keys(${array} ?? []) as ${bladeVar(param)})` : loop.objectIteration === "values" ? `@foreach($bf->values(${array} ?? []) as ${bladeVar(param)})` : `@foreach((${array} ?? []) as ${bladeVar(loopVar)})`;
188650
+ lines.push(forHeader);
188560
188651
  for (const il of indexLocalLines)
188561
188652
  lines.push(il);
188562
188653
  if (loop.filterPredicate) {
@@ -188626,9 +188717,22 @@ ${childrenUnderLoop}` : childrenUnderLoop;
188626
188717
  renderComponent(comp) {
188627
188718
  const segments = [{ kind: "entries", parts: [] }];
188628
188719
  const currentEntries = () => this.componentPropSegmentEntries(segments);
188720
+ const namedSlotCaptures = [];
188629
188721
  for (const p of comp.props) {
188630
188722
  if ((p.name.match(/^on[A-Z]/) || p.name === "ref") && p.value.kind === "expression")
188631
188723
  continue;
188724
+ if (p.value.kind === "jsx-children" && p.name !== "children") {
188725
+ const prevInLoop = this.inLoop;
188726
+ this.inLoop = false;
188727
+ const slotBody = this.renderChildren(p.value.children);
188728
+ this.inLoop = prevInLoop;
188729
+ const captureVar = bladeVar(`bf_prop_${this.childrenCaptureCounter++}`);
188730
+ namedSlotCaptures.push(`@php(ob_start())
188731
+ ${slotBody}
188732
+ ` + `@php(${captureVar} = $bf->backend->mark_raw(preg_replace('/\\n\\z/', '', ob_get_clean(), 1)))`);
188733
+ currentEntries().push(`${bladeHashKey(p.name)} => ${captureVar}`);
188734
+ continue;
188735
+ }
188632
188736
  if (p.value.kind === "spread") {
188633
188737
  const trimmed = p.value.expr.trim();
188634
188738
  if (this.propsObjectName && this.propsObjectName === trimmed) {
@@ -188658,13 +188762,13 @@ ${childrenUnderLoop}` : childrenUnderLoop;
188658
188762
  const captureVar = bladeVar(`bf_children_${comp.slotId ?? "c" + this.childrenCaptureCounter++}`);
188659
188763
  currentEntries().push(`${bladeHashKey("children")} => ${captureVar}`);
188660
188764
  const dict = this.combineComponentPropSegments(segments);
188661
- return `@php(ob_start())
188765
+ return namedSlotCaptures.join("") + `@php(ob_start())
188662
188766
  ${childrenBody}
188663
188767
  ` + `@php(${captureVar} = $bf->backend->mark_raw(preg_replace('/\\n\\z/', '', ob_get_clean(), 1)))` + `{!! $bf->render_child('${tplName}', ${dict}) !!}`;
188664
188768
  }
188665
188769
  const isEmpty = segments.every((s) => s.kind === "entries" && s.parts.length === 0);
188666
188770
  const dictEntries = isEmpty ? "" : `, ${this.combineComponentPropSegments(segments)}`;
188667
- return `{!! $bf->render_child('${tplName}'${dictEntries}) !!}`;
188771
+ return `${namedSlotCaptures.join("")}{!! $bf->render_child('${tplName}'${dictEntries}) !!}`;
188668
188772
  }
188669
188773
  childrenCaptureCounter = 0;
188670
188774
  presenceVarCounter = 0;
@@ -188711,7 +188815,7 @@ ${fallback}
188711
188815
  ${children}`;
188712
188816
  }
188713
188817
  elementAttrEmitter = {
188714
- emitLiteral: (value, name) => `${name}="${value.value}"`,
188818
+ emitLiteral: (value, name) => `${name}="${escapeHtml(value.value)}"`,
188715
188819
  emitExpression: (value, name) => {
188716
188820
  if (name === "style") {
188717
188821
  const css = this.tryLowerStyleObject(value.expr);
@@ -188817,12 +188921,15 @@ ${name}="{!! e($bf->string(${val})) !!}"
188817
188921
  for (const attr of element.attrs) {
188818
188922
  if (attr.clientOnly)
188819
188923
  continue;
188924
+ if (isDangerousInnerHtmlAttr(attr))
188925
+ continue;
188820
188926
  let attrName;
188821
188927
  if (attr.name === "className")
188822
188928
  attrName = "class";
188823
- else if (attr.name === "key")
188824
- attrName = "data-key";
188825
- else
188929
+ else if (attr.name === "key") {
188930
+ const depth = this.currentLoopKeyDepth;
188931
+ attrName = depth > 0 ? `data-key-${depth}` : "data-key";
188932
+ } else
188826
188933
  attrName = attr.name;
188827
188934
  const lowered = emitAttrValue(attr.value, this.elementAttrEmitter, attrName);
188828
188935
  if (lowered)
@@ -189016,6 +189123,8 @@ Options:
189016
189123
  return isBooleanResultExpr(expr) || isAriaBooleanAttr(name) || this.isBooleanTypedPropRef(expr);
189017
189124
  }
189018
189125
  _resolveLiteralConst(name) {
189126
+ if (this.staticLoopSourceBoundNames.has(name))
189127
+ return null;
189019
189128
  const c = (this.localConstants ?? []).find((lc) => lc.name === name);
189020
189129
  if (c?.value === undefined)
189021
189130
  return null;
@@ -189028,6 +189137,8 @@ Options:
189028
189137
  return null;
189029
189138
  }
189030
189139
  _resolveStaticRecordLiteral(objectName, key) {
189140
+ if (this.staticLoopSourceBoundNames.has(objectName))
189141
+ return null;
189031
189142
  const hit = lookupStaticRecordLiteral(objectName, key, this.localConstants);
189032
189143
  if (!hit)
189033
189144
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"conformance-pins.d.ts","sourceRoot":"","sources":["../src/conformance-pins.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,eAAO,MAAM,eAAe,EAAE,eAoF7B,CAAA"}
1
+ {"version":3,"file":"conformance-pins.d.ts","sourceRoot":"","sources":["../src/conformance-pins.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,eAAO,MAAM,eAAe,EAAE,eA4E7B,CAAA"}