Skip to content

Commit

Permalink
Merge pull request #148 from adamreichold/simplify-concat
Browse files Browse the repository at this point in the history
Simplify handling of text node concatenation in tree sink
  • Loading branch information
cfvescovo authored Oct 9, 2023
2 parents e43a1f5 + 71dd0eb commit 1ec503f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 85 deletions.
42 changes: 19 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,37 @@ jobs:
name: Format code
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest stable
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
components: rustfmt
- uses: Swatinem/rust-cache@v1
- name: Check formatting
run: cargo fmt -- --check
clippy_check:
- run: cargo fmt -- --check

clippy:
name: Clippy check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: rustup component add clippy
- uses: actions-rs/clippy-check@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --all-targets --all-features -- --deny warnings

test:
name: Test code
runs-on: ubuntu-latest
strategy:
matrix:
rust_version: [stable, beta, nightly]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust_version}}
override: true
- uses: Swatinem/rust-cache@v1
- name: Update
run: cargo update
- name: Test
run: cargo test
- uses: Swatinem/rust-cache@v2
with:
key: ${{matrix.rust_version}}
- run: cargo update
- run: cargo test --all-features
86 changes: 50 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 29 additions & 26 deletions src/html/tree_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TreeSink for Html {

// Create an element.
//
// When creating a template element (name.ns.expanded() == expanded_name!(html "template")), an
// When creating a template element (name.expanded() == expanded_name!(html "template")), an
// associated document fragment called the "template contents" should also be created. Later
// calls to self.get_template_contents() with that given element return it.
fn create_element(
Expand All @@ -65,12 +65,14 @@ impl TreeSink for Html {
attrs: Vec<Attribute>,
_flags: ElementFlags,
) -> Self::Handle {
let mut node = self
.tree
.orphan(Node::Element(Element::new(name.clone(), attrs)));
if name.expanded() == expanded_name!(html "template") {
let fragment = name.expanded() == expanded_name!(html "template");

let mut node = self.tree.orphan(Node::Element(Element::new(name, attrs)));

if fragment {
node.append(Node::Fragment);
}

node.id()
}

Expand Down Expand Up @@ -115,17 +117,16 @@ impl TreeSink for Html {

NodeOrText::AppendText(text) => {
let text = make_tendril(text);
let can_concat = parent
.last_child()
.map_or(false, |mut n| n.value().is_text());

if can_concat {
let mut last_child = parent.last_child().unwrap();
match *last_child.value() {
Node::Text(ref mut t) => t.text.push_tendril(&text),
_ => unreachable!(),

let did_concat = parent.last_child().map_or(false, |mut n| match n.value() {
Node::Text(t) => {
t.text.push_tendril(&text);
true
}
} else {
_ => false,
});

if !did_concat {
parent.append(Node::Text(Text { text }));
}
}
Expand Down Expand Up @@ -158,17 +159,19 @@ impl TreeSink for Html {

NodeOrText::AppendText(text) => {
let text = make_tendril(text);
let can_concat = sibling
.prev_sibling()
.map_or(false, |mut n| n.value().is_text());

if can_concat {
let mut prev_sibling = sibling.prev_sibling().unwrap();
match *prev_sibling.value() {
Node::Text(ref mut t) => t.text.push_tendril(&text),
_ => unreachable!(),
}
} else {

let did_concat =
sibling
.prev_sibling()
.map_or(false, |mut n| match n.value() {
Node::Text(t) => {
t.text.push_tendril(&text);
true
}
_ => false,
});

if !did_concat {
sibling.insert_before(Node::Text(Text { text }));
}
}
Expand Down

0 comments on commit 1ec503f

Please sign in to comment.