MCPcopy Index your code
hub / github.com/fb55/htmlparser2 / stateInCommentLike

Method stateInCommentLike

src/Tokenizer.ts:418–467  ·  view source on GitHub ↗

* Comments and CDATA end with `-->` and `]]>`. * * Their common qualities are: * - Their end sequences have a distinct character they start with. * - That character is then repeated, so we have to check multiple repeats. * - All characters but the start character of the sequ

(c: number)

Source from the content-addressed store, hash-verified

416 * @param c Current character code point.
417 */
418 private stateInCommentLike(c: number): void {
419 if (
420 !this.xmlMode &&
421 this.currentSequence === Sequences.CommentEnd &&
422 this.sequenceIndex <= 1 &&
423 /*
424 * We're still at the very start of the comment: the only
425 * characters consumed since `<!--` are the dashes that
426 * advanced sequenceIndex (0 for `<!-->`, 1 for `<!--->`).
427 */
428 this.index === this.sectionStart + this.sequenceIndex &&
429 c === CharCodes.Gt
430 ) {
431 // Abruptly closed empty HTML comment.
432 this.emitComment(this.sequenceIndex);
433 } else if (
434 this.currentSequence === Sequences.CommentEnd &&
435 this.sequenceIndex === 2 &&
436 c === CharCodes.Gt
437 ) {
438 // `!` is optional here, so the same sequence also accepts `-->`.
439 this.emitComment(2);
440 } else if (
441 this.currentSequence === Sequences.CommentEnd &&
442 this.sequenceIndex === this.currentSequence.length - 1 &&
443 c !== CharCodes.Gt
444 ) {
445 this.sequenceIndex = Number(c === CharCodes.Dash);
446 } else if (c === this.currentSequence[this.sequenceIndex]) {
447 if (++this.sequenceIndex === this.currentSequence.length) {
448 if (this.currentSequence === Sequences.CdataEnd) {
449 this.cbs.oncdata(this.sectionStart, this.index, 2);
450 } else {
451 this.cbs.oncomment(this.sectionStart, this.index, 3);
452 }
453
454 this.sequenceIndex = 0;
455 this.sectionStart = this.index + 1;
456 this.state = State.Text;
457 }
458 } else if (this.sequenceIndex === 0) {
459 // Fast-forward to the first character of the sequence
460 if (this.fastForwardTo(this.currentSequence[0])) {
461 this.sequenceIndex = 1;
462 }
463 } else if (c !== this.currentSequence[this.sequenceIndex - 1]) {
464 // Allow long sequences, eg. --->, ]]]>
465 this.sequenceIndex = 0;
466 }
467 }
468
469 /**
470 * HTML only allows ASCII alpha characters (a-z and A-Z) at the beginning of a tag name.

Callers 1

parseMethod · 0.95

Calls 4

emitCommentMethod · 0.95
fastForwardToMethod · 0.95
oncdataMethod · 0.65
oncommentMethod · 0.65

Tested by

no test coverage detected