11/**
2- * @typedef {import('hast-util-is-element').TestFunctionAnything } TestFunctionAnything
3- * @typedef {import('hast').Content } Content
2+ * @typedef {import('hast-util-is-element').TestFunction } TestFunction
3+ * @typedef {import('hast').Nodes } Nodes
4+ * @typedef {import('hast').Parents } Parents
45 * @typedef {import('hast').Text } Text
56 * @typedef {import('hast').Comment } Comment
6- * @typedef {import('hast').Root } Root
77 * @typedef {import('hast').Element } Element
88 */
99
1010/**
11- * @typedef {Content | Root } Node
12- * Any node.
13- *
14- * @typedef {Extract<Node, import('unist').Parent> } Parent
15- * Any parent.
16- *
1711 * @typedef {'normal' | 'pre' | 'nowrap' | 'pre-wrap' } Whitespace
1812 * Valid and useful whitespace values (from CSS).
1913 *
@@ -59,7 +53,7 @@ const searchTabOrSpaces = /[\t ]+/g
5953
6054const br = convertElement ( 'br' )
6155const p = convertElement ( 'p' )
62- const cell = convertElement ( [ 'th' , 'td' ] )
56+ const cell = convertElement ( isCell )
6357const row = convertElement ( 'tr' )
6458
6559// Note that we don’t need to include void elements here as they don’t have text.
@@ -151,7 +145,7 @@ const blockOrCaption = convertElement([
151145 * with Chinese, Japanese, or Yi writing systems
152146 * * replaced elements (such as `audio`) are treated like non-replaced elements
153147 *
154- * @param {Node } tree
148+ * @param {Nodes } tree
155149 * Tree to turn into text.
156150 * @param {Options } [options]
157151 * Configuration (optional).
@@ -255,8 +249,8 @@ export function toText(tree, options = {}) {
255249/**
256250 * <https://html.spec.whatwg.org/#inner-text-collection-steps>
257251 *
258- * @param {Node } node
259- * @param {Parent } parent
252+ * @param {Nodes } node
253+ * @param {Parents } parent
260254 * @param {CollectionInfo } info
261255 * @returns {Array<string | BreakNumber> }
262256 */
@@ -279,7 +273,7 @@ function innerTextCollection(node, parent, info) {
279273 *
280274 * @param {Element } node
281275 * Element node.
282- * @param {Parent } parent
276+ * @param {Parents } parent
283277 * @param {CollectionInfo } info
284278 * Info on current collection.
285279 * @returns {Array<string | BreakNumber> }
@@ -330,7 +324,11 @@ function collectElement(node, parent, info) {
330324 // See: <https://html.spec.whatwg.org/#tables-2>
331325 // Note: needs further investigation as this does not account for implicit
332326 // rows.
333- else if ( row ( node ) && findAfter ( parent , node , row ) ) {
327+ else if (
328+ row ( node ) &&
329+ // @ts -expect-error: something up with types of parents.
330+ findAfter ( parent , node , row )
331+ ) {
334332 suffix = '\n'
335333 }
336334
@@ -369,7 +367,11 @@ function collectElement(node, parent, info) {
369367 // (tab) character to items.
370368 //
371369 // See: <https://html.spec.whatwg.org/#tables-2>
372- if ( cell ( node ) && findAfter ( parent , node , cell ) ) {
370+ if (
371+ cell ( node ) &&
372+ // @ts -expect-error: something up with types of parents.
373+ findAfter ( parent , node , cell )
374+ ) {
373375 items . push ( '\t' )
374376 }
375377
@@ -558,7 +560,7 @@ function trimAndCollapseSpacesAndTabs(value, breakBefore, breakAfter) {
558560 *
559561 * We don’t support void elements here (so `nobr wbr` -> `normal` is ignored).
560562 *
561- * @param {Node } node
563+ * @param {Nodes } node
562564 * Node (typically `Element`).
563565 * @param {CollectionInfo } info
564566 * Info on current collection.
@@ -599,12 +601,27 @@ function inferWhitespace(node, info) {
599601 return info . whitespace
600602}
601603
602- /** @type {TestFunctionAnything } */
604+ /**
605+ * @type {TestFunction }
606+ * @param {Element } node
607+ * @returns {node is {properties: {hidden: true}} }
608+ */
603609function hidden ( node ) {
604610 return Boolean ( ( node . properties || { } ) . hidden )
605611}
606612
607- /** @type {TestFunctionAnything } */
613+ /**
614+ * @type {TestFunction }
615+ * @param {Element } node
616+ * @returns {node is {tagName: 'td' | 'th'} }
617+ */
618+ function isCell ( node ) {
619+ return node . tagName === 'td' || node . tagName === 'th'
620+ }
621+
622+ /**
623+ * @type {TestFunction }
624+ */
608625function closedDialog ( node ) {
609626 return node . tagName === 'dialog' && ! ( node . properties || { } ) . open
610627}
0 commit comments