token.d.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { TAG_ID } from './html.js';
  2. export declare enum TokenType {
  3. CHARACTER = 0,
  4. NULL_CHARACTER = 1,
  5. WHITESPACE_CHARACTER = 2,
  6. START_TAG = 3,
  7. END_TAG = 4,
  8. COMMENT = 5,
  9. DOCTYPE = 6,
  10. EOF = 7,
  11. HIBERNATION = 8
  12. }
  13. export interface Location {
  14. /** One-based line index of the first character. */
  15. startLine: number;
  16. /** One-based column index of the first character. */
  17. startCol: number;
  18. /** Zero-based first character index. */
  19. startOffset: number;
  20. /** One-based line index of the last character. */
  21. endLine: number;
  22. /** One-based column index of the last character. Points directly *after* the last character. */
  23. endCol: number;
  24. /** Zero-based last character index. Points directly *after* the last character. */
  25. endOffset: number;
  26. }
  27. export interface LocationWithAttributes extends Location {
  28. /** Start tag attributes' location info. */
  29. attrs?: Record<string, Location>;
  30. }
  31. export interface ElementLocation extends LocationWithAttributes {
  32. /** Element's start tag location info. */
  33. startTag?: Location;
  34. /**
  35. * Element's end tag location info.
  36. * This property is undefined, if the element has no closing tag.
  37. */
  38. endTag?: Location;
  39. }
  40. interface TokenBase {
  41. readonly type: TokenType;
  42. location: Location | null;
  43. }
  44. export interface DoctypeToken extends TokenBase {
  45. readonly type: TokenType.DOCTYPE;
  46. name: string | null;
  47. forceQuirks: boolean;
  48. publicId: string | null;
  49. systemId: string | null;
  50. }
  51. export interface Attribute {
  52. /** The name of the attribute. */
  53. name: string;
  54. /** The namespace of the attribute. */
  55. namespace?: string;
  56. /** The namespace-related prefix of the attribute. */
  57. prefix?: string;
  58. /** The value of the attribute. */
  59. value: string;
  60. }
  61. export interface TagToken extends TokenBase {
  62. readonly type: TokenType.START_TAG | TokenType.END_TAG;
  63. tagName: string;
  64. /** Used to cache the ID of the tag name. */
  65. tagID: TAG_ID;
  66. selfClosing: boolean;
  67. ackSelfClosing: boolean;
  68. attrs: Attribute[];
  69. location: LocationWithAttributes | null;
  70. }
  71. export declare function getTokenAttr(token: TagToken, attrName: string): string | null;
  72. export interface CommentToken extends TokenBase {
  73. readonly type: TokenType.COMMENT;
  74. data: string;
  75. }
  76. export interface EOFToken extends TokenBase {
  77. readonly type: TokenType.EOF;
  78. }
  79. export interface CharacterToken extends TokenBase {
  80. type: TokenType.CHARACTER | TokenType.NULL_CHARACTER | TokenType.WHITESPACE_CHARACTER;
  81. chars: string;
  82. }
  83. export type Token = DoctypeToken | TagToken | CommentToken | EOFToken | CharacterToken;
  84. export {};