版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

472 Zeilen
22 KiB

  1. import * as happy_dom from 'happy-dom';
  2. import { Document as Document$1, DocumentFragment as DocumentFragment$1, IElement as IElement$1, IComment as IComment$1, IText as IText$1, INode as INode$1 } from 'happy-dom';
  3. import { Events } from 'vue';
  4. /**
  5. * Use the self-reported numeric type that a node presents
  6. * to identify what type it is.
  7. */
  8. declare const nodeTypeLookup: (type: number) => NodeType | undefined;
  9. /**
  10. * Determines the "content-type" of a given node
  11. */
  12. declare const getNodeType: (node: Container | HTML) => NodeType;
  13. /**
  14. * A helper utility to help convert DOM nodes or HTML to common type.
  15. *
  16. * Start by providing the _exclusions_ you want to make for input. By default, all
  17. * `Container` types are allowed along with `HTML`
  18. */
  19. declare const solveForNodeType: NodeSolver;
  20. /**
  21. * Ensures any Container, array of Containers, or even HTML or HTML[] are all
  22. * normalized down to just HTML.
  23. */
  24. declare function toHtml<D extends ContainerOrHtml | ContainerOrHtml[] | null>(node: D): HTML;
  25. /**
  26. * Clones most DOM types
  27. */
  28. declare function clone<T extends Container | HTML>(container: T): T;
  29. /**
  30. * ensures that a given string doesn't have any HTML inside of it
  31. */
  32. declare function safeString(str: string): string;
  33. type InspectionTuple = [msg: string, item: unknown];
  34. type HTML = string;
  35. type GetAttribute<T extends string> = <N extends Container | HTML>(node: N) => string;
  36. type NodeTypeInput<T extends ReturnType<typeof getNodeType>> = T extends "html" ? string : T extends "text" ? IText : T extends "element" ? IElement : T extends "document" ? HappyDoc : T;
  37. /** The distinct types of Nodes we may encounter */
  38. type NodeType = "html" | "text" | "element" | "comment" | "node" | "document" | "fragment";
  39. type NodeSolverInput<T extends NodeType> = T extends "html" ? HTML : T extends "comment" ? IComment : T extends "text" ? IText : T extends "element" ? IElement : T extends "node" ? INode : T extends "document" ? HappyDoc : T extends "fragment" ? Fragment : unknown;
  40. type DocRoot = HappyDoc | Fragment;
  41. type DomNode = IElement | IText | IComment | INode;
  42. type Container = DocRoot | DomNode;
  43. type ContainerOrHtml = Container | HTML;
  44. /**
  45. * The select.update/updateAll methods not only receive the _element_
  46. * which is being mutated but also the _index_ and _total count_ of selection
  47. * results.
  48. */
  49. type UpdateSignature = [el: IElement, idx: number, total: number] | [el: IElement, idx: number] | [el: IElement];
  50. interface ToHtmlOptions {
  51. pretty?: boolean;
  52. indent?: number;
  53. }
  54. interface TreeSummary {
  55. node: string;
  56. children: TreeSummary[];
  57. }
  58. interface Tree {
  59. node: Container;
  60. type: string;
  61. level: number;
  62. summary: () => TreeSummary;
  63. toString: () => string;
  64. children: Tree[];
  65. }
  66. /**
  67. * A callback which receives a node type `C` and allows side-effects and/or
  68. * mutation. It expects the same container structure -- mutation or not -- to
  69. * be passed back as a return value. The one exception is that if you pass back
  70. * a `false` value then the element will be removed.
  71. */
  72. type UpdateCallback_Native = (el: IElement, idx: number, total: number) => IElement | false;
  73. type UpdateCallback = (el: IElement, idx: number, total: number) => IElement | false;
  74. type MapCallback<I, O> = (input: I) => O;
  75. interface NodeSolverDict<O> {
  76. html: (input: HTML) => O extends "mirror" ? HTML : O;
  77. text: (input: IText) => O extends "mirror" ? IText : O;
  78. element: (input: IElement, parent?: IElement | DocRoot) => O extends "mirror" ? IElement : O;
  79. comment: (input: IComment) => O extends "mirror" ? IComment : O;
  80. node: (input: INode) => O extends "mirror" ? INode : O;
  81. document: (input: HappyDoc) => O extends "mirror" ? HappyDoc : O;
  82. fragment: (input: Fragment) => O extends "mirror" ? Fragment : O;
  83. }
  84. /**
  85. * A fully configured solver which is ready to convert a node into type `O`; if `O` is never
  86. * then it will mirror the input type as the output type
  87. */
  88. type NodeSolverReady<E extends NodeType, O> = <N extends Exclude<Container | HTML | null, E>>(node: N, parent?: IElement | DocRoot) => O extends "mirror" ? N : O;
  89. interface NodeSolverReceiver<E extends NodeType, O> {
  90. /** provide a solver dictionary */
  91. solver: (solver: Omit<NodeSolverDict<O>, E>) => NodeSolverReady<E, O>;
  92. }
  93. interface NodeSolverWithExclusions<E extends NodeType> {
  94. /** provide a type which all solvers will convert to */
  95. outputType: <O>() => NodeSolverReceiver<E, O>;
  96. /** the input type should be maintained as the output type */
  97. mirror: () => NodeSolverReceiver<E, "mirror">;
  98. }
  99. /**
  100. * Allows you to setup a type-string utility which receives DOM containers
  101. * and returns either the same container type or a specific type.
  102. *
  103. * This uses a builder API, of which this is the first step.
  104. */
  105. type NodeSolver = <E extends NodeType = never>(...exclude: E[]) => NodeSolverWithExclusions<E>;
  106. /**
  107. * A selector API provided by using the `select()` utility
  108. */
  109. interface NodeSelector<T extends Container | "html"> {
  110. /**
  111. * The _type_ of the root node
  112. */
  113. type: () => NodeType;
  114. /**
  115. * Find the first `IElement` found using the selector string.
  116. *
  117. * Note: _by default the return type includes `null` if no results were found but if you
  118. * prefer to throw an error you can state the error text and an error will be thrown_
  119. */
  120. findFirst: <E extends string | undefined>(sel: string, errorMsg?: E) => undefined extends E ? IElement | null : IElement;
  121. /**
  122. * Find _all_ `IElement` results returned from the Selection query
  123. */
  124. findAll: <S extends string | undefined>(sel: S) => IElement[];
  125. /**
  126. * Appends one or more elements to the root selected node
  127. */
  128. append: (content?: (IText | IElement | HTML | undefined) | (IText | IElement | HTML | undefined)[]) => NodeSelector<T>;
  129. /**
  130. * Allows the injection of a callback which will be used to mutate on the first `IElement` node
  131. * which matches the first
  132. */
  133. update: <E extends string | boolean>(sel?: string, errorMsg?: E) => <CB extends UpdateCallback>(cb: CB) => NodeSelector<T>;
  134. /**
  135. * Provides a way to inject an update callback which will be applied to all IElement nodes
  136. * which meet the selector query. If no query is provided, then this will be all `IElement`
  137. * children of the root node.
  138. */
  139. updateAll: <S extends string | undefined>(sel?: S) => <CB extends UpdateCallback>(cb: CB) => NodeSelector<T>;
  140. /**
  141. * Map over all selected items and transform them as needed; results are returned to
  142. * caller (exiting the selection API) and operation does not mutate the parent
  143. * selected DOM tree.
  144. */
  145. mapAll: <S extends string | undefined>(selection?: S) => <O>(mutate: MapCallback<IElement, O>) => O[];
  146. /**
  147. * Filters out all nodes which match the DOM query selector and returns the
  148. * selector API
  149. */
  150. filterAll: <S extends string>(selection: S) => NodeSelector<T>;
  151. /**
  152. * Wraps the current _selection_ with an Element or Fragment.
  153. *
  154. * You may optionally add a prefix to the error message thrown if the
  155. * wrapper does not represent a valid block/wrapper element.
  156. */
  157. wrap: <C extends IElement | string>(wrapper: C, errMsg?: string) => NodeSelector<T>;
  158. /**
  159. * Returns the root node with all mutations included
  160. */
  161. toContainer: () => undefined extends T ? Fragment : T extends "html" ? string : T;
  162. }
  163. type SetAttribute<T extends string> = (value: string) => <N extends Container | HTML>(node: N) => N;
  164. type SetAttributeTo<_T extends string, _V extends string> = <N extends Container | HTML>(node: N) => N;
  165. declare const setAttribute: <T extends string>(attr: T) => SetAttribute<T>;
  166. declare const getAttribute: <T extends string>(attr: T) => GetAttribute<T>;
  167. /**
  168. * Provides the classes defined on a given container's top level
  169. * element as an array of strings
  170. */
  171. declare const getClassList: (container: Container | HTML | null) => string[];
  172. type RemoveClass<R extends string | string[]> = <C extends DocRoot | IElement | HTML>(container: C) => C;
  173. /**
  174. * Removes a class from the top level node of a container's body.
  175. *
  176. * Note: if the class wasn't present then no change is performed
  177. */
  178. declare const removeClass: <R extends string | string[]>(remove: R) => RemoveClass<R>;
  179. type AddClass<A extends string[] | string[][]> = <C extends DocRoot | IElement | HTML>(container: C) => C;
  180. /**
  181. * Adds a class to the top level node of a document's body.
  182. */
  183. declare const addClass: <A extends string[] | string[][]>(...add: A) => AddClass<A>;
  184. declare const addVueEvent: (event: keyof Events, value: string) => <T extends string | happy_dom.IElement>(el: T) => T;
  185. type Filter = string | RegExp;
  186. type FilterCallback = (removed: string[]) => void;
  187. type FiltersWithCallback = [FilterCallback, ...Filter[]];
  188. /**
  189. * Filters classes out from a given element using _filters_, where a filter:
  190. *
  191. * - string - when a string it will compare for a direct match
  192. * - RegExp - will run the RegExp's `test(class)` method
  193. *
  194. * Optionally you may pass in a callback function as the first parameter in the
  195. * the list and this callback will be then called with all filtered properties
  196. * passed to it. This is useful for creating desirable side-effects like _moving_
  197. * the classes to some other DOM element (for instance).
  198. */
  199. declare const filterClasses: <A extends Filter[] | [FilterCallback, ...Filter[]]>(...args: A) => <D extends string | happy_dom.IElement | DocRoot>(doc: D) => D;
  200. /**
  201. * Checks whether a given node has a parent reference
  202. */
  203. declare const hasParentElement: (node: ContainerOrHtml) => boolean;
  204. /**
  205. * Get's the parent element of a given node or returns `null` if not
  206. * present.
  207. */
  208. declare const getParent: (node: ContainerOrHtml) => happy_dom.IElement | null;
  209. /**
  210. * Converts an HTML string into a Happy DOM document tree
  211. */
  212. declare function createDocument(body: string, head?: string): HappyDoc;
  213. type FragmentFrom<_T extends Container | "html"> = Fragment;
  214. declare function createFragment<C extends Container | HTML>(content?: C): FragmentFrom<C extends string ? "html" : Fragment>;
  215. /**
  216. * Creates a DOM Node which will be either an `IElement` or `IText` node
  217. * based on the content passed in.
  218. */
  219. declare const createNode: (node: Container | string) => IElement | IText;
  220. /**
  221. * Creates a IText Node
  222. */
  223. declare function createTextNode(text?: string): IText;
  224. declare function createCommentNode(comment?: string): IComment;
  225. /**
  226. * Creates an element node and can preserve parent relationship if known
  227. */
  228. declare const createElement: (el: Container | HTML, parent?: IElement) => IElement;
  229. interface CssVariable {
  230. prop: string;
  231. value: string | number | boolean;
  232. }
  233. type ClassDefn = Record<string, string | boolean | number>;
  234. type MultiClassDefn = [selector: string, keyValues: ClassDefn][];
  235. interface ClassApi {
  236. /** add a child selector */
  237. addChild: (selector: string, defn: ClassDefn) => ClassApi;
  238. /** add CSS prop/values */
  239. addProps: (defn: ClassDefn) => ClassApi;
  240. }
  241. interface InlineStyle {
  242. /** add a single CSS variable (at a time); the CSS scope will ':root' unless specified */
  243. addCssVariable: (prop: string, value: string | number | boolean, scope?: string) => InlineStyle;
  244. addClassDefinition: (selection: string, cb: (api: ClassApi) => void) => InlineStyle;
  245. addCssVariables: (dictionary: Record<string, string>, scope?: string) => InlineStyle;
  246. convertToVueStyleBlock: (lang: "css" | "scss", scoped: boolean) => InlineStyle;
  247. finish: () => IElement;
  248. }
  249. /**
  250. * Creates a new `<style>...</style>` node and provides a simple API surface to allow
  251. * populating the contents with inline CSS content
  252. */
  253. declare const createInlineStyle: <T extends string = "text/css">(type?: T) => InlineStyle;
  254. declare const describeNode: (node: Container | HTML | null | UpdateSignature | any[]) => string;
  255. declare const inspect: <T extends boolean>(item?: unknown, toJSON?: T) => false extends T ? Record<string, any> : string;
  256. declare const tree: (node: Container | HTML) => Tree;
  257. /**
  258. * Allows various content-types to be wrapped into a single
  259. * Fragment which contains each element as a sibling
  260. */
  261. declare const siblings: <C extends UpdateSignature | ContainerOrHtml[]>(...content: C) => [...C] extends infer T ? T extends [...C] ? T extends UpdateSignature ? false | happy_dom.IElement : happy_dom.DocumentFragment : never : never;
  262. interface StackLine {
  263. fn: string | undefined;
  264. line: number | undefined;
  265. file: string | undefined;
  266. }
  267. declare class HappyMishap extends Error {
  268. name: string;
  269. readonly kind: "HappyWrapper";
  270. trace: string[];
  271. readonly line: number | null;
  272. readonly fn: string;
  273. readonly file: string;
  274. readonly structuredStack: StackLine[];
  275. toJSON(): {
  276. name: string;
  277. message: string;
  278. };
  279. toString(): {
  280. name: string;
  281. message: string;
  282. };
  283. constructor(message: string, options?: {
  284. error?: unknown;
  285. inspect?: unknown;
  286. name?: string;
  287. });
  288. }
  289. /**
  290. * converts a IHTMLCollection or a INodeList to an array
  291. */
  292. declare const getChildren: (el: Container) => (IElement | IText)[];
  293. declare const getChildElements: (el: Container) => IElement[];
  294. /**
  295. * Extracts a node from a DOM tree; is designed to be used with `update/updateAll()`
  296. * and the `updateChildren()` utilities. It can remove a set of elements as well as retain the extracted elements in
  297. * an array of nodes.
  298. * ```
  299. * const memory = []
  300. * domTree = select(domTree)
  301. * .updateAll('.bad-juju')(extract(memory))
  302. * .toContainer()
  303. * ```
  304. */
  305. declare const extract: <M extends happy_dom.IElement | happy_dom.IText | undefined>(memory?: M[] | undefined) => <T extends happy_dom.IElement extends M ? happy_dom.IElement : happy_dom.IElement | happy_dom.IText>(node: T) => false;
  306. /**
  307. * **placeholder**
  308. *
  309. * Very similar to the `extract()` utility where it allows certain elements to be
  310. * removed from the DOM tree and stored in a separate variable but in this case
  311. * rather then leaving _nothing_ in it's place we can instead leave a _placeholder_ node.
  312. *
  313. * If you want to define what this placeholder node should look like you may --
  314. * by setting the optional `placeholder` element -- but by default you will get:
  315. *
  316. * ```html
  317. * <placeholder></placeholder>
  318. * ```
  319. *
  320. * But all classes on removed element will be retained.
  321. */
  322. declare const placeholder: <M extends happy_dom.IElement | happy_dom.IText | undefined>(memory?: M[] | undefined, placeholder?: IElement) => <T extends happy_dom.IElement>(node: T) => IElement;
  323. /**
  324. * Replaces an existing element with a brand new one while preserving the element's
  325. * relationship to the parent node (if one exists).
  326. */
  327. declare const replaceElement: (newElement: IElement | HTML) => (oldElement: IElement) => IElement;
  328. /**
  329. * Appends one or more nodes to a parent container
  330. */
  331. declare const append: <N extends ContainerOrHtml[]>(...nodes: N | N[]) => <P extends UpdateSignature | ContainerOrHtml>(parent: P) => P extends any[] ? happy_dom.IElement : P;
  332. /**
  333. * A _partially applied_ instance of the `into()` utility; currently waiting
  334. * for child/children nodes.
  335. *
  336. * Note: the return type of this function is the Parent node (in whatever)
  337. * container type was passed in. However, child element(s) being wrapped which
  338. * had reference to a parent node, will have their parent node updated to
  339. * point now to the new parent node instead. This is important for proper
  340. * mutation when using the update/updateAll() utilities.
  341. */
  342. type IntoChildren<P extends DocRoot | IElement | HTML | undefined> = <A extends UpdateSignature | ContainerOrHtml[] | ContainerOrHtml[][]>(...args: A) => A extends UpdateSignature ? IElement | false : undefined extends P ? Fragment : P;
  343. /**
  344. * A higher order function which starts by receiving a _wrapper_ component
  345. * and then is fully applied when the child nodes are passed in.
  346. *
  347. * This is the _inverse_ of the **wrap()** utility.
  348. *
  349. * ```ts
  350. * const sandwich = into(bread)(peanut, butter, jelly)
  351. * ```
  352. */
  353. declare const into: <P extends string | happy_dom.IElement | DocRoot | undefined>(parent?: P | undefined) => IntoChildren<P>;
  354. type ChangeTagNameTo<T extends string> = <E extends [IElement | HTML | HappyDoc | Fragment] | UpdateSignature>(...el: E) => E extends UpdateSignature ? IElement : E;
  355. /**
  356. * Changes the tag name for the top level container element passed in
  357. * while preserving the parent node relationship.
  358. * ```ts
  359. * // <div>hi</div>
  360. * const html = changeTagName('div')(`<span>hi</span`)
  361. * ```
  362. */
  363. declare const changeTagName: <T extends string>(tagName: T) => ChangeTagNameTo<T>;
  364. /**
  365. * Prepends an `IElement` as the first child element of a host element.
  366. *
  367. * Note: you can use a string representation of an element
  368. * ```ts
  369. * const startWith = prepend('<h1>just do it</h1>')
  370. * const message: IElement = startWith(body)
  371. * ```
  372. */
  373. declare const prepend: (prepend: IElement | IText | HTML) => (el: IElement) => IElement;
  374. type Before<_T extends ContainerOrHtml> = <A extends [IElement | HTML | HappyDoc | Fragment] | UpdateSignature>(...afterNode: A) => A extends UpdateSignature ? IElement : A extends string ? string : A;
  375. /**
  376. * Inserts a set of Node or string objects in the children list of this Element's
  377. * parent, just before this Element. String objects are inserted as equivalent Text nodes.
  378. *
  379. * Note: you can use a string representation of an element
  380. * ```ts
  381. * const startWith = before('<h1>just do it</h1>')
  382. * const message: IElement = startWith(body)
  383. * ```
  384. */
  385. declare const before: <B extends ContainerOrHtml>(beforeNode: B) => Before<B>;
  386. declare const after: (afterNode: IElement | IText | HTML) => <B extends string | happy_dom.DocumentFragment | happy_dom.IElement>(beforeNode: B) => B;
  387. /**
  388. * A payload which is a partial application of the `wrap()` method and expects
  389. * to no w receive the parent element.
  390. */
  391. type ReadyForWrapper<_C extends UpdateSignature | ContainerOrHtml[]> = <P extends DocRoot | IElement | HTML | undefined>(parent: P) => undefined extends P ? Fragment : P;
  392. /**
  393. * **wrap**
  394. *
  395. * A higher order function which receives child elements which will need
  396. * to be wrapped and then fully applied when it receives the singular _wrapper_
  397. * container.
  398. *
  399. * This is the _inverse_ of the **into()** utility.
  400. *
  401. * ```ts
  402. * const sandwich = wrap(peanut, butter, jelly)(bread)
  403. * ```
  404. */
  405. declare const wrap: <C extends UpdateSignature | ContainerOrHtml[]>(...children: C) => ReadyForWrapper<C>;
  406. /**
  407. * Allows the _selection_ of HTML or a container type which is
  408. * then wrapped and a helpful query and mutation API is provided
  409. * to work with this DOM element.
  410. */
  411. declare const select: <D extends string | HappyDoc | happy_dom.DocumentFragment | happy_dom.IElement>(node: D) => NodeSelector<undefined extends D ? happy_dom.DocumentFragment : D extends string ? "html" : D>;
  412. declare function isHappyWrapperError(err: unknown): err is HappyMishap;
  413. declare const isInspectionTuple: (thing: unknown) => thing is InspectionTuple;
  414. declare function isDocument(dom: unknown): dom is HappyDoc;
  415. declare function isFragment(dom: unknown): dom is Fragment;
  416. declare const nodeStartsWithElement: <D extends DocRoot>(node: D) => boolean;
  417. declare const nodeEndsWithElement: <D extends DocRoot>(node: D) => boolean;
  418. declare const nodeBoundedByElements: <D extends DocRoot>(node: D) => boolean;
  419. /**
  420. * tests whether a given node has a singular Element as a child
  421. * of the given node
  422. */
  423. declare const hasSingularElement: <N extends DocRoot>(node: N) => boolean;
  424. declare function isElement(el: unknown): el is IElement;
  425. /**
  426. * determines if a Doc/DocFragment is a wrapper for only a singular
  427. * `IElement` node
  428. */
  429. declare const isElementLike: (container: unknown) => boolean;
  430. /**
  431. * Tests whether a doc type is wrapping only a text node
  432. */
  433. declare function isTextNodeLike(node: unknown): boolean;
  434. /**
  435. * Type guard which detects that the incoming calling signature matches
  436. * that of the `select()` utilities update/updateAll operation.
  437. */
  438. declare const isUpdateSignature: (args: unknown) => args is UpdateSignature;
  439. declare function isTextNode(node: unknown): node is IText;
  440. declare const isContainer: (thing: unknown) => thing is Container;
  441. /**
  442. * detects whether _all_ children of a give node are Elements
  443. */
  444. declare const nodeChildrenAllElements: <D extends DocRoot>(node: D) => boolean;
  445. /**
  446. * A DOM document originated from Happy DOM and renamed from
  447. * `Document` so as to avoid possible conflicts with Typescript's
  448. * built in `Document` type.
  449. */
  450. interface HappyDoc extends Document$1 {
  451. }
  452. type Document = Document$1;
  453. type DocumentFragment = DocumentFragment$1;
  454. type Fragment = DocumentFragment$1;
  455. /** An element in the DOM [happy-dom] */
  456. type IElement = IElement$1;
  457. type IComment = IComment$1;
  458. type IText = IText$1;
  459. type INode = INode$1;
  460. export { AddClass, Before, ChangeTagNameTo, ClassApi, ClassDefn, Container, ContainerOrHtml, CssVariable, DocRoot, Document, DocumentFragment, DomNode, Filter, FilterCallback, FiltersWithCallback, Fragment, FragmentFrom, GetAttribute, HTML, HappyDoc, HappyMishap, IComment, IElement, INode, IText, InlineStyle, InspectionTuple, IntoChildren, MapCallback, MultiClassDefn, NodeSelector, NodeSolver, NodeSolverDict, NodeSolverInput, NodeSolverReady, NodeSolverReceiver, NodeSolverWithExclusions, NodeType, NodeTypeInput, ReadyForWrapper, RemoveClass, SetAttribute, SetAttributeTo, StackLine, ToHtmlOptions, Tree, TreeSummary, UpdateCallback, UpdateCallback_Native, UpdateSignature, addClass, addVueEvent, after, append, before, changeTagName, clone, createCommentNode, createDocument, createElement, createFragment, createInlineStyle, createNode, createTextNode, describeNode, extract, filterClasses, getAttribute, getChildElements, getChildren, getClassList, getNodeType, getParent, hasParentElement, hasSingularElement, inspect, into, isContainer, isDocument, isElement, isElementLike, isFragment, isHappyWrapperError, isInspectionTuple, isTextNode, isTextNodeLike, isUpdateSignature, nodeBoundedByElements, nodeChildrenAllElements, nodeEndsWithElement, nodeStartsWithElement, nodeTypeLookup, placeholder, prepend, removeClass, replaceElement, safeString, select, setAttribute, siblings, solveForNodeType, toHtml, tree, wrap };