From 00d39b013ec79a8316ed9e7aca553f39b583e38d Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 30 Sep 2026 19:47:39 +0100 Subject: [PATCH 1/3] test: add ShEx shape regex regression tests Bug: T357109 --- README.md | 10 ++++++ tests/shex-regex.test.js | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/shex-regex.test.js diff --git a/README.md b/README.md index afda9aa..c2bc6c2 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,13 @@ Install the required PHP via composer: ```sh composer install ``` + +### Tests + +Currently there is only one node test. + +Run the ShEx shape-matching tests with Node.js: + +```sh +node --test tests/shex-regex.test.js +``` diff --git a/tests/shex-regex.test.js b/tests/shex-regex.test.js new file mode 100644 index 0000000..b4ded4c --- /dev/null +++ b/tests/shex-regex.test.js @@ -0,0 +1,68 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const test = require('node:test'); +const vm = require('node:vm'); + +const shexPage = fs.readFileSync( + path.join(__dirname, '../public_html/vue_components/shex-page.html'), + 'utf8' +); +const regexDeclaration = shexPage.match(/const regex1 = ([^;]+);/); + +assert.ok(regexDeclaration, 'ShEx shape regex declaration exists'); + +function getStartShapeBody( schemaText ) { + const startMatch = schemaText.match( /\n *start\s*=\s*@<\s*(.+?)\s*>/ ); + if ( startMatch === null ) { + return null; + } + + const startShape = startMatch[1]; + const flattenedText = schemaText + .replace( /(?\n' + + 'PREFIX wd: \n' + + 'start = @\n'; + +test( 'matches the start shape when whitespace precedes its opening brace', { + skip: 'Enable after the ShEx regex fix is applied', +}, () => { + const schema = prefixAndStart + ' {\n wdt:P1 [wd:Q1]\n}'; + const body = getStartShapeBody( schema ); + + assert.ok( body, 'the start shape should be detected' ); + assert.match( body, /wdt:P1/ ); +} ); + +test( 'continues to match compact shape syntax', () => { + const schema = prefixAndStart + '{\n wdt:P1 [wd:Q1]\n}'; + const body = getStartShapeBody( schema ); + + assert.ok( body, 'the start shape should be detected' ); + assert.match( body, /wdt:P1/ ); +} ); + +test( 'matches shapes with an EXTRA declaration', () => { + const schema = prefixAndStart + + ' EXTRA wdt:P31 {\n wdt:P1 [wd:Q1]\n}'; + const body = getStartShapeBody( schema ); + + assert.ok( body, 'the start shape should be detected' ); + assert.match( body, /wdt:P1/ ); +} ); From 00470dc4e1177783ba7d6db596cf7bc1c889e2b0 Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 30 Sep 2026 19:55:50 +0100 Subject: [PATCH 2/3] fix: recognize valid ShEx shape definitions Allow whitespace between a shape label and `{`, as permitted by ShExC, and handle EXTRA declarations with normal token spacing. Cradle previously missed these valid schemas and omitted their property fields. Bug: T357109 --- public_html/vue_components/shex-page.html | 2 +- tests/shex-regex.test.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/public_html/vue_components/shex-page.html b/public_html/vue_components/shex-page.html index 7585a5f..bd3e812 100644 --- a/public_html/vue_components/shex-page.html +++ b/public_html/vue_components/shex-page.html @@ -93,7 +93,7 @@

let s = {labels:{} , props:{}} ; let to_load = [] ; - const regex1 = RegExp('<([^/<>]+)>(?: EXTRA .*?)?\w*\{(.+?)\}','gi'); // THIS DOES NOT DEAL WITH {num} OR {num,num}!! + const regex1 = /<([^/<>]+)>(?:\s+EXTRA\s+.*?)?\s*\w*\s*\{(.+?)\}/gi; // THIS DOES NOT DEAL WITH {num} OR {num,num}!! while ( (m = regex1.exec(t)) !== null ) { if ( m[1] != start ) continue ; let parts = m[2].split(';') ; diff --git a/tests/shex-regex.test.js b/tests/shex-regex.test.js index b4ded4c..263cac1 100644 --- a/tests/shex-regex.test.js +++ b/tests/shex-regex.test.js @@ -40,9 +40,7 @@ const prefixAndStart = 'PREFIX wdt: \n' + 'PREFIX wd: \n' + 'start = @\n'; -test( 'matches the start shape when whitespace precedes its opening brace', { - skip: 'Enable after the ShEx regex fix is applied', -}, () => { +test( 'matches the start shape when whitespace precedes its opening brace', () => { const schema = prefixAndStart + ' {\n wdt:P1 [wd:Q1]\n}'; const body = getStartShapeBody( schema ); @@ -66,3 +64,12 @@ test( 'matches shapes with an EXTRA declaration', () => { assert.ok( body, 'the start shape should be detected' ); assert.match( body, /wdt:P1/ ); } ); + +test( 'allows whitespace around EXTRA and before the opening brace', () => { + const schema = prefixAndStart + + ' EXTRA wdt:P31 {\n wdt:P1 [wd:Q1]\n}'; + const body = getStartShapeBody( schema ); + + assert.ok( body, 'the start shape should be detected' ); + assert.match( body, /wdt:P1/ ); +} ); From 1b79090bbcf06b7c90f01a836cc71eaa2d58a77f Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 30 Sep 2026 20:05:14 +0100 Subject: [PATCH 3/3] fix: resolve ShEx prefixes for local properties Use declared prefix mappings to recognize properties in the local /prop/direct/ namespace, rather than requiring the literal wdt: prefix. Bug: T357109 --- README.md | 6 ++-- public_html/vue_components/shex-page.html | 35 +++++++++++++++++-- ...shex-regex.test.js => shex-parser.test.js} | 25 +++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) rename tests/{shex-regex.test.js => shex-parser.test.js} (66%) diff --git a/README.md b/README.md index c2bc6c2..da5fa54 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,8 @@ composer install ### Tests -Currently there is only one node test. - -Run the ShEx shape-matching tests with Node.js: +Run the ShEx parser tests with Node.js: ```sh -node --test tests/shex-regex.test.js +node --test tests/shex-parser.test.js ``` diff --git a/public_html/vue_components/shex-page.html b/public_html/vue_components/shex-page.html index bd3e812..bde0540 100644 --- a/public_html/vue_components/shex-page.html +++ b/public_html/vue_components/shex-page.html @@ -65,6 +65,29 @@