Skip to content

Commit

Permalink
<xsl:key> (#101)
Browse files Browse the repository at this point in the history
* `<xsl:key>` basic implementation.
* - Implementing `<xsl:key>`
* - Refactorings and cleanup
* Updating Node version in PR build.
  • Loading branch information
leonelsanchesdasilva committed Sep 19, 2024
1 parent 3916d7e commit 6245c11
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
with:
node-version: '16'
node-version: '20'
- uses: ArtiomTr/jest-coverage-report-action@v2
id: coverage
with:
Expand Down
10 changes: 8 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"--runInBand",
"--testTimeout=100000000"
],
"smartStep": true,
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"smartStep": true,
"skipFiles": [
"<node_internals>/**",
"node_modules/**"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
Expand All @@ -31,6 +34,9 @@
"args": [
"${file}"
],
"env": {
"TS_NODE_PROJECT": "${workspaceRoot}/tsconfig.debug.json"
},
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Expand Down
7 changes: 5 additions & 2 deletions src/xpath/expr-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class ExprContext {
xsltVersion: '1.0' | '2.0' | '3.0';

variables: { [name: string]: NodeValue };
keys: { [name: string]: { [key: string]: NodeValue } };
knownNamespaces: { [alias: string]: string };

caseInsensitive: any;
Expand Down Expand Up @@ -110,6 +111,7 @@ export class ExprContext {
this.outputPosition = opt_outputPosition || 0;

this.variables = opt_variables || {};
this.keys = opt_parent?.keys || {};
this.knownNamespaces = opt_knownNamespaces || {};

this.parent = opt_parent || null;
Expand Down Expand Up @@ -196,7 +198,7 @@ export class ExprContext {
);
}

setVariable(name?: any, value?: any) {
setVariable(name?: string, value?: NodeValue | string) {
if (
value instanceof StringValue ||
value instanceof BooleanValue ||
Expand All @@ -206,11 +208,12 @@ export class ExprContext {
this.variables[name] = value;
return;
}

if ('true' === value) {
this.variables[name] = new BooleanValue(true);
} else if ('false' === value) {
this.variables[name] = new BooleanValue(false);
} else if (TOK_NUMBER.re.test(value)) {
} else if (TOK_NUMBER.re.test(String(value))) {
this.variables[name] = new NumberValue(value);
} else {
// DGF What if it's null?
Expand Down
4 changes: 3 additions & 1 deletion src/xpath/expressions/function-call-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
ceiling,
round,
current,
formatNumber
formatNumber,
key
} from '../functions';
import { extCardinal, extIf, extJoin } from '../functions/non-standard';
import { lowerCase, _replace, upperCase } from '../functions/standard-20';
Expand All @@ -56,6 +57,7 @@ export class FunctionCallExpr extends Expression {
floor,
'generate-id': generateId,
id,
key,
lang,
last,
'local-name': localName,
Expand Down
6 changes: 3 additions & 3 deletions src/xpath/expressions/location-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class LocationExpr extends Expression {
this.xPath = xPath;
}

appendStep(s) {
appendStep(s: StepExpr) {
const combinedStep = this._combineSteps(this.steps[this.steps.length - 1], s);
if (combinedStep) {
this.steps[this.steps.length - 1] = combinedStep;
Expand All @@ -28,7 +28,7 @@ export class LocationExpr extends Expression {
}
}

prependStep(s) {
prependStep(s: StepExpr) {
const combinedStep = this._combineSteps(s, this.steps[0]);
if (combinedStep) {
this.steps[0] = combinedStep;
Expand All @@ -38,7 +38,7 @@ export class LocationExpr extends Expression {
}

// DGF try to combine two steps into one step (perf enhancement)
_combineSteps(prevStep, nextStep) {
private _combineSteps(prevStep: any, nextStep: any) {
if (!prevStep) return null;
if (!nextStep) return null;
const hasPredicates = prevStep.predicates && prevStep.predicates.length > 0;
Expand Down
2 changes: 2 additions & 0 deletions src/xpath/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './non-standard';
export * from './standard';
export * from './standard-20';
export * from './xslt-specific';
14 changes: 14 additions & 0 deletions src/xpath/functions/xslt-specific.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ExprContext } from "../expr-context";
import { Expression } from "../expressions/expression";
import { NodeValue, StringValue } from "../values";
import { assert } from "./internal-functions";

export function key(context: ExprContext): NodeValue {
assert(this.args.length === 2);
const keyNameStringValue: StringValue = (this.args[0] as Expression).evaluate(context);
const keyValueStringValue: StringValue = (this.args[1] as Expression).evaluate(context);
const keyName = keyNameStringValue.stringValue();
const keyValue = keyValueStringValue.stringValue();
const nodeSet = context.keys[keyName][keyValue];
return nodeSet;
}
2 changes: 1 addition & 1 deletion src/xpath/values/node-set-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class NodeSetValue implements NodeValue {
value: XNode[];
type: string;

constructor(value: any) {
constructor(value: XNode[]) {
this.value = value;
this.type = 'node-set';
}
Expand Down
Loading

0 comments on commit 6245c11

Please sign in to comment.