@litejs/dom 25.9.2 → 25.12.0

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.
Files changed (3) hide show
  1. package/dom.js +39 -1
  2. package/package.json +4 -2
  3. package/types/index.d.ts +191 -0
package/dom.js CHANGED
@@ -32,6 +32,12 @@ var boolAttrs = {
32
32
  DOCUMENT_NODE: 9,
33
33
  DOCUMENT_TYPE_NODE: 10,
34
34
  DOCUMENT_FRAGMENT_NODE: 11,
35
+ DOCUMENT_POSITION_DISCONNECTED: 1,
36
+ DOCUMENT_POSITION_PRECEDING: 2,
37
+ DOCUMENT_POSITION_FOLLOWING: 4,
38
+ DOCUMENT_POSITION_CONTAINS: 8,
39
+ DOCUMENT_POSITION_CONTAINED_BY: 16,
40
+ DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32,
35
41
  nodeName: null,
36
42
  parentNode: null,
37
43
  ownerDocument: null,
@@ -130,10 +136,26 @@ var boolAttrs = {
130
136
  }
131
137
  return clone
132
138
  },
139
+ compareDocumentPosition(other) {
140
+ var node = this
141
+ if (node === other) return 0
142
+ if (node.getRootNode() !== other.getRootNode()) return Node.DOCUMENT_POSITION_DISCONNECTED
143
+ if (node.contains(other)) return Node.DOCUMENT_POSITION_CONTAINS
144
+ if (other.contains(node)) return Node.DOCUMENT_POSITION_CONTAINED_BY
145
+
146
+ for (; node; node = node.nextSibling || node.parentNode && node.parentNode.nextSibling) {
147
+ if (node === other) return Node.DOCUMENT_POSITION_FOLLOWING
148
+ }
149
+ return Node.DOCUMENT_POSITION_PRECEDING
150
+ },
133
151
  contains(el) {
134
152
  for (; el; el = el.parentNode) if (el === this) return true
135
153
  return false
136
154
  },
155
+ getRootNode() {
156
+ for (var node = this; node.parentNode; ) node = node.parentNode
157
+ return node
158
+ },
137
159
  hasChildNodes() {
138
160
  return !!this.firstChild
139
161
  },
@@ -180,6 +202,22 @@ var boolAttrs = {
180
202
  this.tagName === "STYLE" && (min === true || min && min.css) ? "\n" + makeSheet(this, min.css || true) + "\n" :
181
203
  this.textContent
182
204
  ) : this.childNodes.map(node => node.toString(min)).join("")
205
+ },
206
+ toJSON() {
207
+ var node = this
208
+ , json = {
209
+ name: node.nodeName
210
+ }
211
+ if (node.nodeType === 1) {
212
+ json.attributes = {}
213
+ node.attributes.names().forEach(name => json.attributes[name] = node.getAttribute(name))
214
+ }
215
+ if (node.nodeType === 1 || node.nodeType === 9) {
216
+ json.children = node.childNodes.map(child => child.toJSON())
217
+ } else {
218
+ json.data = node.data
219
+ }
220
+ return json
183
221
  }
184
222
  }
185
223
  , Element = {
@@ -425,6 +463,7 @@ extendNode(DocumentType, {
425
463
 
426
464
  function Document() {
427
465
  this.childNodes = []
466
+ this.attributes = new NamedNodeMap(this)
428
467
  this
429
468
  .appendChild(this.createElement("html"))
430
469
  .appendChild(this.body = this.createElement("body"))
@@ -532,4 +571,3 @@ exports.DocumentFragment = DocumentFragment
532
571
  exports.HTMLElement = HTMLElement
533
572
  exports.Node = Node
534
573
  exports.XMLSerializer = XMLSerializer
535
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litejs/dom",
3
- "version": "25.9.2",
3
+ "version": "25.12.0",
4
4
  "description": "A small DOM library for server-side testing, rendering, and handling of HTML files",
5
5
  "license": "MIT",
6
6
  "author": "Lauri Rooden <lauri@rooden.ee>",
@@ -15,9 +15,11 @@
15
15
  "XMLSerializer",
16
16
  "litejs"
17
17
  ],
18
+ "types": "types/index.d.ts",
18
19
  "main": "dom.js",
19
20
  "files": [
20
- "*.js"
21
+ "*.js",
22
+ "types"
21
23
  ],
22
24
  "scripts": {
23
25
  "test": "lj t"
@@ -0,0 +1,191 @@
1
+ export type JSONNode = {
2
+ nodeType: number
3
+ nodeName: string
4
+ tagName?: string
5
+ attributes?: Record<string, string>
6
+ data?: string
7
+ childNodes?: JSONNode[]
8
+ contentType?: string
9
+ }
10
+
11
+ export interface Attr {
12
+ name: string
13
+ value: string
14
+ ownerElement: HTMLElement
15
+ }
16
+
17
+ export interface NamedNodeMap {
18
+ readonly length: number
19
+ readonly ownerElement: HTMLElement
20
+ names(): string[]
21
+ getNamedItem(name: string): Attr | null
22
+ setNamedItem(attr: Attr): Attr | null
23
+ removeNamedItem(name: string): Attr | null
24
+ toString(minify?: boolean): string
25
+ }
26
+
27
+ export interface NodeConstants {
28
+ readonly ELEMENT_NODE: 1
29
+ readonly TEXT_NODE: 3
30
+ readonly PROCESSING_INSTRUCTION_NODE: 7
31
+ readonly COMMENT_NODE: 8
32
+ readonly DOCUMENT_NODE: 9
33
+ readonly DOCUMENT_TYPE_NODE: 10
34
+ readonly DOCUMENT_FRAGMENT_NODE: 11
35
+ readonly DOCUMENT_POSITION_DISCONNECTED: 1
36
+ readonly DOCUMENT_POSITION_PRECEDING: 2
37
+ readonly DOCUMENT_POSITION_FOLLOWING: 4
38
+ readonly DOCUMENT_POSITION_CONTAINS: 8
39
+ readonly DOCUMENT_POSITION_CONTAINED_BY: 16
40
+ readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32
41
+ }
42
+
43
+ export type MinifyOption = boolean | { css?: boolean }
44
+
45
+ export abstract class Node {
46
+ static readonly ELEMENT_NODE: 1
47
+ static readonly TEXT_NODE: 3
48
+ static readonly PROCESSING_INSTRUCTION_NODE: 7
49
+ static readonly COMMENT_NODE: 8
50
+ static readonly DOCUMENT_NODE: 9
51
+ static readonly DOCUMENT_TYPE_NODE: 10
52
+ static readonly DOCUMENT_FRAGMENT_NODE: 11
53
+ static readonly DOCUMENT_POSITION_DISCONNECTED: 1
54
+ static readonly DOCUMENT_POSITION_PRECEDING: 2
55
+ static readonly DOCUMENT_POSITION_FOLLOWING: 4
56
+ static readonly DOCUMENT_POSITION_CONTAINS: 8
57
+ static readonly DOCUMENT_POSITION_CONTAINED_BY: 16
58
+ static readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32
59
+
60
+ nodeType: number
61
+ nodeName: string
62
+ parentNode: Node | null
63
+ ownerDocument: Document | null
64
+ childNodes: Node[]
65
+ nodeValue: string | null
66
+ textContent: string
67
+ firstChild: Node | null
68
+ lastChild: Node | null
69
+ nextSibling: Node | null
70
+ previousSibling: Node | null
71
+ appendChild(child: Node): Node
72
+ insertBefore(child: Node, ref?: Node | null): Node
73
+ removeChild(child: Node): Node
74
+ replaceChild(child: Node, ref: Node): Node
75
+ cloneNode(deep?: boolean): Node
76
+ contains(node: Node | null): boolean
77
+ compareDocumentPosition(other: Node): number
78
+ getRootNode(): Node
79
+ hasChildNodes(): boolean
80
+ toString(minify?: MinifyOption): string
81
+ toJSON(): JSONNode
82
+ }
83
+
84
+ export class DocumentFragment extends Node {
85
+ constructor()
86
+ override nodeType: 11
87
+ override nodeName: "#document-fragment"
88
+ }
89
+
90
+ export class CSSStyleDeclaration {
91
+ constructor(cssText?: string)
92
+ cssText: string
93
+ [key: string]: string
94
+ }
95
+
96
+ export interface StyleSheetInit {
97
+ min?: boolean | { css?: boolean }
98
+ href?: string
99
+ ownerNode?: HTMLElement
100
+ }
101
+
102
+ export class CSSStyleSheet {
103
+ constructor(init?: StyleSheetInit, cssText?: string)
104
+ href?: string
105
+ ownerNode?: HTMLElement
106
+ replaceSync(cssText: string): void
107
+ toString(minify?: boolean | { css?: boolean }): string
108
+ }
109
+
110
+ export class HTMLElement extends Node {
111
+ attributes: NamedNodeMap
112
+ localName: string
113
+ tagName: string
114
+ namespaceURI: string
115
+ style: CSSStyleDeclaration
116
+ outerHTML: string
117
+ innerHTML: string
118
+ firstElementChild: HTMLElement | null
119
+ lastElementChild: HTMLElement | null
120
+ nextElementSibling: HTMLElement | null
121
+ previousElementSibling: HTMLElement | null
122
+ readonly sheet?: CSSStyleSheet
123
+ replaceChildren(...nodes: Node[]): void
124
+ getAttribute(name: string): string | null
125
+ setAttribute(name: string, value: unknown): void
126
+ removeAttribute(name: string): void
127
+ hasAttribute(name: string): boolean
128
+ getAttributeNS(ns: string | null, name: string): string | null
129
+ setAttributeNS(ns: string | null, name: string, value: unknown): void
130
+ removeAttributeNS(ns: string | null, name: string): void
131
+ hasAttributeNS(ns: string | null, name: string): boolean
132
+ getElementsByTagName(tag: string): HTMLElement[]
133
+ getElementsByClassName(sel: string): HTMLElement[]
134
+ querySelector(sel: string): HTMLElement | null
135
+ querySelectorAll(sel: string): HTMLElement[]
136
+ matches(sel: string): boolean
137
+ closest(sel: string): HTMLElement | null
138
+ blur(): void
139
+ focus(): void
140
+ }
141
+
142
+ export class ElementNS extends HTMLElement {
143
+ constructor(namespace: string | null, tag: string)
144
+ }
145
+
146
+ export class Text extends Node {
147
+ constructor(data: string | number | null | undefined)
148
+ data: string
149
+ }
150
+
151
+ export class Comment extends Node {
152
+ constructor(data: string | number | null | undefined)
153
+ data: string
154
+ }
155
+
156
+ export class DocumentType extends Node {
157
+ constructor(name: string)
158
+ data: string
159
+ }
160
+
161
+ export class Document extends HTMLElement {
162
+ constructor()
163
+ override nodeType: 9
164
+ override nodeName: "#document"
165
+ body: HTMLElement
166
+ documentElement: HTMLElement
167
+ contentType: string
168
+ styleSheets: CSSStyleSheet[]
169
+ title: string
170
+ createElement(tag: string): HTMLElement
171
+ createElementNS(namespace: string | null, tag: string): ElementNS
172
+ createTextNode(data: string | number | null | undefined): Text
173
+ createComment(data: string | number | null | undefined): Comment
174
+ createDocumentType(name: string): DocumentType
175
+ createDocumentFragment(): DocumentFragment
176
+ getElementById(id: string): HTMLElement | null
177
+ }
178
+
179
+ export class DOMParser {
180
+ parseFromString(str: string, mime?: string): Document
181
+ }
182
+
183
+ export class XMLSerializer {
184
+ serializeToString(doc: Document): string
185
+ }
186
+
187
+ export const document: Document
188
+ export const entities: Record<string, string>
189
+ export function mergeAttributes(source: HTMLElement | null, target: HTMLElement | null): void
190
+ export function selectorSplit(selector: string): string[]
191
+ export function cssEscape(selector: string | number): string