Skip to content

Commit

Permalink
Expose tag and attribute names with the original case. (#190)
Browse files Browse the repository at this point in the history
* Expose tag and attribute names with the original case.

* Expose tag and attribute names with the original case through the FFI.

* Release 1.1.0.
  • Loading branch information
orium authored Jul 28, 2023
1 parent 8946bfc commit 56f17ce
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.1.0

### Added

- Added ability to get the tag and attribute names with the original casing.

## v1.0.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lol_html"
version = "1.0.1"
version = "1.1.0"
authors = ["Ivan Nikulin <[email protected], [email protected]>"]
license = "BSD-3-Clause"
description = "Streaming HTML rewriter/parser with CSS selector-based API"
Expand Down
2 changes: 1 addition & 1 deletion c-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lolhtml"
version = "1.0.1"
version = "1.1.0"
authors = ["Ivan Nikulin <[email protected]>", "Joshua Nelson <[email protected]>"]
edition = "2021"

Expand Down
9 changes: 9 additions & 0 deletions c-api/include/lol_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ void *lol_html_text_chunk_user_data_get(const lol_html_text_chunk_t *chunk);
// Returns the tag name of the element.
lol_html_str_t lol_html_element_tag_name_get(const lol_html_element_t *element);

// Returns the tag name of the element, preserving its case.
lol_html_str_t lol_html_element_tag_name_get_preserve_case(const lol_html_element_t *element);

// Sets the tag name of the element.
//
// Name should be a valid UTF8-string.
Expand Down Expand Up @@ -528,6 +531,9 @@ const lol_html_attribute_t *lol_html_attributes_iterator_next(
// Returns the attribute name.
lol_html_str_t lol_html_attribute_name_get(const lol_html_attribute_t *attribute);

// Returns the attribute name, preserving its case.
lol_html_str_t lol_html_attribute_name_get_preserve_case(const lol_html_attribute_t *attribute);

// Returns the attribute value.
lol_html_str_t lol_html_attribute_value_get(const lol_html_attribute_t *attribute);

Expand Down Expand Up @@ -746,6 +752,9 @@ void lol_html_end_tag_remove(lol_html_end_tag_t *end_tag);
// Returns the end tag name.
lol_html_str_t lol_html_end_tag_name_get(const lol_html_end_tag_t *end_tag);

// Returns the end tag name, preserving its case.
lol_html_str_t lol_html_end_tag_name_get_preserve_case(const lol_html_end_tag_t *end_tag);

// Sets the tag name of the end tag.
//
// Name should be a valid UTF8-string.
Expand Down
20 changes: 20 additions & 0 deletions c-api/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ pub extern "C" fn lol_html_element_tag_name_get(element: *const Element) -> Str
Str::new(element.tag_name())
}

#[no_mangle]
pub extern "C" fn lol_html_element_tag_name_get_preserve_case(element: *const Element) -> Str {
let element = to_ref!(element);

Str::new(element.tag_name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_element_tag_name_set(
element: *mut Element,
Expand Down Expand Up @@ -67,6 +74,13 @@ pub extern "C" fn lol_html_attribute_name_get(attribute: *const Attribute) -> St
Str::new(attribute.name())
}

#[no_mangle]
pub extern "C" fn lol_html_attribute_name_get_preserve_case(attribute: *const Attribute) -> Str {
let attribute = to_ref!(attribute);

Str::new(attribute.name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_attribute_value_get(attribute: *const Attribute) -> Str {
let attribute = to_ref!(attribute);
Expand Down Expand Up @@ -281,6 +295,12 @@ pub extern "C" fn lol_html_end_tag_name_get(end_tag: *mut EndTag) -> Str {
Str::new(tag.name())
}

#[no_mangle]
pub extern "C" fn lol_html_end_tag_name_get_preserve_case(end_tag: *mut EndTag) -> Str {
let tag = to_ref_mut!(end_tag);
Str::new(tag.name_preserve_case())
}

#[no_mangle]
pub extern "C" fn lol_html_end_tag_name_set(
end_tag: *mut EndTag,
Expand Down
2 changes: 1 addition & 1 deletion js-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lol-html-js-api"
version = "1.0.1"
version = "1.1.0"
authors = ["Ivan Nikulin <[email protected]>"]
edition = "2021"

Expand Down
6 changes: 6 additions & 0 deletions src/rewritable_units/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ impl<'r, 't> Element<'r, 't> {
self.start_tag.name()
}

/// Returns the tag name of the element, preserving its case.
#[inline]
pub fn tag_name_preserve_case(&self) -> String {
self.start_tag.name_preserve_case()
}

/// Sets the tag name of the element.
#[inline]
pub fn set_tag_name(&mut self, name: &str) -> Result<(), TagNameError> {
Expand Down
6 changes: 6 additions & 0 deletions src/rewritable_units/tokens/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl<'i> Attribute<'i> {
self.name.as_lowercase_string(self.encoding)
}

/// Returns the name of the attribute, preserving its case.
#[inline]
pub fn name_preserve_case(&self) -> String {
self.name.as_string(self.encoding)
}

/// Returns the value of the attribute.
#[inline]
pub fn value(&self) -> String {
Expand Down
7 changes: 7 additions & 0 deletions src/rewritable_units/tokens/end_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ impl<'i> EndTag<'i> {
})
}

/// Returns the name of the tag.
#[inline]
pub fn name(&self) -> String {
self.name.as_lowercase_string(self.encoding)
}

/// Returns the name of the tag, preserving its case.
#[inline]
pub fn name_preserve_case(&self) -> String {
self.name.as_string(self.encoding)
}

#[inline]
pub fn set_name(&mut self, name: Bytes<'static>) {
self.name = name;
Expand Down
7 changes: 7 additions & 0 deletions src/rewritable_units/tokens/start_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ impl<'i> StartTag<'i> {
self.encoding
}

/// Returns the name of the tag.
#[inline]
pub fn name(&self) -> String {
self.name.as_lowercase_string(self.encoding)
}

/// Returns the name of the tag, preserving its case.
#[inline]
pub fn name_preserve_case(&self) -> String {
self.name.as_string(self.encoding)
}

#[inline]
pub fn set_name(&mut self, name: Bytes<'static>) {
self.name = name;
Expand Down

0 comments on commit 56f17ce

Please sign in to comment.