@khanacademy/perseus-linter 0.2.4 → 0.3.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.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +18 -0
- package/dist/es/index.js +277 -407
- package/dist/es/index.js.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +281 -398
- package/dist/index.js.flow +18 -2
- package/dist/index.js.map +1 -1
- package/dist/proptypes.d.ts +9 -0
- package/dist/proptypes.js.flow +17 -0
- package/dist/rule.d.ts +170 -0
- package/dist/rule.js.flow +86 -0
- package/dist/rules/absolute-url.d.ts +3 -0
- package/dist/rules/absolute-url.js.flow +9 -0
- package/dist/rules/all-rules.d.ts +2 -0
- package/dist/rules/all-rules.js.flow +9 -0
- package/dist/rules/blockquoted-math.d.ts +3 -0
- package/dist/rules/blockquoted-math.js.flow +9 -0
- package/dist/rules/blockquoted-widget.d.ts +3 -0
- package/dist/rules/blockquoted-widget.js.flow +9 -0
- package/dist/rules/double-spacing-after-terminal.d.ts +3 -0
- package/dist/rules/double-spacing-after-terminal.js.flow +9 -0
- package/dist/rules/extra-content-spacing.d.ts +3 -0
- package/dist/rules/extra-content-spacing.js.flow +9 -0
- package/dist/rules/heading-level-1.d.ts +3 -0
- package/dist/rules/heading-level-1.js.flow +9 -0
- package/dist/rules/heading-level-skip.d.ts +3 -0
- package/dist/rules/heading-level-skip.js.flow +9 -0
- package/dist/rules/heading-sentence-case.d.ts +3 -0
- package/dist/rules/heading-sentence-case.js.flow +9 -0
- package/dist/rules/heading-title-case.d.ts +3 -0
- package/dist/rules/heading-title-case.js.flow +9 -0
- package/dist/rules/image-alt-text.d.ts +3 -0
- package/dist/rules/image-alt-text.js.flow +9 -0
- package/dist/rules/image-in-table.d.ts +3 -0
- package/dist/rules/image-in-table.js.flow +9 -0
- package/dist/rules/image-spaces-around-urls.d.ts +3 -0
- package/dist/rules/image-spaces-around-urls.js.flow +9 -0
- package/dist/rules/image-widget.d.ts +3 -0
- package/dist/rules/image-widget.js.flow +9 -0
- package/dist/rules/link-click-here.d.ts +3 -0
- package/dist/rules/link-click-here.js.flow +9 -0
- package/dist/rules/lint-utils.d.ts +2 -0
- package/dist/rules/lint-utils.js.flow +8 -0
- package/dist/rules/long-paragraph.d.ts +3 -0
- package/dist/rules/long-paragraph.js.flow +9 -0
- package/dist/rules/math-adjacent.d.ts +3 -0
- package/dist/rules/math-adjacent.js.flow +9 -0
- package/dist/rules/math-align-extra-break.d.ts +3 -0
- package/dist/rules/math-align-extra-break.js.flow +9 -0
- package/dist/rules/math-align-linebreaks.d.ts +3 -0
- package/dist/rules/math-align-linebreaks.js.flow +9 -0
- package/dist/rules/math-empty.d.ts +3 -0
- package/dist/rules/math-empty.js.flow +9 -0
- package/dist/rules/math-font-size.d.ts +3 -0
- package/dist/rules/math-font-size.js.flow +9 -0
- package/dist/rules/math-frac.d.ts +3 -0
- package/dist/rules/math-frac.js.flow +9 -0
- package/dist/rules/math-nested.d.ts +3 -0
- package/dist/rules/math-nested.js.flow +9 -0
- package/dist/rules/math-starts-with-space.d.ts +3 -0
- package/dist/rules/math-starts-with-space.js.flow +9 -0
- package/dist/rules/math-text-empty.d.ts +3 -0
- package/dist/rules/math-text-empty.js.flow +9 -0
- package/dist/rules/math-without-dollars.d.ts +3 -0
- package/dist/rules/math-without-dollars.js.flow +9 -0
- package/dist/rules/nested-lists.d.ts +3 -0
- package/dist/rules/nested-lists.js.flow +9 -0
- package/dist/rules/profanity.d.ts +3 -0
- package/dist/rules/profanity.js.flow +9 -0
- package/dist/rules/table-missing-cells.d.ts +3 -0
- package/dist/rules/table-missing-cells.js.flow +9 -0
- package/dist/rules/unbalanced-code-delimiters.d.ts +3 -0
- package/dist/rules/unbalanced-code-delimiters.js.flow +9 -0
- package/dist/rules/unescaped-dollar.d.ts +3 -0
- package/dist/rules/unescaped-dollar.js.flow +9 -0
- package/dist/rules/widget-in-table.d.ts +3 -0
- package/dist/rules/widget-in-table.js.flow +9 -0
- package/dist/selector.d.ts +108 -0
- package/dist/selector.js.flow +31 -0
- package/dist/tree-transformer.d.ts +205 -0
- package/dist/tree-transformer.js.flow +253 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js.flow +12 -0
- package/package.json +4 -4
- package/src/__tests__/{matcher_test.js → matcher.test.ts} +60 -60
- package/src/__tests__/{rule_test.js → rule.test.ts} +13 -5
- package/src/__tests__/{rules_test.js → rules.test.ts} +99 -39
- package/src/__tests__/{selector-parser_test.js → selector-parser.test.ts} +1 -2
- package/src/__tests__/{tree-transformer_test.js → tree-transformer.test.ts} +39 -41
- package/src/{index.js → index.ts} +21 -23
- package/src/{proptypes.js → proptypes.ts} +4 -14
- package/src/{rule.js → rule.ts} +45 -38
- package/src/rules/{absolute-url.js → absolute-url.ts} +4 -5
- package/src/rules/all-rules.ts +71 -0
- package/src/rules/{blockquoted-math.js → blockquoted-math.ts} +3 -4
- package/src/rules/{blockquoted-widget.js → blockquoted-widget.ts} +3 -4
- package/src/rules/{double-spacing-after-terminal.js → double-spacing-after-terminal.ts} +3 -4
- package/src/rules/{extra-content-spacing.js → extra-content-spacing.ts} +3 -4
- package/src/rules/{heading-level-1.js → heading-level-1.ts} +3 -4
- package/src/rules/{heading-level-skip.js → heading-level-skip.ts} +3 -4
- package/src/rules/{heading-sentence-case.js → heading-sentence-case.ts} +3 -4
- package/src/rules/{heading-title-case.js → heading-title-case.ts} +11 -6
- package/src/rules/{image-alt-text.js → image-alt-text.ts} +3 -4
- package/src/rules/{image-in-table.js → image-in-table.ts} +3 -4
- package/src/rules/{image-spaces-around-urls.js → image-spaces-around-urls.ts} +3 -4
- package/src/rules/{image-widget.js → image-widget.ts} +3 -4
- package/src/rules/{link-click-here.js → link-click-here.ts} +3 -4
- package/src/rules/{lint-utils.js → lint-utils.ts} +1 -2
- package/src/rules/{long-paragraph.js → long-paragraph.ts} +3 -4
- package/src/rules/{math-adjacent.js → math-adjacent.ts} +3 -4
- package/src/rules/{math-align-extra-break.js → math-align-extra-break.ts} +3 -4
- package/src/rules/{math-align-linebreaks.js → math-align-linebreaks.ts} +3 -4
- package/src/rules/{math-empty.js → math-empty.ts} +3 -4
- package/src/rules/{math-font-size.js → math-font-size.ts} +3 -4
- package/src/rules/{math-frac.js → math-frac.ts} +3 -4
- package/src/rules/{math-nested.js → math-nested.ts} +3 -4
- package/src/rules/{math-starts-with-space.js → math-starts-with-space.ts} +3 -4
- package/src/rules/{math-text-empty.js → math-text-empty.ts} +3 -4
- package/src/rules/{math-without-dollars.js → math-without-dollars.ts} +3 -4
- package/src/rules/{nested-lists.js → nested-lists.ts} +3 -4
- package/src/rules/{profanity.js → profanity.ts} +3 -4
- package/src/rules/{table-missing-cells.js → table-missing-cells.ts} +3 -4
- package/src/rules/{unbalanced-code-delimiters.js → unbalanced-code-delimiters.ts} +3 -4
- package/src/rules/{unescaped-dollar.js → unescaped-dollar.ts} +3 -4
- package/src/rules/{widget-in-table.js → widget-in-table.ts} +3 -4
- package/src/{selector.js → selector.ts} +12 -13
- package/src/{tree-transformer.js → tree-transformer.ts} +24 -24
- package/src/types.ts +7 -0
- package/tsconfig.json +12 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/rules/all-rules.js +0 -72
- package/src/types.js +0 -10
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Selector class implements a CSS-like system for matching nodes in a
|
|
3
|
+
* parse tree based on the structure of the tree. Create a Selector object by
|
|
4
|
+
* calling the static Selector.parse() method on a string that describes the
|
|
5
|
+
* tree structure you want to match. For example, if you want to find text
|
|
6
|
+
* nodes that are direct children of paragraph nodes that immediately follow
|
|
7
|
+
* heading nodes, you could create an appropriate selector like this:
|
|
8
|
+
*
|
|
9
|
+
* selector = Selector.parse("heading + paragraph > text");
|
|
10
|
+
*
|
|
11
|
+
* Recall from the TreeTransformer class, that we consider any object with a
|
|
12
|
+
* string-valued `type` property to be a tree node. The words "heading",
|
|
13
|
+
* "paragraph" and "text" in the selector string above specify node types and
|
|
14
|
+
* will match nodes in a parse tree that have `type` properties with those
|
|
15
|
+
* values.
|
|
16
|
+
*
|
|
17
|
+
* Selectors are designed for use during tree traversals done with the
|
|
18
|
+
* TreeTransformer traverse() method. To test whether the node currently being
|
|
19
|
+
* traversed matches a selector, simply pass the TraversalState object to the
|
|
20
|
+
* match() method of the Selector object. If the node does not match the
|
|
21
|
+
* selector, match() returns null. If it does match, then match() returns an
|
|
22
|
+
* array of nodes that match the selector. In the example above the first
|
|
23
|
+
* element of the array would be the node the heading node, the second would
|
|
24
|
+
* be the paragraph node that follows it, and the third would be the text node
|
|
25
|
+
* that is a child of the paragraph. The last element of a returned array of
|
|
26
|
+
* nodes is always equal to the current node of the tree traversal.
|
|
27
|
+
*
|
|
28
|
+
* Code that uses a selector might look like this:
|
|
29
|
+
*
|
|
30
|
+
* matchingNodes = selector.match(state);
|
|
31
|
+
* if (matchingNodes) {
|
|
32
|
+
* let heading = matchingNodes[0];
|
|
33
|
+
* let text = matchingNodes[2];
|
|
34
|
+
* // do something with those nodes
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* The Selector.parse() method recognizes a grammar that is similar to CSS
|
|
38
|
+
* selectors:
|
|
39
|
+
*
|
|
40
|
+
* selector := treeSelector (, treeSelector)*
|
|
41
|
+
*
|
|
42
|
+
* A selector is one or more comma-separated treeSelectors. A node matches
|
|
43
|
+
* the selector if it matches any of the treeSelectors.
|
|
44
|
+
*
|
|
45
|
+
* treeSelector := (treeSelector combinator)? nodeSelector
|
|
46
|
+
*
|
|
47
|
+
* A treeSelector is a nodeSelector optionally preceeded by a combinator
|
|
48
|
+
* and another tree selector. The tree selector matches if the current node
|
|
49
|
+
* matches the node selector and a sibling or ancestor (depending on the
|
|
50
|
+
* combinator) of the current node matches the optional treeSelector.
|
|
51
|
+
*
|
|
52
|
+
* combinator := ' ' | '>' | '+' | '~' // standard CSS3 combinators
|
|
53
|
+
*
|
|
54
|
+
* A combinator is a space or punctuation character that specifies the
|
|
55
|
+
* relationship between two nodeSelectors. A space between two
|
|
56
|
+
* nodeSelectors means that the first selector much match an ancestor of
|
|
57
|
+
* the node that matches the second selector. A '>' character means that
|
|
58
|
+
* the first selector must match the parent of the node matched by the
|
|
59
|
+
* second. The '~' combinator means that the first selector must match a
|
|
60
|
+
* previous sibling of the node matched by the second. And the '+' selector
|
|
61
|
+
* means that first selector must match the immediate previous sibling of
|
|
62
|
+
* the node that matched the second.
|
|
63
|
+
*
|
|
64
|
+
* nodeSelector := <IDENTIFIER> | '*'
|
|
65
|
+
*
|
|
66
|
+
* A nodeSelector is simply an identifier (a letter followed by any number
|
|
67
|
+
* of letters, digits, hypens, and underscores) or the wildcard asterisk
|
|
68
|
+
* character. A wildcard node selector matches any node. An identifier
|
|
69
|
+
* selector matches any node that has a `type` property whose value matches
|
|
70
|
+
* the identifier.
|
|
71
|
+
*
|
|
72
|
+
* If you call Selector.parse() on a string that does not match this grammar,
|
|
73
|
+
* it will throw an exception
|
|
74
|
+
*
|
|
75
|
+
* TODO(davidflanagan): it might be useful to allow more sophsticated node
|
|
76
|
+
* selector matching with attribute matches and pseudo-classes, like
|
|
77
|
+
* "heading[level=2]" or "paragraph:first-child"
|
|
78
|
+
*
|
|
79
|
+
* Implementation Note: this file exports a very simple Selector class but all
|
|
80
|
+
* the actual work is done in various internal classes. The Parser class
|
|
81
|
+
* parses the string representation of a selector into a parse tree that
|
|
82
|
+
* consists of instances of various subclasses of the Selector class. It is
|
|
83
|
+
* these subclasses that implement the selector matching logic, often
|
|
84
|
+
* depending on features of the TraversalState object from the TreeTransformer
|
|
85
|
+
* traversal.
|
|
86
|
+
*/
|
|
87
|
+
import type { TreeNode, TraversalState } from "./tree-transformer";
|
|
88
|
+
/**
|
|
89
|
+
* This is the base class for all Selector types. The key method that all
|
|
90
|
+
* selector subclasses must implement is match(). It takes a TraversalState
|
|
91
|
+
* object (from a TreeTransformer traversal) and tests whether the selector
|
|
92
|
+
* matches at the current node. See the comment at the start of this file for
|
|
93
|
+
* more details on the match() method.
|
|
94
|
+
*/
|
|
95
|
+
export default class Selector {
|
|
96
|
+
static parse(selectorText: string): Selector;
|
|
97
|
+
/**
|
|
98
|
+
* Return an array of the nodes that matched or null if no match.
|
|
99
|
+
* This is the base class so we just throw an exception. All Selector
|
|
100
|
+
* subclasses must provide an implementation of this method.
|
|
101
|
+
*/
|
|
102
|
+
match(state: TraversalState): ReadonlyArray<TreeNode> | null | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* Selector subclasses all define a toString() method primarily
|
|
105
|
+
* because it makes it easy to write parser tests.
|
|
106
|
+
*/
|
|
107
|
+
toString(): string;
|
|
108
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowtype definitions for data
|
|
3
|
+
* Generated by Flowgen from a Typescript Definition
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
|
+
* @flow
|
|
6
|
+
*/
|
|
7
|
+
import type { TreeNode, TraversalState } from "./tree-transformer";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This is the base class for all Selector types. The key method that all
|
|
11
|
+
* selector subclasses must implement is match(). It takes a TraversalState
|
|
12
|
+
* object (from a TreeTransformer traversal) and tests whether the selector
|
|
13
|
+
* matches at the current node. See the comment at the start of this file for
|
|
14
|
+
* more details on the match() method.
|
|
15
|
+
*/
|
|
16
|
+
declare export default class Selector {
|
|
17
|
+
static parse(selectorText: string): Selector;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Return an array of the nodes that matched or null if no match.
|
|
21
|
+
* This is the base class so we just throw an exception. All Selector
|
|
22
|
+
* subclasses must provide an implementation of this method.
|
|
23
|
+
*/
|
|
24
|
+
match(state: TraversalState): $ReadOnlyArray<TreeNode> | null | void;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Selector subclasses all define a toString() method primarily
|
|
28
|
+
* because it makes it easy to write parser tests.
|
|
29
|
+
*/
|
|
30
|
+
toString(): string;
|
|
31
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TreeTransformer is a class for traversing and transforming trees. Create a
|
|
3
|
+
* TreeTransformer by passing the root node of the tree to the
|
|
4
|
+
* constructor. Then traverse that tree by calling the traverse() method. The
|
|
5
|
+
* argument to traverse() is a callback function that will be called once for
|
|
6
|
+
* each node in the tree. This is a post-order depth-first traversal: the
|
|
7
|
+
* callback is not called on the a way down, but on the way back up. That is,
|
|
8
|
+
* the children of a node are traversed before the node itself is.
|
|
9
|
+
*
|
|
10
|
+
* The traversal callback function is passed three arguments, the node being
|
|
11
|
+
* traversed, a TraversalState object, and the concatentated text content of
|
|
12
|
+
* the node and all of its descendants. The TraversalState object is the most
|
|
13
|
+
* most interesting argument: it has methods for querying the ancestors and
|
|
14
|
+
* siblings of the node, and for deleting or replacing the node. These
|
|
15
|
+
* transformation methods are why this class is a tree transformer and not
|
|
16
|
+
* just a tree traverser.
|
|
17
|
+
*
|
|
18
|
+
* A typical tree traversal looks like this:
|
|
19
|
+
*
|
|
20
|
+
* new TreeTransformer(root).traverse((node, state, content) => {
|
|
21
|
+
* let parent = state.parent();
|
|
22
|
+
* let previous = state.previousSibling();
|
|
23
|
+
* // etc.
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* The traverse() method descends through nodes and arrays of nodes and calls
|
|
27
|
+
* the traverse callback on each node on the way back up to the root of the
|
|
28
|
+
* tree. (Note that it only calls the callback on the nodes themselves, not
|
|
29
|
+
* any arrays that contain nodes.) A node is loosely defined as any object
|
|
30
|
+
* with a string-valued `type` property. Objects that do not have a type
|
|
31
|
+
* property are assumed to not be part of the tree and are not traversed. When
|
|
32
|
+
* traversing an array, all elements of the array are examined, and any that
|
|
33
|
+
* are nodes or arrays are recursively traversed. When traversing a node, all
|
|
34
|
+
* properties of the object are examined and any node or array values are
|
|
35
|
+
* recursively traversed. In typical parse trees, the children of a node are
|
|
36
|
+
* in a `children` or `content` array, but this class is designed to handle
|
|
37
|
+
* more general trees. The Perseus markdown parser, for example, produces
|
|
38
|
+
* nodes of type "table" that have children in the `header` and `cells`
|
|
39
|
+
* properties.
|
|
40
|
+
*
|
|
41
|
+
* CAUTION: the traverse() method does not make any attempt to detect
|
|
42
|
+
* cycles. If you call it on a cyclic graph instead of a tree, it will cause
|
|
43
|
+
* infinite recursion (or, more likely, a stack overflow).
|
|
44
|
+
*
|
|
45
|
+
* TODO(davidflanagan): it probably wouldn't be hard to detect cycles: when
|
|
46
|
+
* pushing a new node onto the containers stack we could just check that it
|
|
47
|
+
* isn't already there.
|
|
48
|
+
*
|
|
49
|
+
* If a node has a text-valued `content` property, it is taken to be the
|
|
50
|
+
* plain-text content of the node. The traverse() method concatenates these
|
|
51
|
+
* content strings and passes them to the traversal callback for each
|
|
52
|
+
* node. This means that the callback has access the full text content of its
|
|
53
|
+
* node and all of the nodes descendants.
|
|
54
|
+
*
|
|
55
|
+
* See the TraversalState class for more information on what information and
|
|
56
|
+
* methods are available to the traversal callback.
|
|
57
|
+
**/
|
|
58
|
+
export type TreeNode = {
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
61
|
+
export type TraversalCallback = (node: TreeNode, state: TraversalState, content: string) => void;
|
|
62
|
+
export default class TreeTransformer {
|
|
63
|
+
root: TreeNode;
|
|
64
|
+
constructor(root: TreeNode);
|
|
65
|
+
static isNode(n: any): boolean;
|
|
66
|
+
static isTextNode(n: any): boolean;
|
|
67
|
+
traverse(f: TraversalCallback): void;
|
|
68
|
+
_traverse(n: TreeNode | Array<TreeNode>, state: TraversalState, f: TraversalCallback): string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* This class represents the state of a tree traversal. An instance is created
|
|
72
|
+
* by the traverse() method of the TreeTransformer class to maintain the state
|
|
73
|
+
* for that traversal, and the instance is passed to the traversal callback
|
|
74
|
+
* function for each node that is traversed. This class is not intended to be
|
|
75
|
+
* instantiated directly, but is exported so that its type can be used for
|
|
76
|
+
* Flow annotaions.
|
|
77
|
+
**/
|
|
78
|
+
export declare class TraversalState {
|
|
79
|
+
root: TreeNode;
|
|
80
|
+
_currentNode: TreeNode | null | undefined;
|
|
81
|
+
_containers: Stack<TreeNode | Array<TreeNode>>;
|
|
82
|
+
_indexes: Stack<string | number>;
|
|
83
|
+
_ancestors: Stack<TreeNode>;
|
|
84
|
+
constructor(root: TreeNode);
|
|
85
|
+
/**
|
|
86
|
+
* Return the current node in the traversal. Any time the traversal
|
|
87
|
+
* callback is called, this method will return the name value as the
|
|
88
|
+
* first argument to the callback.
|
|
89
|
+
*/
|
|
90
|
+
currentNode(): TreeNode;
|
|
91
|
+
/**
|
|
92
|
+
* Return the parent of the current node, if there is one, or null.
|
|
93
|
+
*/
|
|
94
|
+
parent(): TreeNode | null | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Return an array of ancestor nodes. The first element of this array is
|
|
97
|
+
* the same as this.parent() and the last element is the root node. If we
|
|
98
|
+
* are currently at the root node, the the returned array will be empty.
|
|
99
|
+
* This method makes a copy of the internal state, so modifications to the
|
|
100
|
+
* returned array have no effect on the traversal.
|
|
101
|
+
*/
|
|
102
|
+
ancestors(): ReadonlyArray<TreeNode>;
|
|
103
|
+
/**
|
|
104
|
+
* Return the next sibling of this node, if it has one, or null otherwise.
|
|
105
|
+
*/
|
|
106
|
+
nextSibling(): TreeNode | null | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* Return the previous sibling of this node, if it has one, or null
|
|
109
|
+
* otherwise.
|
|
110
|
+
*/
|
|
111
|
+
previousSibling(): TreeNode | null | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Remove the next sibling node (if there is one) from the tree. Returns
|
|
114
|
+
* the removed sibling or null. This method makes it easy to traverse a
|
|
115
|
+
* tree and concatenate adjacent text nodes into a single node.
|
|
116
|
+
*/
|
|
117
|
+
removeNextSibling(): TreeNode | null | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Replace the current node in the tree with the specified nodes. If no
|
|
120
|
+
* nodes are passed, this is a node deletion. If one node (or array) is
|
|
121
|
+
* passed, this is a 1-for-1 replacement. If more than one node is passed
|
|
122
|
+
* then this is a combination of deletion and insertion. The new node or
|
|
123
|
+
* nodes will not be traversed, so this method can safely be used to
|
|
124
|
+
* reparent the current node node beneath a new parent.
|
|
125
|
+
*
|
|
126
|
+
* This method throws an error if you attempt to replace the root node of
|
|
127
|
+
* the tree.
|
|
128
|
+
*/
|
|
129
|
+
replace(...replacements: ReadonlyArray<TreeNode>): void;
|
|
130
|
+
/**
|
|
131
|
+
* Returns true if the current node has a previous sibling and false
|
|
132
|
+
* otherwise. If this method returns false, then previousSibling() will
|
|
133
|
+
* return null, and goToPreviousSibling() will throw an error.
|
|
134
|
+
*/
|
|
135
|
+
hasPreviousSibling(): boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Modify this traversal state object to have the state it would have had
|
|
138
|
+
* when visiting the previous sibling. Note that you may want to use
|
|
139
|
+
* clone() to make a copy before modifying the state object like this.
|
|
140
|
+
* This mutator method is not typically used during ordinary tree
|
|
141
|
+
* traversals, but is used by the Selector class for matching multi-node
|
|
142
|
+
* selectors.
|
|
143
|
+
*/
|
|
144
|
+
goToPreviousSibling(): void;
|
|
145
|
+
/**
|
|
146
|
+
* Returns true if the current node has an ancestor and false otherwise.
|
|
147
|
+
* If this method returns false, then the parent() method will return
|
|
148
|
+
* null and goToParent() will throw an error
|
|
149
|
+
*/
|
|
150
|
+
hasParent(): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Modify this object to look like it will look when we (later) visit the
|
|
153
|
+
* parent node of this node. You should not modify the instance passed to
|
|
154
|
+
* the tree traversal callback. Instead, make a copy with the clone()
|
|
155
|
+
* method and modify that. This mutator method is not typically used
|
|
156
|
+
* during ordinary tree traversals, but is used by the Selector class for
|
|
157
|
+
* matching multi-node selectors that involve parent and ancestor
|
|
158
|
+
* selectors.
|
|
159
|
+
*/
|
|
160
|
+
goToParent(): void;
|
|
161
|
+
/**
|
|
162
|
+
* Return a new TraversalState object that is a copy of this one.
|
|
163
|
+
* This method is useful in conjunction with the mutating methods
|
|
164
|
+
* goToParent() and goToPreviousSibling().
|
|
165
|
+
*/
|
|
166
|
+
clone(): TraversalState;
|
|
167
|
+
/**
|
|
168
|
+
* Returns true if this TraversalState object is equal to that
|
|
169
|
+
* TraversalState object, or false otherwise. This method exists
|
|
170
|
+
* primarily for use by our unit tests.
|
|
171
|
+
*/
|
|
172
|
+
equals(that: TraversalState): boolean;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* This class is an internal utility that just treats an array as a stack
|
|
176
|
+
* and gives us a top() method so we don't have to write expressions like
|
|
177
|
+
* `ancestors[ancestors.length-1]`. The values() method automatically
|
|
178
|
+
* copies the internal array so we don't have to worry about client code
|
|
179
|
+
* modifying our internal stacks. The use of this Stack abstraction makes
|
|
180
|
+
* the TraversalState class simpler in a number of places.
|
|
181
|
+
*/
|
|
182
|
+
declare class Stack<T> {
|
|
183
|
+
stack: Array<T>;
|
|
184
|
+
constructor(array?: ReadonlyArray<T> | null);
|
|
185
|
+
/** Push a value onto the stack. */
|
|
186
|
+
push(v: T): void;
|
|
187
|
+
/** Pop a value off of the stack. */
|
|
188
|
+
pop(): T;
|
|
189
|
+
/** Return the top value of the stack without popping it. */
|
|
190
|
+
top(): T;
|
|
191
|
+
/** Return a copy of the stack as an array */
|
|
192
|
+
values(): ReadonlyArray<T>;
|
|
193
|
+
/** Return the number of elements in the stack */
|
|
194
|
+
size(): number;
|
|
195
|
+
/** Return a string representation of the stack */
|
|
196
|
+
toString(): string;
|
|
197
|
+
/** Return a shallow copy of the stack */
|
|
198
|
+
clone(): Stack<T>;
|
|
199
|
+
/**
|
|
200
|
+
* Compare this stack to another and return true if the contents of
|
|
201
|
+
* the two arrays are the same.
|
|
202
|
+
*/
|
|
203
|
+
equals(that: Stack<T>): boolean;
|
|
204
|
+
}
|
|
205
|
+
export {};
|