From 9d46db72a010f449888ebbb0b6daaad7cc27860b Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Fri, 22 Nov 2024 21:17:44 -0600 Subject: [PATCH] Fix a bug in the `prevChar` method of the PGML parser. This was an error in conversion from the Perl `substr` method to the JavaScript `substring` method. This caused emphasis to not work correctly. --- package-lock.json | 10 ++-- package.json | 2 +- src/pgml-parse.ts | 18 ++++--- test/pgml-formatting.txt | 102 ++++++++++++++++++++++----------------- 4 files changed, 74 insertions(+), 58 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ed773f..971ad56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@openwebwork/codemirror-lang-pg", - "version": "0.0.1-beta.14", + "version": "0.0.1-beta.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@openwebwork/codemirror-lang-pg", - "version": "0.0.1-beta.14", + "version": "0.0.1-beta.15", "license": "MIT", "dependencies": { "@codemirror/language": "^6.10.2", @@ -1497,9 +1497,9 @@ "license": "MIT" }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 59f9d39..88a941b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openwebwork/codemirror-lang-pg", - "version": "0.0.1-beta.14", + "version": "0.0.1-beta.15", "description": "PG language support for CodeMirror", "author": "The WeBWorK Project", "license": "MIT", diff --git a/src/pgml-parse.ts b/src/pgml-parse.ts index 0f2002c..4176c8d 100644 --- a/src/pgml-parse.ts +++ b/src/pgml-parse.ts @@ -110,17 +110,19 @@ export class PGMLParse { } nextChar(defaultNext?: string) { - if (typeof defaultNext === 'undefined') defaultNext = ''; - return (this.split[this.i] ?? this.split[this.i + 1] ?? defaultNext).substring(0, 1); + return ( + this.split[this.i] ?? + this.split[this.i + 1] ?? + (typeof defaultNext === 'undefined' ? '' : defaultNext) + ).substring(0, 1); } prevChar(defaultPrev?: string) { - if (typeof defaultPrev === 'undefined') defaultPrev = ''; - let i2 = this.i - 2; - if (i2 < 0) i2 = 0; - let i3 = this.i - 3; - if (i3 < 0) i3 = 0; - return (this.split[i2] ?? this.split[i3] ?? defaultPrev).substring(-1, 1); + return ( + this.split[this.i >= 2 ? this.i - 2 : 0] ?? + this.split[this.i >= 3 ? this.i - 3 : 0] ?? + (typeof defaultPrev === 'undefined' ? '' : defaultPrev) + ).slice(-1); } Parse(split: (string | undefined)[]) { diff --git a/test/pgml-formatting.txt b/test/pgml-formatting.txt index c36db13..8dcf0d7 100644 --- a/test/pgml-formatting.txt +++ b/test/pgml-formatting.txt @@ -1,7 +1,7 @@ # bold text BEGIN_PGML -*bold text with math [`x + 3`]* +The following is *bold text with math [`x + 3`]* END_PGML ==> @@ -9,47 +9,47 @@ END_PGML { "name": "Program", "from": 0, - "to": 51, + "to": 68, "children": [ { "name": "PGMLBlock", "from": 0, - "to": 51, + "to": 68, "children": [ { "name": "BeginPG", "from": 0, "to": 10 }, { "name": "PGMLContent", "from": 10, - "to": 43, + "to": 60, "children": [ { "name": "Paragraph", "from": 10, - "to": 43, + "to": 60, "children": [ { "name": "StrongEmphasis", - "from": 11, - "to": 42, + "from": 28, + "to": 59, "children": [ - { "name": "EmphasisMark", "from": 11, "to": 12 }, + { "name": "EmphasisMark", "from": 28, "to": 29 }, { "name": "MathMode", - "from": 32, - "to": 41, + "from": 49, + "to": 58, "children": [ - { "name": "MathModeMark", "from": 32, "to": 34 }, - { "name": "MathModeMark", "from": 39, "to": 41 } + { "name": "MathModeMark", "from": 49, "to": 51 }, + { "name": "MathModeMark", "from": 56, "to": 58 } ] }, - { "name": "EmphasisMark", "from": 41, "to": 42 } + { "name": "EmphasisMark", "from": 58, "to": 59 } ] } ] } ] }, - { "name": "EndPG", "from": 43, "to": 51 } + { "name": "EndPG", "from": 60, "to": 68 } ] } ] @@ -59,26 +59,33 @@ END_PGML from: '0' parseAll: 'true' -to: '32' +to: '49' type: 'root' stack: [ [ # 0 combine: { indent: 'indent', list: { indent: '1' }, par: 'true' } from: '0' indent: '0' - to: '32' + to: '49' type: 'indent' stack: [ [ # 0 + combine: { text: 'type' } from: '0' - to: '31' + to: '17' + type: 'text' + stack: ['The following is '] + ] + [ # 1 + from: '17' + to: '48' token: '*' type: 'bold' stack: [ [ # 0 combine: { text: 'type' } - from: '1' - to: '21' + from: '18' + to: '38' type: 'text' stack: ['bold text with math '] ] @@ -86,24 +93,24 @@ stack: [ children: [ { combine: { text: 'type' } - from: '23' - to: '28' + from: '40' + to: '45' type: 'text' stack: ['x + 3'] } ] - from: '21' + from: '38' terminator: '`]' - to: '30' + to: '47' token: '[`' type: 'math' ] ] ] - [ # 1 + [ # 2 combine: { text: 'type' } - from: '31' - to: '32' + from: '48' + to: '49' type: 'text' stack: ['\n'] ] @@ -114,7 +121,7 @@ stack: [ # italic text BEGIN_PGML -_italic text_ +The following is _italic text_ END_PGML ==> @@ -122,38 +129,38 @@ END_PGML { "name": "Program", "from": 0, - "to": 33, + "to": 50, "children": [ { "name": "PGMLBlock", "from": 0, - "to": 33, + "to": 50, "children": [ { "name": "BeginPG", "from": 0, "to": 10 }, { "name": "PGMLContent", "from": 10, - "to": 25, + "to": 42, "children": [ { "name": "Paragraph", "from": 10, - "to": 25, + "to": 42, "children": [ { "name": "Emphasis", - "from": 11, - "to": 24, + "from": 28, + "to": 41, "children": [ - { "name": "EmphasisMark", "from": 11, "to": 12 }, - { "name": "EmphasisMark", "from": 23, "to": 24 } + { "name": "EmphasisMark", "from": 28, "to": 29 }, + { "name": "EmphasisMark", "from": 40, "to": 41 } ] } ] } ] }, - { "name": "EndPG", "from": 25, "to": 33 } + { "name": "EndPG", "from": 42, "to": 50 } ] } ] @@ -163,35 +170,42 @@ END_PGML from: '0' parseAll: 'true' -to: '14' +to: '31' type: 'root' stack: [ [ # 0 combine: { indent: 'indent', list: { indent: '1' }, par: 'true' } from: '0' indent: '0' - to: '14' + to: '31' type: 'indent' stack: [ [ # 0 + combine: { text: 'type' } from: '0' - to: '13' + to: '17' + type: 'text' + stack: ['The following is '] + ] + [ # 1 + from: '17' + to: '30' token: '_' type: 'italic' stack: [ [ # 0 combine: { text: 'type' } - from: '1' - to: '12' + from: '18' + to: '29' type: 'text' stack: ['italic text'] ] ] ] - [ # 1 + [ # 2 combine: { text: 'type' } - from: '13' - to: '14' + from: '30' + to: '31' type: 'text' stack: ['\n'] ]