MCPcopy Create free account
hub / github.com/denoland/std / onText

Method onText

xml/_parser.ts:533–576  ·  view source on GitHub ↗
(
    content: string,
    line: number,
    column: number,
    offset: number,
  )

Source from the content-addressed store, hash-verified

531 }
532
533 onText(
534 content: string,
535 line: number,
536 column: number,
537 offset: number,
538 ): void {
539 // XML 1.0 §2.8: Prolog and epilog only allow Misc, where:
540 // Misc ::= Comment | PI | S
541 // S is LITERAL whitespace only, not character/entity references
542 const outsideRoot = !this.#hasRoot || this.#rootClosed;
543 if (outsideRoot) {
544 // Check raw content for any entity/character references
545 if (content.includes("&")) {
546 throw new XmlSyntaxError(
547 "Cannot use character/entity references in prolog/epilog (XML 1.0 §2.8)",
548 { line, column, offset },
549 );
550 }
551 // For outside root, only whitespace is allowed - check raw content first
552 // (avoids entity decoding for whitespace-only content)
553 if (!WHITESPACE_ONLY_REGEXP.test(content)) {
554 if (!this.#hasRoot) {
555 throw new XmlSyntaxError(
556 "Cannot have content before the root element (XML 1.0 §2.8)",
557 { line, column, offset },
558 );
559 } else {
560 throw new XmlSyntaxError(
561 "Cannot have content after the root element (XML 1.0 §2.8)",
562 { line, column, offset },
563 );
564 }
565 }
566 // Outside root whitespace - skip if ignoring whitespace, otherwise emit
567 if (this.#ignoreWhitespace) return;
568 this.#callbacks.onText?.(content, line, column, offset);
569 return;
570 }
571
572 // Inside root element - decode entities
573 const text = decodeEntities(content, this.#xml11);
574 if (this.#ignoreWhitespace && WHITESPACE_ONLY_REGEXP.test(text)) return;
575 this.#callbacks.onText?.(text, line, column, offset);
576 }
577
578 onCData(
579 content: string,

Callers 2

#flushTextMethod · 0.80
onCDataMethod · 0.80

Calls 2

decodeEntitiesFunction · 0.90
includesMethod · 0.80

Tested by

no test coverage detected