@@ -130,6 +130,15 @@ function countMatches(source: string, pattern: RegExp): number {
130130 return source . match ( pattern ) ?. length ?? 0 ;
131131}
132132
133+ function getRootStartTag ( xml : string , tagName : string ) : string {
134+ const escapedTagName = tagName . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
135+ const match = xml . match ( new RegExp ( `<${ escapedTagName } \\b[^>]*>` ) ) ;
136+ if ( ! match ) {
137+ throw new Error ( `Missing root start tag <${ tagName } >.` ) ;
138+ }
139+ return match [ 0 ] ;
140+ }
141+
133142describe ( 'document-api story: lists.create numbering metadata regression' , ( ) => {
134143 it ( 'registers numbering metadata when bullet list creation adds numbering to a numbering-less source docx' , async ( ) => {
135144 const resultsDir = path . join ( STORIES_ROOT , 'results' , 'lists' , 'numbering-metadata-regression' ) ;
@@ -180,5 +189,21 @@ describe('document-api story: lists.create numbering metadata regression', () =>
180189 ) ,
181190 ) . toBe ( 1 ) ;
182191 expect ( countMatches ( resultDocumentRels , / T a r g e t = " n u m b e r i n g \. x m l " / g) ) . toBe ( 1 ) ;
192+
193+ // SD-2252: every namespace prefix used in the numbering part must be
194+ // declared on the root element, otherwise Word flags the file as
195+ // unreadable. Check the actual <w:numbering ...> start tag so we do not
196+ // false-pass on an xmlns declaration that appears later or in a narrower scope.
197+ const numberingRootStartTag = getRootStartTag ( resultNumbering , 'w:numbering' ) ;
198+ const usedPrefixes = new Set ( [ ...resultNumbering . matchAll ( / (?: ^ | [ \s < ] ) ( \w + ) : / g) ] . map ( ( m ) => m [ 1 ] ) ) ;
199+ // xml and xmlns are built-in prefixes that never need an explicit declaration.
200+ usedPrefixes . delete ( 'xml' ) ;
201+ usedPrefixes . delete ( 'xmlns' ) ;
202+
203+ for ( const prefix of usedPrefixes ) {
204+ expect ( numberingRootStartTag , `missing xmlns:${ prefix } declaration on <w:numbering>` ) . toMatch (
205+ new RegExp ( `xmlns:${ prefix } =` ) ,
206+ ) ;
207+ }
183208 } ) ;
184209} ) ;
0 commit comments