Skip to content

Commit 6c6a36e

Browse files
authored
Add JSDoc to CssNode (#65)
also removed 2 unused methods closes #63
1 parent bb77fad commit 6c6a36e

File tree

3 files changed

+87
-79
lines changed

3 files changed

+87
-79
lines changed

src/css-node.ts

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -187,47 +187,46 @@ export class CSSNode {
187187
this.index = index
188188
}
189189

190-
// Get the node index (for internal use)
191-
get_index(): number {
192-
return this.index
193-
}
194-
195-
// Get node type as number (for performance)
190+
/** Get node type as number (for performance) */
196191
get type(): CSSNodeType {
197192
return this.arena.get_type(this.index) as CSSNodeType
198193
}
199194

200-
// Get node type as human-readable string
195+
/** Get node type as human-readable string */
201196
get type_name(): TypeName {
202197
return TYPE_NAMES[this.type] || 'unknown'
203198
}
204199

205-
// Get the full text of this node from source
200+
/** Get the full text of this node from source */
206201
get text(): string {
207202
let start = this.arena.get_start_offset(this.index)
208203
let length = this.arena.get_length(this.index)
209204
return this.source.substring(start, start + length)
210205
}
211206

212-
// Get the "content" text (property name for declarations, at-rule name for at-rules, layer name for import layers)
207+
/** Get the "content" text (property name for declarations, at-rule name for at-rules, layer name for import layers) */
213208
get name(): string {
214209
let start = this.arena.get_content_start(this.index)
215210
let length = this.arena.get_content_length(this.index)
216211
if (length === 0) return ''
217212
return this.source.substring(start, start + length)
218213
}
219214

220-
// Alias for name (for declarations: "color" in "color: blue")
221-
// More semantic than `name` for declaration nodes
215+
/**
216+
* Alias for name (for declarations: "color" in "color: blue")
217+
* More semantic than `name` for declaration nodes
218+
*/
222219
get property(): string {
223220
return this.name
224221
}
225222

226-
// Get the value text (for declarations: "blue" in "color: blue")
227-
// For dimension/number nodes: returns the numeric value as a number
228-
// For string nodes: returns the string content without quotes
229-
// For URL nodes with quoted string: returns the string with quotes (consistent with STRING node)
230-
// For URL nodes with unquoted URL: returns the URL content without quotes
223+
/**
224+
* Get the value text (for declarations: "blue" in "color: blue")
225+
* For dimension/number nodes: returns the numeric value as a number
226+
* For string nodes: returns the string content without quotes
227+
* For URL nodes with quoted string: returns the string with quotes (consistent with STRING node)
228+
* For URL nodes with unquoted URL: returns the URL content without quotes
229+
*/
231230
get value(): string | number | null {
232231
let { type, text } = this
233232

@@ -270,6 +269,7 @@ export class CSSNode {
270269
return this.source.substring(start, start + length)
271270
}
272271

272+
/** Get the numeric value for NUMBER and DIMENSION nodes, or null for other node types */
273273
get value_as_number(): number | null {
274274
let text = this.text
275275
if (this.type === NUMBER) {
@@ -281,38 +281,44 @@ export class CSSNode {
281281
return null
282282
}
283283

284-
// Get the prelude text (for at-rules: "(min-width: 768px)" in "@media (min-width: 768px)")
285-
// This is an alias for `value` to make at-rule usage more semantic
284+
/**
285+
* Get the prelude text (for at-rules: "(min-width: 768px)" in "@media (min-width: 768px)")
286+
* This is an alias for `value` to make at-rule usage more semantic
287+
*/
286288
get prelude(): string | null {
287289
let val = this.value
288290
return typeof val === 'string' ? val : null
289291
}
290292

291-
// Get the attribute operator (for attribute selectors: =, ~=, |=, ^=, $=, *=)
292-
// Returns one of the ATTR_OPERATOR_* constants
293+
/**
294+
* Get the attribute operator (for attribute selectors: =, ~=, |=, ^=, $=, *=)
295+
* Returns one of the ATTR_OPERATOR_* constants
296+
*/
293297
get attr_operator(): number {
294298
return this.arena.get_attr_operator(this.index)
295299
}
296300

297-
// Get the attribute flags (for attribute selectors: i, s)
298-
// Returns one of the ATTR_FLAG_* constants
301+
/**
302+
* Get the attribute flags (for attribute selectors: i, s)
303+
* Returns one of the ATTR_FLAG_* constants
304+
*/
299305
get attr_flags(): number {
300306
return this.arena.get_attr_flags(this.index)
301307
}
302308

303-
// Get the unit for dimension nodes (e.g., "px" from "100px", "%" from "50%")
309+
/** Get the unit for dimension nodes (e.g., "px" from "100px", "%" from "50%") */
304310
get unit(): string | null {
305311
if (this.type !== DIMENSION) return null
306312
return parse_dimension(this.text).unit
307313
}
308314

309-
// Check if this declaration has !important
315+
/** Check if this declaration has !important */
310316
get is_important(): boolean | null {
311317
if (this.type !== DECLARATION) return null
312318
return this.arena.has_flag(this.index, FLAG_IMPORTANT)
313319
}
314320

315-
// Check if this has a vendor prefix (computed on-demand)
321+
/** Check if this has a vendor prefix (computed on-demand) */
316322
get is_vendor_prefixed(): boolean {
317323
switch (this.type) {
318324
case DECLARATION:
@@ -336,27 +342,27 @@ export class CSSNode {
336342
}
337343
}
338344

339-
// Check if this node has an error
345+
/** Check if this node has an error */
340346
get has_error(): boolean {
341347
return this.arena.has_flag(this.index, FLAG_HAS_ERROR)
342348
}
343349

344-
// Check if this at-rule has a prelude
350+
/** Check if this at-rule has a prelude */
345351
get has_prelude(): boolean {
346352
return this.arena.get_value_length(this.index) > 0
347353
}
348354

349-
// Check if this rule has a block { }
355+
/** Check if this rule has a block { } */
350356
get has_block(): boolean {
351357
return this.arena.has_flag(this.index, FLAG_HAS_BLOCK)
352358
}
353359

354-
// Check if this style rule has declarations
360+
/** Check if this style rule has declarations */
355361
get has_declarations(): boolean {
356362
return this.arena.has_flag(this.index, FLAG_HAS_DECLARATIONS)
357363
}
358364

359-
// Get the block node (for style rules and at-rules with blocks)
365+
/** Get the block node (for style rules and at-rules with blocks) */
360366
get block(): CSSNode | null {
361367
// For StyleRule: block is sibling after selector list
362368
if (this.type === STYLE_RULE) {
@@ -386,7 +392,7 @@ export class CSSNode {
386392
return null
387393
}
388394

389-
// Check if this block is empty (no declarations or rules, only comments allowed)
395+
/** Check if this block is empty (no declarations or rules, only comments allowed) */
390396
get is_empty(): boolean {
391397
// Only valid on block nodes
392398
if (this.type !== BLOCK) return false
@@ -404,7 +410,7 @@ export class CSSNode {
404410

405411
// --- Value Node Access (for declarations) ---
406412

407-
// Get array of parsed value nodes (for declarations only)
413+
/** Get array of parsed value nodes (for declarations only) */
408414
get values(): CSSNode[] {
409415
let result: CSSNode[] = []
410416
let child = this.first_child
@@ -415,67 +421,61 @@ export class CSSNode {
415421
return result
416422
}
417423

418-
// Get count of value nodes
419-
get value_count(): number {
420-
let count = 0
421-
let child = this.first_child
422-
while (child) {
423-
count++
424-
child = child.next_sibling
425-
}
426-
return count
427-
}
428-
429-
// Get start line number
424+
/** Get start line number */
430425
get line(): number {
431426
return this.arena.get_start_line(this.index)
432427
}
433428

434-
// Get start column number
429+
/** Get start column number */
435430
get column(): number {
436431
return this.arena.get_start_column(this.index)
437432
}
438433

439-
// Get start offset in source
434+
/** Get start offset in source */
440435
get start(): number {
441436
return this.arena.get_start_offset(this.index)
442437
}
443438

444-
// Get length in source
439+
/** Get length in source */
445440
get length(): number {
446441
return this.arena.get_length(this.index)
447442
}
448443

449-
// Get end offset in source
450-
// End is not stored, must be calculated
444+
/**
445+
* Get end offset in source
446+
* End is not stored, must be calculated
447+
*/
451448
get end(): number {
452449
return this.start + this.length
453450
}
454451

455452
// --- Tree Traversal ---
456453

457-
// Get first child node
454+
/** Get first child node */
458455
get first_child(): CSSNode | null {
459456
let child_index = this.arena.get_first_child(this.index)
460457
if (child_index === 0) return null
461458
return new CSSNode(this.arena, this.source, child_index)
462459
}
463460

464-
// Get next sibling node
461+
/** Get next sibling node */
465462
get next_sibling(): CSSNode | null {
466463
let sibling_index = this.arena.get_next_sibling(this.index)
467464
if (sibling_index === 0) return null
468465
return new CSSNode(this.arena, this.source, sibling_index)
469466
}
470467

468+
/** Check if this node has a next sibling */
471469
get has_next(): boolean {
472470
let sibling_index = this.arena.get_next_sibling(this.index)
473471
return sibling_index !== 0
474472
}
475473

476-
// Check if this node has children
477-
// For pseudo-class/pseudo-element functions, returns true if FLAG_HAS_PARENS is set
478-
// This allows formatters to distinguish :lang() from :hover
474+
/**
475+
* Check if this node has children
476+
* For pseudo-class/pseudo-element functions, returns true if FLAG_HAS_PARENS is set
477+
* This allows formatters to distinguish :lang() from :hover
478+
*/
479479
get has_children(): boolean {
480480
// For pseudo-class/pseudo-element nodes, check if they have function syntax
481481
if (this.type === PSEUDO_CLASS_SELECTOR || this.type === PSEUDO_ELEMENT_SELECTOR) {
@@ -488,7 +488,7 @@ export class CSSNode {
488488
return this.arena.has_children(this.index)
489489
}
490490

491-
// Get all children as an array
491+
/** Get all children as an array */
492492
get children(): CSSNode[] {
493493
let result: CSSNode[] = []
494494
let child = this.first_child
@@ -499,7 +499,7 @@ export class CSSNode {
499499
return result
500500
}
501501

502-
// Make CSSNode iterable over its children
502+
/** Make CSSNode iterable over its children */
503503
*[Symbol.iterator](): Iterator<CSSNode> {
504504
let child = this.first_child
505505
while (child) {
@@ -510,7 +510,7 @@ export class CSSNode {
510510

511511
// --- An+B Expression Helpers (for NODE_SELECTOR_NTH) ---
512512

513-
// Get the 'a' coefficient from An+B expression (e.g., "2n" from "2n+1", "odd" from "odd")
513+
/** Get the 'a' coefficient from An+B expression (e.g., "2n" from "2n+1", "odd" from "odd") */
514514
get nth_a(): string | null {
515515
if (this.type !== NTH_SELECTOR) return null
516516

@@ -520,7 +520,7 @@ export class CSSNode {
520520
return this.source.substring(start, start + len)
521521
}
522522

523-
// Get the 'b' coefficient from An+B expression (e.g., "+1" from "2n+1")
523+
/** Get the 'b' coefficient from An+B expression (e.g., "+1" from "2n+1") */
524524
get nth_b(): string | null {
525525
if (this.type !== NTH_SELECTOR) return null
526526

@@ -552,13 +552,13 @@ export class CSSNode {
552552

553553
// --- Pseudo-Class Nth-Of Helpers (for NODE_SELECTOR_NTH_OF) ---
554554

555-
// Get the An+B formula node from :nth-child(2n+1 of .foo)
555+
/** Get the An+B formula node from :nth-child(2n+1 of .foo) */
556556
get nth(): CSSNode | null {
557557
if (this.type !== NTH_OF_SELECTOR) return null
558558
return this.first_child // First child is always NODE_SELECTOR_NTH
559559
}
560560

561-
// Get the selector list from :nth-child(2n+1 of .foo)
561+
/** Get the selector list from :nth-child(2n+1 of .foo) */
562562
get selector(): CSSNode | null {
563563
if (this.type !== NTH_OF_SELECTOR) return null
564564
let first = this.first_child
@@ -567,8 +567,10 @@ export class CSSNode {
567567

568568
// --- Pseudo-Class Selector List Helper ---
569569

570-
// Get selector list from pseudo-class functions
571-
// Works for :is(.a), :not(.b), :has(.c), :where(.d), :nth-child(2n of .e)
570+
/**
571+
* Get selector list from pseudo-class functions
572+
* Works for :is(.a), :not(.b), :has(.c), :where(.d), :nth-child(2n of .e)
573+
*/
572574
get selector_list(): CSSNode | null {
573575
if (this.type !== PSEUDO_CLASS_SELECTOR) return null
574576

@@ -591,8 +593,10 @@ export class CSSNode {
591593

592594
// --- Compound Selector Helpers (for NODE_SELECTOR) ---
593595

594-
// Iterator over first compound selector parts (zero allocation)
595-
// Yields parts before the first combinator
596+
/**
597+
* Iterator over first compound selector parts (zero allocation)
598+
* Yields parts before the first combinator
599+
*/
596600
*compound_parts(): IterableIterator<CSSNode> {
597601
if (this.type !== SELECTOR) return
598602

@@ -604,8 +608,10 @@ export class CSSNode {
604608
}
605609
}
606610

607-
// Get first compound selector as array
608-
// Returns array of parts before first combinator
611+
/**
612+
* Get first compound selector as array
613+
* Returns array of parts before first combinator
614+
*/
609615
get first_compound(): CSSNode[] {
610616
if (this.type !== SELECTOR) return []
611617

@@ -619,8 +625,10 @@ export class CSSNode {
619625
return result
620626
}
621627

622-
// Split selector into compound selectors
623-
// Returns array of compound arrays split by combinators
628+
/**
629+
* Split selector into compound selectors
630+
* Returns array of compound arrays split by combinators
631+
*/
624632
get all_compounds(): CSSNode[][] {
625633
if (this.type !== SELECTOR) return []
626634

@@ -647,7 +655,7 @@ export class CSSNode {
647655
return compounds
648656
}
649657

650-
// Check if selector is compound (no combinators)
658+
/** Check if selector is compound (no combinators) */
651659
get is_compound(): boolean {
652660
if (this.type !== SELECTOR) return false
653661

@@ -659,7 +667,7 @@ export class CSSNode {
659667
return true
660668
}
661669

662-
// Get text of first compound selector (no node allocation)
670+
/** Get text of first compound selector (no node allocation) */
663671
get first_compound_text(): string {
664672
if (this.type !== SELECTOR) return ''
665673

0 commit comments

Comments
 (0)