55 gs1datamatrix ,
66 gs1datamatrixrectangular
77} from "@bwip-js/browser" ;
8+ import DOMPurify from "dompurify" ;
89import { ReactElement , useMemo , useRef } from "react" ;
910import { DownloadButton } from "./DownloadButton" ;
1011import { DataMatrixTypeConfig } from "../config/Barcode.config" ;
@@ -16,25 +17,27 @@ interface DataMatrixRendererProps {
1617 config : DataMatrixTypeConfig ;
1718}
1819
20+ type DataMatrixEncodeParams = Pick < DataMatrixTypeConfig , "codeValue" | "size" | "margin" | "gs1Mode" | "shape" > ;
21+
1922/** Selects the bwip-js encoder for the requested GS1 mode and symbol shape. */
20- function encodeDataMatrix ( config : DataMatrixTypeConfig ) : string {
23+ function encodeDataMatrix ( { codeValue , size , margin , gs1Mode , shape } : DataMatrixEncodeParams ) : string {
2124 const opts = {
22- text : config . codeValue ,
25+ text : codeValue ,
2326 // bwip-js scale is in module units; map the pixel size onto a reasonable scale.
24- scale : Math . max ( 1 , Math . round ( config . size / 32 ) ) ,
25- paddingwidth : config . margin ,
26- paddingheight : config . margin ,
27+ scale : Math . max ( 1 , Math . round ( size / 32 ) ) ,
28+ paddingwidth : margin ,
29+ paddingheight : margin ,
2730 // GS1 AI syntax uses parentheses; parse must be on for the human-readable form.
28- parse : config . gs1Mode
31+ parse : gs1Mode
2932 } as const ;
3033
31- if ( config . gs1Mode ) {
32- return config . shape === "rectangle"
34+ if ( gs1Mode ) {
35+ return shape === "rectangle"
3336 ? gs1datamatrixrectangular ( { ...opts , bcid : "gs1datamatrixrectangular" } , drawingSVG ( ) )
3437 : gs1datamatrix ( { ...opts , bcid : "gs1datamatrix" } , drawingSVG ( ) ) ;
3538 }
3639
37- return config . shape === "rectangle"
40+ return shape === "rectangle"
3841 ? datamatrixrectangular ( { ...opts , bcid : "datamatrixrectangular" } , drawingSVG ( ) )
3942 : datamatrix ( { ...opts , bcid : "datamatrix" } , drawingSVG ( ) ) ;
4043}
@@ -53,7 +56,7 @@ function getSvgPixelSize(svg: string, size: number): { width: number; height: nu
5356
5457export function DataMatrixRenderer ( { config } : DataMatrixRendererProps ) : ReactElement {
5558 const containerRef = useRef < HTMLDivElement > ( null ) ;
56- const { codeValue, downloadButton, size, gs1Mode } = config ;
59+ const { codeValue, downloadButton, size, gs1Mode, shape , margin , logLevel } = config ;
5760 const buttonPosition = downloadButton ?. buttonPosition ?? "bottom" ;
5861
5962 const { svg, error } = useMemo < { svg : string | null ; error : boolean } > ( ( ) => {
@@ -63,27 +66,26 @@ export function DataMatrixRenderer({ config }: DataMatrixRendererProps): ReactEl
6366
6467 const baseValidation = validateBarcodeValue ( "DataMatrix" , codeValue ) ;
6568 if ( ! baseValidation . valid ) {
66- printError ( `Validation failed for Data Matrix: ${ baseValidation . message } ` , config . logLevel ) ;
69+ printError ( `Validation failed for Data Matrix: ${ baseValidation . message } ` , logLevel ) ;
6770 return { svg : null , error : true } ;
6871 }
6972
7073 if ( gs1Mode ) {
7174 const gs1Validation = validateGs1DataMatrixValue ( codeValue ) ;
7275 if ( ! gs1Validation . valid ) {
73- printError ( `GS1 Data Matrix validation failed: ${ gs1Validation . message } ` , config . logLevel ) ;
76+ printError ( `GS1 Data Matrix validation failed: ${ gs1Validation . message } ` , logLevel ) ;
7477 return { svg : null , error : true } ;
7578 }
7679 }
7780
7881 try {
79- return { svg : encodeDataMatrix ( config ) , error : false } ;
82+ return { svg : encodeDataMatrix ( { codeValue , size , margin , gs1Mode , shape } ) , error : false } ;
8083 } catch ( e ) {
8184 const message = e instanceof Error ? e . message : "Error generating Data Matrix" ;
82- printError ( `Rendering failed: ${ message } \nValue: "${ codeValue } "` , config . logLevel ) ;
85+ printError ( `Rendering failed: ${ message } \nValue: "${ codeValue } "` , logLevel ) ;
8386 return { svg : null , error : true } ;
8487 }
85- // eslint-disable-next-line react-hooks/exhaustive-deps
86- } , [ codeValue , size , gs1Mode , config . shape , config . margin , config . logLevel ] ) ;
88+ } , [ codeValue , size , gs1Mode , shape , margin , logLevel ] ) ;
8789
8890 if ( error || ! svg ) {
8991 return (
@@ -117,7 +119,9 @@ export function DataMatrixRenderer({ config }: DataMatrixRendererProps): ReactEl
117119 ref = { containerRef }
118120 className = "datamatrix-svg"
119121 style = { { width, height } }
120- dangerouslySetInnerHTML = { { __html : svg } }
122+ dangerouslySetInnerHTML = { {
123+ __html : DOMPurify . sanitize ( svg , { USE_PROFILES : { svg : true , svgFilters : true } } )
124+ } }
121125 />
122126 { buttonPosition === "bottom" && button }
123127 </ div >
0 commit comments