From 83b9a1727788e41b4819ee3e9124c442794e40f1 Mon Sep 17 00:00:00 2001 From: darshanr0107 Date: Thu, 12 Jun 2025 18:13:11 +0530 Subject: [PATCH] fix: prevent pie chart crash on zero or negative values --- cypress/platform/darshan.html | 35 +++++++++++++++++++ docs/syntax/pie.md | 5 +++ packages/mermaid/src/diagrams/pie/pie.spec.ts | 18 ++++++++++ packages/mermaid/src/diagrams/pie/pieDb.ts | 11 ++++++ packages/mermaid/src/docs/syntax/pie.md | 5 +++ .../parser/src/language/common/common.langium | 4 +-- 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 cypress/platform/darshan.html diff --git a/cypress/platform/darshan.html b/cypress/platform/darshan.html new file mode 100644 index 000000000..df5994d7d --- /dev/null +++ b/cypress/platform/darshan.html @@ -0,0 +1,35 @@ + + + + + + Mermaid Quick Test Page + + + + + +

Pie chart demos

+
+     pie title Default text position: Animal adoption
+        accTitle: simple pie char demo
+        accDescr: pie chart with 3 sections: dogs, cats, rats. Most are dogs.
+         "dogs" : -60.67
+        "rats" : 40.12
+    
+ +
+ + + diff --git a/docs/syntax/pie.md b/docs/syntax/pie.md index b8f452b66..87fb3fc58 100644 --- a/docs/syntax/pie.md +++ b/docs/syntax/pie.md @@ -37,6 +37,11 @@ Drawing a pie chart is really simple in mermaid. - Followed by `:` colon as separator - Followed by `positive numeric value` (supported up to two decimal places) +**Note:** + +> Pie chart values must be **positive numbers greater than zero**.\ +> **Zero and negative values are not allowed** and will result in an error. + \[pie] \[showData] (OPTIONAL) \[title] \[titlevalue] (OPTIONAL) "\[datakey1]" : \[dataValue1] diff --git a/packages/mermaid/src/diagrams/pie/pie.spec.ts b/packages/mermaid/src/diagrams/pie/pie.spec.ts index f00906cc5..2b50f5604 100644 --- a/packages/mermaid/src/diagrams/pie/pie.spec.ts +++ b/packages/mermaid/src/diagrams/pie/pie.spec.ts @@ -139,6 +139,24 @@ describe('pie', () => { }).rejects.toThrowError(); }); + it('should handle simple pie with zero slice value', async () => { + await expect(async () => { + await parser.parse(`pie + "ash" : 0 + "bat" : 40.12 + `); + }).rejects.toThrowError(); + }); + + it('should handle simple pie with negative slice value', async () => { + await expect(async () => { + await parser.parse(`pie + "ash" : -60 + "bat" : 40.12 + `); + }).rejects.toThrowError(); + }); + it('should handle unsafe properties', async () => { await expect( parser.parse(`pie title Unsafe props test diff --git a/packages/mermaid/src/diagrams/pie/pieDb.ts b/packages/mermaid/src/diagrams/pie/pieDb.ts index 64831495c..87735b84e 100644 --- a/packages/mermaid/src/diagrams/pie/pieDb.ts +++ b/packages/mermaid/src/diagrams/pie/pieDb.ts @@ -34,6 +34,17 @@ const clear = (): void => { }; const addSection = ({ label, value }: D3Section): void => { + if (value <= 0) { + const error: any = new Error( + `Section "${label}" has invalid value: ${value}. Zero and negative values are not allowed in pie charts. All slice values must be > 0` + ); + error.hash = { + text: `pie "${label}": ${value}`, + token: `${value}`, + expected: ['a positive number (> 0)'], + }; + throw error; + } if (!sections.has(label)) { sections.set(label, value); log.debug(`added new section: ${label}, with value: ${value}`); diff --git a/packages/mermaid/src/docs/syntax/pie.md b/packages/mermaid/src/docs/syntax/pie.md index 2e7a1799a..4ad0dc4e0 100644 --- a/packages/mermaid/src/docs/syntax/pie.md +++ b/packages/mermaid/src/docs/syntax/pie.md @@ -24,6 +24,11 @@ Drawing a pie chart is really simple in mermaid. - Followed by `:` colon as separator - Followed by `positive numeric value` (supported up to two decimal places) +**Note:** + +> Pie chart values must be **positive numbers greater than zero**. +> **Zero and negative values are not allowed** and will result in an error. + [pie] [showData] (OPTIONAL) [title] [titlevalue] (OPTIONAL) "[datakey1]" : [dataValue1] diff --git a/packages/parser/src/language/common/common.langium b/packages/parser/src/language/common/common.langium index b74ffc34d..e6d3d488f 100644 --- a/packages/parser/src/language/common/common.langium +++ b/packages/parser/src/language/common/common.langium @@ -17,8 +17,8 @@ terminal ACC_DESCR: /[\t ]*accDescr(?:[\t ]*:([^\n\r]*?(?=%%)|[^\n\r]*)|\s*{([^} terminal ACC_TITLE: /[\t ]*accTitle[\t ]*:(?:[^\n\r]*?(?=%%)|[^\n\r]*)/; terminal TITLE: /[\t ]*title(?:[\t ][^\n\r]*?(?=%%)|[\t ][^\n\r]*|)/; -terminal FLOAT returns number: /[0-9]+\.[0-9]+(?!\.)/; -terminal INT returns number: /0|[1-9][0-9]*(?!\.)/; +terminal FLOAT returns number: /-?[0-9]+\.[0-9]+(?!\.)/; +terminal INT returns number: /-?(0|[1-9][0-9]*)(?!\.)/; terminal NUMBER returns number: FLOAT | INT; terminal STRING returns string: /"([^"\\]|\\.)*"|'([^'\\]|\\.)*'/;