@@ -7,13 +7,13 @@ import Tokenizer from './Tokenizer';
77import ResolvedApiItem from './ResolvedApiItem' ;
88
99export default class DocElementParser {
10-
1110 /**
12- * Matches only strings that contain characters for words.
13- * Any non word characters or spaces, will be present in the third entry in the match results
14- * if they exist.
11+ * Used to validate the display text for an \@link tag. The display text can contain any
12+ * characters except for certain AEDoc delimiters: "@", "|", "{", "}".
13+ * This RegExp matches the first bad character.
14+ * Example: "Microsoft's {spec}" --> "{"
1515 */
16- private static _wordRegEx : RegExp = / ^ ( [ \w \s ] * ) / ;
16+ private static _displayTextBadCharacterRegEx : RegExp = / [ @ | { } ] / ;
1717
1818 /**
1919 * Matches a href reference. This is used to get an idea whether a given reference is for an href
@@ -142,67 +142,72 @@ export default class DocElementParser {
142142 }
143143
144144 // Make sure there are no extra pipes
145- let pipeSplitContent : string [ ] = tokenItem . text . split ( '|' ) ;
146- pipeSplitContent = pipeSplitContent . map ( value => {
145+ const pipeSplitContent : string [ ] = tokenItem . text . split ( '|' ) . map ( value => {
147146 if ( value ) {
148147 return value . trim ( ) ;
149148 }
150149 } ) ;
151150 if ( pipeSplitContent . length > 2 ) {
152151 documentation . reportError ( 'The {@link} tag contains more than one pipe character ("|")' ) ;
153- return ;
152+ return undefined ;
154153 }
155154
155+ const addressPart : string = pipeSplitContent [ 0 ] ;
156+ const displayTextPart : string = pipeSplitContent . length > 1 ? pipeSplitContent [ 1 ] : '' ;
157+
156158 // Try to guess if the tokenContent is a link or API definition reference
157159 let linkDocElement : ICodeLinkElement | IHrefLinkElement ;
158- if ( tokenItem . text . match ( this . _hrefRegEx ) ) {
159- const urlContent : string [ ] = pipeSplitContent [ 0 ] . split ( ' ' ) ;
160-
160+ if ( this . _hrefRegEx . test ( addressPart ) ) {
161161 // Make sure only a single URL is given
162- if ( urlContent . length > 1 && urlContent [ 1 ] !== '' ) {
162+ if ( addressPart . indexOf ( ' ' ) >= 0 ) {
163163 documentation . reportError ( 'The {@link} tag contains additional spaces after the URL;'
164164 + ' if the URL contains spaces, encode them using %20; for display text, use a pipe delimiter ("|")' ) ;
165- return ;
165+ return undefined ;
166166 }
167167
168168 linkDocElement = {
169169 kind : 'linkDocElement' ,
170170 referenceType : 'href' ,
171- targetUrl : urlContent [ 0 ] ,
172- value : ''
171+ targetUrl : addressPart
172+ // (" value" will be assigned below)
173173 } ;
174174
175175 } else {
176176 // we are processing an API definition reference
177177 const apiDefitionRef : ApiDefinitionReference = ApiDefinitionReference . createFromString (
178- pipeSplitContent [ 0 ] ,
178+ addressPart ,
179179 documentation . reportError
180180 ) ;
181181
182182 // Once we can locate local API definitions, an error should be reported here if not found.
183- if ( apiDefitionRef ) {
184-
185- linkDocElement = {
186- kind : 'linkDocElement' ,
187- referenceType : 'code' ,
188- scopeName : apiDefitionRef . scopeName ,
189- packageName : apiDefitionRef . packageName ,
190- exportName : apiDefitionRef . exportName ,
191- memberName : apiDefitionRef . memberName
192- } ;
183+ if ( ! apiDefitionRef ) {
184+ return undefined ;
193185 }
186+
187+ linkDocElement = {
188+ kind : 'linkDocElement' ,
189+ referenceType : 'code' ,
190+ scopeName : apiDefitionRef . scopeName ,
191+ packageName : apiDefitionRef . packageName ,
192+ exportName : apiDefitionRef . exportName ,
193+ memberName : apiDefitionRef . memberName
194+ // ("value" will be assigned below)
195+ } ;
194196 }
195197
196198 // If a display name is given, ensure it only contains characters for words.
197- if ( linkDocElement && pipeSplitContent . length > 1 ) {
198- const displayTextParts : string [ ] = pipeSplitContent [ 1 ] . match ( this . _wordRegEx ) ;
199- if ( displayTextParts && displayTextParts [ 0 ] . length !== pipeSplitContent [ 1 ] . length ) {
200- documentation . reportError ( `The {@link} tag\'s display text contains unsupported `
201- + ` symbols : "${ pipeSplitContent [ 1 ] } "` ) ;
202- return ;
199+ if ( displayTextPart ) {
200+ const match : RegExpExecArray | undefined = this . _displayTextBadCharacterRegEx . exec ( displayTextPart ) ;
201+ if ( match ) {
202+ documentation . reportError ( `The {@link} tag\'s display text contains an unsupported `
203+ + ` character : "${ match [ 0 ] } "` ) ;
204+ return undefined ;
203205 }
204206 // Full match is valid text
205- linkDocElement . value = displayTextParts [ 0 ] . trim ( ) ;
207+ linkDocElement . value = displayTextPart ;
208+ } else {
209+ // If the display text is not explicitly provided, then use the address as the display text
210+ linkDocElement . value = addressPart ;
206211 }
207212
208213 return linkDocElement ;
0 commit comments