* Validates an EncName per XML 1.0 §4.3.3. * EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* * * @returns true if valid, false otherwise
(encoding: string)
| 125 | * @returns true if valid, false otherwise |
| 126 | */ |
| 127 | function isValidEncName(encoding: string): boolean { |
| 128 | if (encoding.length === 0) { |
| 129 | return false; |
| 130 | } |
| 131 | // First character must be a letter |
| 132 | const first = encoding.charCodeAt(0); |
| 133 | if ( |
| 134 | !(first >= 0x41 && first <= 0x5A) && // A-Z |
| 135 | !(first >= 0x61 && first <= 0x7A) // a-z |
| 136 | ) { |
| 137 | return false; |
| 138 | } |
| 139 | // Rest can be letter, digit, '.', '_', '-' |
| 140 | for (let i = 1; i < encoding.length; i++) { |
| 141 | const code = encoding.charCodeAt(i); |
| 142 | if ( |
| 143 | !(code >= 0x41 && code <= 0x5A) && // A-Z |
| 144 | !(code >= 0x61 && code <= 0x7A) && // a-z |
| 145 | !(code >= 0x30 && code <= 0x39) && // 0-9 |
| 146 | code !== 0x2E && // . |
| 147 | code !== 0x5F && // _ |
| 148 | code !== 0x2D // - |
| 149 | ) { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Validates and parses an XML declaration content string. |
no outgoing calls
no test coverage detected