Skip to content

Commit de2dd0d

Browse files
authored
fix: escaped symbols in regex patterns (openscd#1266)
* fix: escaped symbols in regex patterns Signed-off-by: Stef3st <[email protected]> * chore: fixed snapshot tests Signed-off-by: Stef3st <[email protected]> --------- Signed-off-by: Stef3st <[email protected]>
1 parent a7a7081 commit de2dd0d

15 files changed

+374
-40
lines changed

src/foundation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,7 @@ const nameStartChar =
26072607
'|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]' +
26082608
'|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]';
26092609
const nameChar =
2610-
nameStartChar + '|[.0-9-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]';
2610+
nameStartChar + '|[.0-9\\-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]';
26112611
const name = nameStartChar + '(' + nameChar + ')*';
26122612
const nmToken = '(' + nameChar + ')+';
26132613

@@ -2621,9 +2621,9 @@ export const patterns = {
26212621
nmToken,
26222622
names: name + '( ' + name + ')*',
26232623
nmTokens: nmToken + '( ' + nmToken + ')*',
2624-
decimal: '[+-]?[0-9]+(([.][0-9]*)?|([.][0-9]+))',
2624+
decimal: '[+\\-]?[0-9]+(([.][0-9]*)?|([.][0-9]+))',
26252625
unsigned: '[+]?[0-9]+(([.][0-9]*)?|([.][0-9]+))',
2626-
integer: '[+-]?[0-9]+([0-9]*)',
2626+
integer: '[+\\-]?[0-9]+([0-9]*)',
26272627
alphanumericFirstUpperCase: '[A-Z][0-9,A-Z,a-z]*',
26282628
alphanumericFirstLowerCase: '[a-z][0-9,A-Z,a-z]*',
26292629
lnClass: '(LLN0)|[A-Z]{4,4}',

test/integration/editors/substation/__snapshots__/voltage-level-editor-wizarding.test.snap.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ snapshots["voltage-level-editor wizarding integration looks like the latest snap
2727
helper="[voltagelevel.wizard.nomFreqHelper]"
2828
label="nomFreq"
2929
nullable=""
30-
pattern="[+]?[0-9]+(([.][0-9]*)?|([.][0-9]+))"
3130
required=""
3231
suffix="Hz"
3332
validationmessage="[textfield.nonempty]"
@@ -49,7 +48,6 @@ snapshots["voltage-level-editor wizarding integration looks like the latest snap
4948
helper="[voltagelevel.wizard.voltageHelper]"
5049
label="Voltage"
5150
nullable=""
52-
pattern="[+-]?[0-9]+(([.][0-9]*)?|([.][0-9]+))"
5351
required=""
5452
unit="V"
5553
validationmessage="[textfield.nonempty]"

test/integration/editors/substation/voltage-level-editor-wizarding.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { MockWizard } from '../../../mock-wizard.js';
66

77
import '../../../../src/editors/substation/voltage-level-editor.js';
88
import { regexString, regExp, inverseRegExp } from '../../../foundation.js';
9+
import { patterns } from '../../../../src/foundation.js';
910

1011
describe('voltage-level-editor wizarding integration', () => {
1112
let doc: XMLDocument;
@@ -33,7 +34,28 @@ describe('voltage-level-editor wizarding integration', () => {
3334
await parent.updateComplete;
3435
});
3536
it('looks like the latest snapshot', async () => {
36-
await expect(parent.wizardUI.dialog).to.equalSnapshot();
37+
await expect(parent.wizardUI.dialog).to.equalSnapshot({
38+
ignoreAttributes: [
39+
{ tags: ['wizard-textfield'], attributes: ['pattern'] },
40+
],
41+
});
42+
});
43+
//work around, because the escapes get removed in snapshot
44+
it('should have correct pattern', async () => {
45+
expect(
46+
parent.wizardUI.dialog!.querySelectorAll('wizard-textfield[pattern]')
47+
.length
48+
).to.equal(2);
49+
expect(
50+
parent.wizardUI
51+
.dialog!.querySelectorAll('wizard-textfield[pattern]')![0]
52+
.getAttribute('pattern')
53+
).to.equal(patterns.unsigned);
54+
expect(
55+
parent.wizardUI
56+
.dialog!.querySelectorAll('wizard-textfield[pattern]')![1]
57+
.getAttribute('pattern')
58+
).to.equal(patterns.decimal);
3759
});
3860
describe('the first input element', () => {
3961
it('edits the attribute name', async () => {

test/integration/editors/templates/__snapshots__/datype-wizarding.test.snap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ snapshots["DAType wizards defines a createDATypeWizard looks like the latest sna
583583
label="id"
584584
maxlength="255"
585585
minlength="1"
586-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
587586
required=""
588587
>
589588
</wizard-textfield>
@@ -592,7 +591,6 @@ snapshots["DAType wizards defines a createDATypeWizard looks like the latest sna
592591
helper="[scl.desc]"
593592
label="desc"
594593
nullable=""
595-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
596594
>
597595
</wizard-textfield>
598596
</div>
@@ -666,7 +664,6 @@ snapshots["DAType wizards defines a dATypeWizard looks like the latest snapshot"
666664
label="id"
667665
maxlength="127"
668666
minlength="1"
669-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
670667
required=""
671668
>
672669
</wizard-textfield>
@@ -675,7 +672,6 @@ snapshots["DAType wizards defines a dATypeWizard looks like the latest snapshot"
675672
helper="[scl.desc]"
676673
label="desc"
677674
nullable=""
678-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
679675
>
680676
</wizard-textfield>
681677
<mwc-list style="margin-top: 0px;">

test/integration/editors/templates/__snapshots__/enumtype-wizarding.test.snap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,6 @@ snapshots["EnumType wizards defines a createEnumTypeWizard looks like the latest
13351335
label="id"
13361336
maxlength="127"
13371337
minlength="1"
1338-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
13391338
required=""
13401339
>
13411340
</wizard-textfield>
@@ -1344,7 +1343,6 @@ snapshots["EnumType wizards defines a createEnumTypeWizard looks like the latest
13441343
helper="[scl.desc]"
13451344
label="desc"
13461345
nullable=""
1347-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
13481346
>
13491347
</wizard-textfield>
13501348
</div>
@@ -1418,7 +1416,6 @@ snapshots["EnumType wizards defines an eNumTypeEditWizard looks like the latest
14181416
label="id"
14191417
maxlength="127"
14201418
minlength="1"
1421-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
14221419
required=""
14231420
>
14241421
</wizard-textfield>
@@ -1427,7 +1424,6 @@ snapshots["EnumType wizards defines an eNumTypeEditWizard looks like the latest
14271424
helper="[scl.desc]"
14281425
label="desc"
14291426
nullable=""
1430-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
14311427
>
14321428
</wizard-textfield>
14331429
<mwc-list style="margin-top: 0px;">

test/integration/editors/templates/__snapshots__/lnodetype-wizard.test.snap.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ snapshots["LNodeType wizards defines a lNodeTypeHelperWizard looks like the late
5353
label="id"
5454
maxlength="127"
5555
minlength="1"
56-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
5756
required=""
5857
>
5958
</wizard-textfield>
@@ -62,13 +61,11 @@ snapshots["LNodeType wizards defines a lNodeTypeHelperWizard looks like the late
6261
helper="[scl.desc]"
6362
label="desc"
6463
nullable=""
65-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
6664
>
6765
</wizard-textfield>
6866
<wizard-textfield
6967
helper="[scl.lnClass]"
7068
label="lnClass"
71-
pattern="(LLN0)|[A-Z]{4,4}"
7269
required=""
7370
>
7471
</wizard-textfield>
@@ -4319,7 +4316,6 @@ snapshots["LNodeType wizards defines a createLNodeTypeWizard looks like the late
43194316
label="id"
43204317
maxlength="127"
43214318
minlength="1"
4322-
pattern="([:_A-Za-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[.0-9-]|·|[̀-ͯ]|[‿-⁀])+"
43234319
required=""
43244320
>
43254321
</wizard-textfield>
@@ -4328,7 +4324,6 @@ snapshots["LNodeType wizards defines a createLNodeTypeWizard looks like the late
43284324
helper="[scl.desc]"
43294325
label="desc"
43304326
nullable=""
4331-
pattern="([ -~]|[…]|[ -퟿]|[-�])*"
43324327
>
43334328
</wizard-textfield>
43344329
</div>

test/integration/editors/templates/datype-wizarding.test.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Select } from '@material/mwc-select';
99
import { FilteredList } from '../../../../src/filtered-list.js';
1010
import { WizardTextField } from '../../../../src/wizard-textfield.js';
1111
import TemplatesPlugin from '../../../../src/editors/Templates.js';
12+
import { patterns } from '../../../../src/foundation.js';
1213

1314
describe('DAType wizards', () => {
1415
if (customElements.get('templates-editor') === undefined)
@@ -64,7 +65,38 @@ describe('DAType wizards', () => {
6465
});
6566

6667
it('looks like the latest snapshot', async () => {
67-
await expect(parent.wizardUI.dialog).to.equalSnapshot();
68+
await expect(parent.wizardUI.dialog).to.equalSnapshot({
69+
ignoreAttributes: [
70+
{
71+
tags: ['wizard-textfield'],
72+
attributes: ['pattern'],
73+
},
74+
],
75+
});
76+
});
77+
78+
//work around, because the escapes get removed in snapshot
79+
it('should have correct pattern', async () => {
80+
const pattern =
81+
'([:_A-Za-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]' +
82+
'|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]' +
83+
'|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[.0-9\\-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040])+';
84+
expect(
85+
parent.wizardUI.dialog!.querySelectorAll('wizard-textfield[pattern]')!
86+
.length
87+
).to.equal(2);
88+
89+
expect(
90+
parent.wizardUI
91+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[0]
92+
.getAttribute('pattern')
93+
).to.equal(pattern);
94+
95+
expect(
96+
parent.wizardUI
97+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[1]
98+
.getAttribute('pattern')
99+
).to.equal(patterns.normalizedString);
68100
});
69101

70102
it('allows to add empty DATypes to the project', async () => {
@@ -140,7 +172,38 @@ describe('DAType wizards', () => {
140172
});
141173

142174
it('looks like the latest snapshot', async () => {
143-
await expect(parent.wizardUI.dialog).to.equalSnapshot();
175+
await expect(parent.wizardUI.dialog).to.equalSnapshot({
176+
ignoreAttributes: [
177+
{
178+
tags: ['wizard-textfield'],
179+
attributes: ['pattern'],
180+
},
181+
],
182+
});
183+
});
184+
185+
//work around, because the escapes get removed in snapshot
186+
it('should have correct pattern', async () => {
187+
const pattern =
188+
'([:_A-Za-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]' +
189+
'|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]' +
190+
'|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[.0-9\\-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040])+';
191+
expect(
192+
parent.wizardUI.dialog!.querySelectorAll('wizard-textfield[pattern]')!
193+
.length
194+
).to.equal(2);
195+
196+
expect(
197+
parent.wizardUI
198+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[0]
199+
.getAttribute('pattern')
200+
).to.equal(pattern);
201+
202+
expect(
203+
parent.wizardUI
204+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[1]
205+
.getAttribute('pattern')
206+
).to.equal(patterns.normalizedString);
144207
});
145208
it('edits DAType attributes id', async () => {
146209
expect(doc.querySelector('DAType[id="Dummy.LLN0.Mod.SBOw"]')).to.exist;

test/integration/editors/templates/enumtype-wizarding.test.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Select } from '@material/mwc-select';
99
import { FilteredList } from '../../../../src/filtered-list.js';
1010
import TemplatesPlugin from '../../../../src/editors/Templates.js';
1111
import { WizardTextField } from '../../../../src/wizard-textfield.js';
12+
import { patterns } from '../../../../src/foundation.js';
1213

1314
describe('EnumType wizards', () => {
1415
if (customElements.get('templates-editor') === undefined)
@@ -64,7 +65,38 @@ describe('EnumType wizards', () => {
6465
});
6566

6667
it('looks like the latest snapshot', async () => {
67-
await expect(parent.wizardUI.dialog).to.equalSnapshot();
68+
await expect(parent.wizardUI.dialog).to.equalSnapshot({
69+
ignoreAttributes: [
70+
{
71+
tags: ['wizard-textfield'],
72+
attributes: ['pattern'],
73+
},
74+
],
75+
});
76+
});
77+
78+
//work around, because the escapes get removed in snapshot
79+
it('should have correct pattern', async () => {
80+
const pattern =
81+
'([:_A-Za-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]' +
82+
'|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]' +
83+
'|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[.0-9\\-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040])+';
84+
expect(
85+
parent.wizardUI.dialog!.querySelectorAll('wizard-textfield[pattern]')!
86+
.length
87+
).to.equal(2);
88+
89+
expect(
90+
parent.wizardUI
91+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[0]
92+
.getAttribute('pattern')
93+
).to.equal(pattern);
94+
95+
expect(
96+
parent.wizardUI
97+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[1]
98+
.getAttribute('pattern')
99+
).to.equal(patterns.normalizedString);
68100
});
69101

70102
it('allows to add empty EnumType to the project', async () => {
@@ -127,7 +159,38 @@ describe('EnumType wizards', () => {
127159
});
128160

129161
it('looks like the latest snapshot', async () => {
130-
await expect(parent.wizardUI.dialog).to.equalSnapshot();
162+
await expect(parent.wizardUI.dialog).to.equalSnapshot({
163+
ignoreAttributes: [
164+
{
165+
tags: ['wizard-textfield'],
166+
attributes: ['pattern'],
167+
},
168+
],
169+
});
170+
});
171+
172+
//work around, because the escapes get removed in snapshot
173+
it('should have correct pattern', async () => {
174+
const pattern =
175+
'([:_A-Za-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]' +
176+
'|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]' +
177+
'|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[.0-9\\-]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040])+';
178+
expect(
179+
parent.wizardUI.dialog!.querySelectorAll('wizard-textfield[pattern]')!
180+
.length
181+
).to.equal(2);
182+
183+
expect(
184+
parent.wizardUI
185+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[0]
186+
.getAttribute('pattern')
187+
).to.equal(pattern);
188+
189+
expect(
190+
parent.wizardUI
191+
.dialog!.querySelectorAll('wizard-textfield[pattern]')[1]
192+
.getAttribute('pattern')
193+
).to.equal(patterns.normalizedString);
131194
});
132195

133196
it('edits EnumType attributes id', async () => {

0 commit comments

Comments
 (0)