diff --git a/README.md b/README.md index 77630ee..9f13335 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,22 @@ ### NodeRepr +#### Constructors + +##### new NodeRepr() + +> **new NodeRepr**(): [`NodeRepr`](README.md#noderepr) + +The node object, cann't be instantiated in javascript. So call the constructor will throw an error. + +###### Returns + +[`NodeRepr`](README.md#noderepr) + +###### Defined in + +[index.d.ts:137](https://github.com/umijs/niddle/blob/main/index.d.ts#L137) + #### Methods ##### append() @@ -30,7 +46,7 @@ The child node will be remove from its previous position. ###### Defined in -[index.d.ts:14](https://github.com/umijs/niddle/blob/main/index.d.ts#L14) +[index.d.ts:18](https://github.com/umijs/niddle/blob/main/index.d.ts#L18) ##### appendSequence() @@ -52,7 +68,7 @@ These children nodes will be remove from their previous position. ###### Defined in -[index.d.ts:20](https://github.com/umijs/niddle/blob/main/index.d.ts#L20) +[index.d.ts:25](https://github.com/umijs/niddle/blob/main/index.d.ts#L25) ##### clone() @@ -66,7 +82,7 @@ Clone this node to a new instance, not clone its descendants. ###### Defined in -[index.d.ts:90](https://github.com/umijs/niddle/blob/main/index.d.ts#L90) +[index.d.ts:142](https://github.com/umijs/niddle/blob/main/index.d.ts#L142) ##### cloneRecursive() @@ -80,21 +96,7 @@ Clone this node to a new instance, including its all descendants. ###### Defined in -[index.d.ts:92](https://github.com/umijs/niddle/blob/main/index.d.ts#L92) - -##### constructor() - -> **constructor**(): `void` - -The node object, cann't be instantiated in javascript. So call the constructor will throw an error - -###### Returns - -`void` - -###### Defined in - -[index.d.ts:96](https://github.com/umijs/niddle/blob/main/index.d.ts#L96) +[index.d.ts:147](https://github.com/umijs/niddle/blob/main/index.d.ts#L147) ##### getAttribute() @@ -114,7 +116,7 @@ Get attribute value of this node by given name. ###### Defined in -[index.d.ts:74](https://github.com/umijs/niddle/blob/main/index.d.ts#L74) +[index.d.ts:112](https://github.com/umijs/niddle/blob/main/index.d.ts#L112) ##### getAttributes() @@ -128,7 +130,7 @@ Get attributes K-V object of this node. ###### Defined in -[index.d.ts:76](https://github.com/umijs/niddle/blob/main/index.d.ts#L76) +[index.d.ts:117](https://github.com/umijs/niddle/blob/main/index.d.ts#L117) ##### getChildren() @@ -142,13 +144,13 @@ Get all children nodes of this node. ###### Defined in -[index.d.ts:72](https://github.com/umijs/niddle/blob/main/index.d.ts#L72) +[index.d.ts:107](https://github.com/umijs/niddle/blob/main/index.d.ts#L107) ##### innerHtml() > **innerHtml**(): `string` -Get the serialized html of this node, only including its all descendants;. +Get the serialized html of this node, only including its all descendants. ###### Returns @@ -156,7 +158,7 @@ Get the serialized html of this node, only including its all descendants;. ###### Defined in -[index.d.ts:80](https://github.com/umijs/niddle/blob/main/index.d.ts#L80) +[index.d.ts:127](https://github.com/umijs/niddle/blob/main/index.d.ts#L127) ##### insertAfter() @@ -178,7 +180,7 @@ The sibling node will be remove from its previous position. ###### Defined in -[index.d.ts:38](https://github.com/umijs/niddle/blob/main/index.d.ts#L38) +[index.d.ts:46](https://github.com/umijs/niddle/blob/main/index.d.ts#L46) ##### insertBefore() @@ -200,7 +202,7 @@ The sibling node will be remove from its previous position. ###### Defined in -[index.d.ts:50](https://github.com/umijs/niddle/blob/main/index.d.ts#L50) +[index.d.ts:60](https://github.com/umijs/niddle/blob/main/index.d.ts#L60) ##### insertSequenceAfter() @@ -222,7 +224,7 @@ These sibling nodes will be remove from their previous position. ###### Defined in -[index.d.ts:44](https://github.com/umijs/niddle/blob/main/index.d.ts#L44) +[index.d.ts:53](https://github.com/umijs/niddle/blob/main/index.d.ts#L53) ##### insertSequenceBefore() @@ -244,13 +246,13 @@ These sibling nodes will be remove from their previous position. ###### Defined in -[index.d.ts:56](https://github.com/umijs/niddle/blob/main/index.d.ts#L56) +[index.d.ts:67](https://github.com/umijs/niddle/blob/main/index.d.ts#L67) ##### outerHtml() > **outerHtml**(): `string` -Get the serialized html of this node, including its all descendants and itelf;. +Get the serialized html of this node, including its all descendants and itelf. ###### Returns @@ -258,7 +260,7 @@ Get the serialized html of this node, including its all descendants and itelf;. ###### Defined in -[index.d.ts:78](https://github.com/umijs/niddle/blob/main/index.d.ts#L78) +[index.d.ts:122](https://github.com/umijs/niddle/blob/main/index.d.ts#L122) ##### prepend() @@ -280,7 +282,7 @@ The child node will be remove from its previous position. ###### Defined in -[index.d.ts:26](https://github.com/umijs/niddle/blob/main/index.d.ts#L26) +[index.d.ts:32](https://github.com/umijs/niddle/blob/main/index.d.ts#L32) ##### prependSequence() @@ -302,7 +304,7 @@ These children nodes will be remove from their previous position. ###### Defined in -[index.d.ts:32](https://github.com/umijs/niddle/blob/main/index.d.ts#L32) +[index.d.ts:39](https://github.com/umijs/niddle/blob/main/index.d.ts#L39) ##### remove() @@ -316,13 +318,13 @@ Remove a node from its parent and siblings. Children are not affected. ###### Defined in -[index.d.ts:58](https://github.com/umijs/niddle/blob/main/index.d.ts#L58) +[index.d.ts:72](https://github.com/umijs/niddle/blob/main/index.d.ts#L72) ##### removeAllAttributes() > **removeAllAttributes**(): `void` -Remove all attributes of this node +Remove all attributes of this node. ###### Returns @@ -330,13 +332,13 @@ Remove all attributes of this node ###### Defined in -[index.d.ts:66](https://github.com/umijs/niddle/blob/main/index.d.ts#L66) +[index.d.ts:92](https://github.com/umijs/niddle/blob/main/index.d.ts#L92) ##### removeAttribute() > **removeAttribute**(`name`): `void` -Remove an attribute of this node by name +Remove an attribute of this node by name. ###### Parameters @@ -350,7 +352,7 @@ Remove an attribute of this node by name ###### Defined in -[index.d.ts:64](https://github.com/umijs/niddle/blob/main/index.d.ts#L64) +[index.d.ts:87](https://github.com/umijs/niddle/blob/main/index.d.ts#L87) ##### select() @@ -372,7 +374,7 @@ Select the the fist node that match the given css selector, like document.queryS ###### Defined in -[index.d.ts:68](https://github.com/umijs/niddle/blob/main/index.d.ts#L68) +[index.d.ts:97](https://github.com/umijs/niddle/blob/main/index.d.ts#L97) ###### select(selectors) @@ -390,7 +392,7 @@ Select the the fist node that match the given css selector, like document.queryS ###### Defined in -[index.d.ts:97](https://github.com/umijs/niddle/blob/main/index.d.ts#L97) +[index.d.ts:150](https://github.com/umijs/niddle/blob/main/index.d.ts#L150) ###### select(selectors) @@ -408,7 +410,7 @@ Select the the fist node that match the given css selector, like document.queryS ###### Defined in -[index.d.ts:98](https://github.com/umijs/niddle/blob/main/index.d.ts#L98) +[index.d.ts:151](https://github.com/umijs/niddle/blob/main/index.d.ts#L151) ###### select(selectors) @@ -426,7 +428,7 @@ Select the the fist node that match the given css selector, like document.queryS ###### Defined in -[index.d.ts:99](https://github.com/umijs/niddle/blob/main/index.d.ts#L99) +[index.d.ts:152](https://github.com/umijs/niddle/blob/main/index.d.ts#L152) ##### selectAll() @@ -446,7 +448,7 @@ Select all nodes that match the given css selector, like document.querySelectorA ###### Defined in -[index.d.ts:70](https://github.com/umijs/niddle/blob/main/index.d.ts#L70) +[index.d.ts:102](https://github.com/umijs/niddle/blob/main/index.d.ts#L102) ##### setAttribute() @@ -467,13 +469,13 @@ Assign an attribute K-V to this node ###### Defined in -[index.d.ts:60](https://github.com/umijs/niddle/blob/main/index.d.ts#L60) +[index.d.ts:77](https://github.com/umijs/niddle/blob/main/index.d.ts#L77) ##### setAttributes() > **setAttributes**(`attrs`): `void` -Assign attributes K-V object to this node +Assign attributes K-V object to this node. ###### Parameters @@ -487,13 +489,13 @@ Assign attributes K-V object to this node ###### Defined in -[index.d.ts:62](https://github.com/umijs/niddle/blob/main/index.d.ts#L62) +[index.d.ts:82](https://github.com/umijs/niddle/blob/main/index.d.ts#L82) ##### text() > **text**(): `string` -Get all text nodes content of this node, including its all descendants and itelf;. +Get all text nodes content of this node, including its all descendants and itelf. ###### Returns @@ -501,7 +503,7 @@ Get all text nodes content of this node, including its all descendants and itelf ###### Defined in -[index.d.ts:82](https://github.com/umijs/niddle/blob/main/index.d.ts#L82) +[index.d.ts:132](https://github.com/umijs/niddle/blob/main/index.d.ts#L132) ## Functions @@ -523,4 +525,4 @@ Parse string input to a html tree, return the root node. #### Defined in -[index.d.ts:7](https://github.com/umijs/niddle/blob/main/index.d.ts#L7) +[index.d.ts:10](https://github.com/umijs/niddle/blob/main/index.d.ts#L10) diff --git a/crates/niddle_napi/src/lib.rs b/crates/niddle_napi/src/lib.rs index 63d8923..6356f99 100644 --- a/crates/niddle_napi/src/lib.rs +++ b/crates/niddle_napi/src/lib.rs @@ -13,6 +13,7 @@ mod serializer; mod node_repr; /// Parse string input to a html tree, return the root node. +/// #[napi] pub fn parse(html: String) -> NodeRepr { let parser = parse_html(); diff --git a/crates/niddle_napi/src/node_repr/mod.rs b/crates/niddle_napi/src/node_repr/mod.rs index 9073748..54faa61 100644 --- a/crates/niddle_napi/src/node_repr/mod.rs +++ b/crates/niddle_napi/src/node_repr/mod.rs @@ -20,8 +20,7 @@ impl From for NodeRepr { #[napi] impl NodeRepr { - /// - /// @private + /// The node object, cann't be instantiated in javascript. So call the constructor will throw an error. /// #[napi(constructor, ts_return_type = "void")] pub fn constructor() { @@ -32,6 +31,7 @@ impl NodeRepr { #[napi] impl NodeRepr { /// Clone this node to a new instance, not clone its descendants. + /// #[napi(js_name = "clone")] pub fn clone_self_only(&self) -> NodeRepr { let new_node_ref = NodeRef::new(self.0.data().clone()); @@ -39,6 +39,7 @@ impl NodeRepr { } /// Clone this node to a new instance, including its all descendants. + /// #[napi] pub fn clone_recursive(&self) -> NodeRepr { NodeRepr::from(clone_node_ref_recursive(&self.0)) diff --git a/crates/niddle_napi/src/node_repr/modify.rs b/crates/niddle_napi/src/node_repr/modify.rs index ee7f79c..5ab5753 100644 --- a/crates/niddle_napi/src/node_repr/modify.rs +++ b/crates/niddle_napi/src/node_repr/modify.rs @@ -8,6 +8,7 @@ impl NodeRepr { /// Append a child node to this node, after existing children. /// /// The child node will be remove from its previous position. + /// #[napi] pub fn append(&self, new_child: &NodeRepr) { self.0.append(new_child.0.clone()) @@ -16,6 +17,7 @@ impl NodeRepr { /// Append some children nodes to this node by order, after existing children. /// /// These children nodes will be remove from their previous position. + /// #[napi] pub fn append_sequence(&self, new_children: Vec<&NodeRepr>) { new_children @@ -26,6 +28,7 @@ impl NodeRepr { /// Prepend a child node to this node, before existing children. /// /// The child node will be remove from its previous position. + /// #[napi] pub fn prepend(&self, new_child: &NodeRepr) { self.0.prepend(new_child.0.clone()) @@ -34,6 +37,7 @@ impl NodeRepr { /// Prepend some children nodes to this node by order, before existing children. /// /// These children nodes will be remove from their previous position. + /// #[napi] pub fn prepend_sequence(&self, new_children: Vec<&NodeRepr>) { if !new_children.is_empty() { @@ -49,6 +53,7 @@ impl NodeRepr { /// Insert a new sibling after this node. /// /// The sibling node will be remove from its previous position. + /// #[napi] pub fn insert_after(&self, new_sibling: &NodeRepr) { self.0.insert_after(new_sibling.0.clone()) @@ -57,6 +62,7 @@ impl NodeRepr { /// Insert some siblings after this node. /// /// These sibling nodes will be remove from their previous position. + /// #[napi] pub fn insert_sequence_after(&self, new_siblings: Vec<&NodeRepr>) { if !new_siblings.is_empty() { @@ -76,6 +82,7 @@ impl NodeRepr { /// Insert a new sibling before this node. /// /// The sibling node will be remove from its previous position. + /// #[napi] pub fn insert_before(&self, new_sibling: &NodeRepr) { self.0.insert_before(new_sibling.0.clone()) @@ -84,6 +91,7 @@ impl NodeRepr { /// Insert some siblings before this node. /// /// These sibling nodes will be remove from their previous position. + /// #[napi] pub fn insert_sequence_before(&self, new_siblings: Vec<&NodeRepr>) { if !new_siblings.is_empty() { @@ -97,12 +105,14 @@ impl NodeRepr { } /// Remove a node from its parent and siblings. Children are not affected. + /// #[napi] pub fn remove(&self) { self.0.detach() } /// Assign an attribute K-V to this node + /// #[napi] pub fn set_attribute(&self, name: String, value: String) { if let Some(ele) = self.0.as_element() { @@ -113,7 +123,8 @@ impl NodeRepr { } } - /// Assign attributes K-V object to this node + /// Assign attributes K-V object to this node. + /// #[napi] pub fn set_attributes(&self, attrs: IndexMap) { if let Some(ele) = self.0.as_element() { @@ -126,7 +137,8 @@ impl NodeRepr { } } - /// Remove an attribute of this node by name + /// Remove an attribute of this node by name. + /// #[napi] pub fn remove_attribute(&self, name: String) { if let Some(ele) = self.0.as_element() { @@ -134,7 +146,8 @@ impl NodeRepr { } } - /// Remove all attributes of this node + ///Remove all attributes of this node. + /// #[napi] pub fn remove_all_attributes(&self) { if let Some(ele) = self.0.as_element() { diff --git a/crates/niddle_napi/src/node_repr/query.rs b/crates/niddle_napi/src/node_repr/query.rs index c381a8c..e74708b 100644 --- a/crates/niddle_napi/src/node_repr/query.rs +++ b/crates/niddle_napi/src/node_repr/query.rs @@ -9,12 +9,14 @@ use super::NodeRepr; #[napi] impl NodeRepr { /// Select the the fist node that match the given css selector, like document.querySelector. + /// #[napi] pub fn select(&self, selectors: String) -> Option { self.0.select_first(&selectors).ok().map(Into::into) } /// Select all nodes that match the given css selector, like document.querySelectorAll. + /// #[napi] pub fn select_all(&self, selectors: String) -> Vec { self @@ -24,12 +26,14 @@ impl NodeRepr { } /// Get all children nodes of this node. + /// #[napi] pub fn get_children(&self) -> Vec { self.0.children().map(Into::into).collect() } /// Get attribute value of this node by given name. + /// #[napi] pub fn get_attribute(&self, name: String) -> Option { self @@ -39,6 +43,7 @@ impl NodeRepr { } /// Get attributes K-V object of this node. + /// #[napi] pub fn get_attributes(&self) -> IndexMap { self.0.as_element().map_or_else(IndexMap::new, |e| { @@ -54,7 +59,8 @@ impl NodeRepr { }) } - /// Get the serialized html of this node, including its all descendants and itelf;. + /// Get the serialized html of this node, including its all descendants and itelf. + /// #[napi] pub fn outer_html(&self) -> String { let mut u8_vec = Vec::new(); @@ -71,7 +77,8 @@ impl NodeRepr { unsafe { String::from_utf8_unchecked(u8_vec) } } - /// Get the serialized html of this node, only including its all descendants;. + /// Get the serialized html of this node, only including its all descendants. + /// #[napi] pub fn inner_html(&self) -> String { let mut buf = Vec::::new(); @@ -88,7 +95,8 @@ impl NodeRepr { unsafe { String::from_utf8_unchecked(buf) } } - /// Get all text nodes content of this node, including its all descendants and itelf;. + /// Get all text nodes content of this node, including its all descendants and itelf. + /// #[napi] pub fn text(&self) -> String { let mut buf = Vec::::new(); diff --git a/index.d.ts b/index.d.ts index e7fc0a6..f50bdca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,91 +3,151 @@ /* auto-generated by NAPI-RS */ -/** Parse string input to a html tree, return the root node. */ +/** + * Parse string input to a html tree, return the root node. + * + */ function parse(html: string): NodeRepr export declare class NodeRepr { /** * Append a child node to this node, after existing children. * * The child node will be remove from its previous position. + * */ append(newChild: NodeRepr): void /** * Append some children nodes to this node by order, after existing children. * * These children nodes will be remove from their previous position. + * */ appendSequence(newChildren: Array): void /** * Prepend a child node to this node, before existing children. * * The child node will be remove from its previous position. + * */ prepend(newChild: NodeRepr): void /** * Prepend some children nodes to this node by order, before existing children. * * These children nodes will be remove from their previous position. + * */ prependSequence(newChildren: Array): void /** * Insert a new sibling after this node. * * The sibling node will be remove from its previous position. + * */ insertAfter(newSibling: NodeRepr): void /** * Insert some siblings after this node. * * These sibling nodes will be remove from their previous position. + * */ insertSequenceAfter(newSiblings: Array): void /** * Insert a new sibling before this node. * * The sibling node will be remove from its previous position. + * */ insertBefore(newSibling: NodeRepr): void /** * Insert some siblings before this node. * * These sibling nodes will be remove from their previous position. + * */ insertSequenceBefore(newSiblings: Array): void - /** Remove a node from its parent and siblings. Children are not affected. */ + /** + * Remove a node from its parent and siblings. Children are not affected. + * + */ remove(): void - /** Assign an attribute K-V to this node */ + /** + * Assign an attribute K-V to this node + * + */ setAttribute(name: string, value: string): void - /** Assign attributes K-V object to this node */ + /** + * Assign attributes K-V object to this node. + * + */ setAttributes(attrs: Record): void - /** Remove an attribute of this node by name */ + /** + * Remove an attribute of this node by name. + * + */ removeAttribute(name: string): void - /** Remove all attributes of this node */ + /** + *Remove all attributes of this node. + * + */ removeAllAttributes(): void - /** Select the the fist node that match the given css selector, like document.querySelector. */ + /** + * Select the the fist node that match the given css selector, like document.querySelector. + * + */ select(selectors: string): NodeRepr | null - /** Select all nodes that match the given css selector, like document.querySelectorAll. */ + /** + * Select all nodes that match the given css selector, like document.querySelectorAll. + * + */ selectAll(selectors: string): Array - /** Get all children nodes of this node. */ + /** + * Get all children nodes of this node. + * + */ getChildren(): Array - /** Get attribute value of this node by given name. */ + /** + * Get attribute value of this node by given name. + * + */ getAttribute(name: string): string | null - /** Get attributes K-V object of this node. */ + /** + * Get attributes K-V object of this node. + * + */ getAttributes(): Record - /** Get the serialized html of this node, including its all descendants and itelf;. */ + /** + * Get the serialized html of this node, including its all descendants and itelf. + * + */ outerHtml(): string - /** Get the serialized html of this node, only including its all descendants;. */ + /** + * Get the serialized html of this node, only including its all descendants. + * + */ innerHtml(): string - /** Get all text nodes content of this node, including its all descendants and itelf;. */ - text(): string /** + * Get all text nodes content of this node, including its all descendants and itelf. * - * @private + */ + text(): string + /** + * The node object, cann't be instantiated in javascript. So call the constructor will throw an error. * */ constructor(): void - /** Clone this node to a new instance, not clone its descendants. */ + /** + * Clone this node to a new instance, not clone its descendants. + * + */ clone(): NodeRepr - /** Clone this node to a new instance, including its all descendants. */ + /** + * Clone this node to a new instance, including its all descendants. + * + */ cloneRecursive(): NodeRepr } +export declare interface NodeRepr { + select(selectors: "html"): NodeRepr; + select(selectors: "head"): NodeRepr; + select(selectors: "body"): NodeRepr; +} diff --git a/patch.d.ts b/patch.d.ts index 01d61cb..df5ee81 100644 --- a/patch.d.ts +++ b/patch.d.ts @@ -1,6 +1,4 @@ export declare interface NodeRepr { - /** The node object, cann't be instantiated in javascript. So call the constructor will throw an error */ - constructor(): void; select(selectors: "html"): NodeRepr; select(selectors: "head"): NodeRepr; select(selectors: "body"): NodeRepr;