@cjser/parse5 8.0.1-cjser.2 → 8.1.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,9 @@ export declare class Preprocessor {
18
18
  });
19
19
  /** The column on the current line. If we just saw a gap (eg. a surrogate pair), return the index before. */
20
20
  get col(): number;
21
+ /** The index in `html` at which the current code point starts (the high surrogate of a pair). */
22
+ get codePointStart(): number;
23
+ /** The offset of the current code point, before the second unit of a surrogate pair. */
21
24
  get offset(): number;
22
25
  getError(code: ERR, cpOffset: number): ParserError;
23
26
  private lastErrOffset;
@@ -28,8 +28,13 @@ export class Preprocessor {
28
28
  get col() {
29
29
  return this.pos - this.lineStartPos + Number(this.lastGapPos !== this.pos);
30
30
  }
31
+ /** The index in `html` at which the current code point starts (the high surrogate of a pair). */
32
+ get codePointStart() {
33
+ return this.pos - Number(this.lastGapPos === this.pos);
34
+ }
35
+ /** The offset of the current code point, before the second unit of a surrogate pair. */
31
36
  get offset() {
32
- return this.droppedBufferSize + this.pos;
37
+ return this.droppedBufferSize + this.codePointStart;
33
38
  }
34
39
  getError(code, cpOffset) {
35
40
  const { line, col, offset } = this;
@@ -81,12 +86,16 @@ export class Preprocessor {
81
86
  }
82
87
  dropParsedChunk() {
83
88
  if (this.willDropParsedChunk()) {
84
- this.html = this.html.substring(this.pos);
85
- this.lineStartPos -= this.pos;
86
- this.droppedBufferSize += this.pos;
87
- this.pos = 0;
89
+ // Retain the entire current code point, including both units of a surrogate pair.
90
+ const droppedSize = this.offset - this.droppedBufferSize;
91
+ this.html = this.html.substring(droppedSize);
92
+ this.lineStartPos -= droppedSize;
93
+ this.droppedBufferSize += droppedSize;
94
+ this.pos -= droppedSize;
88
95
  this.lastGapPos = -2;
89
96
  this.gapStack.length = 0;
97
+ if (this.pos > 0)
98
+ this._addGap();
90
99
  }
91
100
  }
92
101
  write(chunk, isLastChunk) {
@@ -12,6 +12,15 @@ export interface TreeAdapterTypeMap<Node = unknown, ParentNode = unknown, ChildN
12
12
  template: Template;
13
13
  documentType: DocumentType;
14
14
  }
15
+ /** Parser state needed to establish a form-associated element's parser-established form owner. */
16
+ export interface ElementCreationContext<T extends TreeAdapterTypeMap = TreeAdapterTypeMap> {
17
+ /** The target parent from the appropriate place for inserting a node. */
18
+ intendedParent: T['parentNode'];
19
+ /** The element referenced by the parser's form element pointer, if any. */
20
+ formElement: T['element'] | null;
21
+ /** Whether the stack of open elements contains an HTML `template` element. */
22
+ isInTemplate: boolean;
23
+ }
15
24
  /**
16
25
  * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format.
17
26
  * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library
@@ -240,6 +249,14 @@ export interface TreeAdapter<T extends TreeAdapterTypeMap = TreeAdapterTypeMap>
240
249
  * @param contentElement - Content element.
241
250
  */
242
251
  setTemplateContent(templateElement: T['template'], contentElement: T['documentFragment']): void;
252
+ /**
253
+ * Optional callback invoked after an element is created from a token and before it is inserted. This exposes the
254
+ * parser state needed to establish a form-associated element's parser-established form owner.
255
+ *
256
+ * @param element The created element.
257
+ * @param context Relevant parser state at the time of creation.
258
+ */
259
+ onElementCreated?: (element: T['element'], context: ElementCreationContext<T>) => void;
243
260
  /**
244
261
  * Optional callback for elements being pushed to the stack of open elements.
245
262
  *