Skip to content

Commit

Permalink
Merge pull request #69 from CloudCannon/fix/html-unescaping
Browse files Browse the repository at this point in the history
Stop Rosey unescaping escaped HTML
  • Loading branch information
bglw authored Dec 2, 2024
2 parents 6c3cfca + ee8606a commit 72b5dc8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Unreleased

* Fix issue where Rosey would unescape characters that were entity encoded in the source HTML.

## v2.3.0 (October 31, 2024)

* Added `data-rosey-ignore` attribute to ignore hrefs when rewriting pages.
Expand Down
29 changes: 29 additions & 0 deletions rosey/features/build/rosey-build-code-blocks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature: Rosey Build Code Blocks
Background:
Given I have the environment variables:
| ROSEY_SOURCE | dist/site |
| ROSEY_DEST | dist/translated_site |

Scenario: Rosey doesn't ruin innocent escaped code
Given I have a "dist/site/index.html" file with the content:
"""
<html>
<body>
<div data-rosey="seal">Kiss From A Rose</div>
<pre><code>&lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt;</code></pre>
</body>
</html>
"""
And I have a "rosey/locales/manuka.json" file with the content:
"""
{
"seal": "Pollen From A Tree"
}
"""
When I run my program with the flags:
| build |
Then I should see a selector '[data-rosey="seal"]' in "dist/translated_site/manuka/index.html" with the attributes:
| data-rosey | seal |
| innerText | Pollen From A Tree |
Then I should see a selector 'pre > code' in "dist/translated_site/manuka/index.html" with the attributes:
| innerText | <configuration><setting v='true'/></configuration> |
22 changes: 22 additions & 0 deletions rosey/features/generate/rosey-generate-code-blocks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Feature: Rosey Generate Code Blocks
Background:
Given I have the environment variables:
| ROSEY_SOURCE | dist/site |
| ROSEY_DEST | dist/translated_site |

Scenario: Rosey generates base.json files with escaped code
Given I have a "dist/site/index.html" file with the content:
"""
<html>
<body data-rosey-ns='home'>
<div data-rosey="seal">Kiss From A Rose</div>
<pre><code data-rosey="code">&lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt;</code></pre>
</body>
</html>
"""
When I run my program with the flags:
| generate |
Then I should see "rosey/base.json" containing the values:
| version | int:2 |
| keys.home:seal.original | Kiss From A Rose |
| keys.home:code.original | &lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt; |
11 changes: 11 additions & 0 deletions rosey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,14 @@ pub fn inline_templates(dom: &kuchiki::NodeRef) {
}
})
}

pub fn escape_source_text(dom: &kuchiki::NodeRef) {
dom.children().for_each(|node| {
if let Some(text_node) = node.as_text() {
text_node.replace_with(|old| old.replace('<', "&lt;").replace('>', "&gt;"));
} else {
node.descendants()
.for_each(|node| escape_source_text(&node));
}
});
}
1 change: 1 addition & 0 deletions rosey/src/runners/builder/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl<'a> RoseyPage<'a> {
) -> Self {
let dom = kuchiki::parse_html().one(content);
crate::inline_templates(&dom);
crate::escape_source_text(&dom);

RoseyPage {
dom,
Expand Down

0 comments on commit 72b5dc8

Please sign in to comment.