* Batch-scan comment using indexOf("-->"). Returns true if complete and emitted. * When incomplete, consumes safe content (excluding trailing -) for char-by-char. * * Validates XML 1.0 constraints: * - §2.5: "--" is not permitted within comments, and "--" must be followed by ">" * - §
(buffer: string, bufferLen: number)
| 921 | * - §2.2: Illegal C0 control characters are rejected |
| 922 | */ |
| 923 | #captureComment(buffer: string, bufferLen: number): boolean { |
| 924 | const endIdx = buffer.indexOf("-->", this.#bufferIndex); |
| 925 | |
| 926 | if (endIdx !== -1) { |
| 927 | // Fast path: found complete "-->" terminator |
| 928 | const newContent = buffer.slice(this.#commentStartIdx, endIdx); |
| 929 | |
| 930 | // XML 1.0 §2.5: "--" is not permitted within comments |
| 931 | // Check both the accumulated partial and new content for "--" |
| 932 | if (this.#commentPartial.includes("--") || newContent.includes("--")) { |
| 933 | this.#error( |
| 934 | `Cannot use '--' within comments (XML 1.0 §2.5)`, |
| 935 | ); |
| 936 | } |
| 937 | |
| 938 | // Also check the boundary between partial and new content |
| 939 | if ( |
| 940 | this.#commentPartial.endsWith("-") && newContent.startsWith("-") |
| 941 | ) { |
| 942 | this.#error( |
| 943 | `Cannot use '--' within comments (XML 1.0 §2.5)`, |
| 944 | ); |
| 945 | } |
| 946 | |
| 947 | // Check for trailing dash immediately before "-->" |
| 948 | // (grammar requires every "-" to be followed by a non-dash char) |
| 949 | if ( |
| 950 | newContent.length > 0 && |
| 951 | newContent.charCodeAt(newContent.length - 1) === CC_DASH |
| 952 | ) { |
| 953 | this.#bufferIndex = endIdx - 1; |
| 954 | this.#error( |
| 955 | `Cannot use '-' immediately before '-->' (XML 1.0 §2.5)`, |
| 956 | ); |
| 957 | } |
| 958 | // Also check if partial ends with dash and new content is empty |
| 959 | if ( |
| 960 | newContent.length === 0 && |
| 961 | this.#commentPartial.length > 0 && |
| 962 | this.#commentPartial.charCodeAt( |
| 963 | this.#commentPartial.length - 1, |
| 964 | ) === CC_DASH |
| 965 | ) { |
| 966 | this.#bufferIndex = endIdx - 1; |
| 967 | this.#error( |
| 968 | `Cannot use '-' immediately before '-->' (XML 1.0 §2.5)`, |
| 969 | ); |
| 970 | } |
| 971 | |
| 972 | for (let i = this.#commentStartIdx; i < endIdx; i++) { |
| 973 | const code = buffer.charCodeAt(i); |
| 974 | if (this.#isIllegalLiteralChar(code)) { |
| 975 | this.#bufferIndex = i; |
| 976 | this.#error( |
| 977 | `Illegal XML character U+${ |
| 978 | code.toString(16).toUpperCase().padStart(4, "0") |
| 979 | }`, |
| 980 | ); |
no test coverage detected