Skip to content

Commit 72b5dc8

Browse files
authored
Merge pull request #69 from CloudCannon/fix/html-unescaping
Stop Rosey unescaping escaped HTML
2 parents 6c3cfca + ee8606a commit 72b5dc8

5 files changed

Lines changed: 65 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
## Unreleased
1111

12+
* Fix issue where Rosey would unescape characters that were entity encoded in the source HTML.
13+
1214
## v2.3.0 (October 31, 2024)
1315

1416
* Added `data-rosey-ignore` attribute to ignore hrefs when rewriting pages.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Feature: Rosey Build Code Blocks
2+
Background:
3+
Given I have the environment variables:
4+
| ROSEY_SOURCE | dist/site |
5+
| ROSEY_DEST | dist/translated_site |
6+
7+
Scenario: Rosey doesn't ruin innocent escaped code
8+
Given I have a "dist/site/index.html" file with the content:
9+
"""
10+
<html>
11+
<body>
12+
<div data-rosey="seal">Kiss From A Rose</div>
13+
<pre><code>&lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt;</code></pre>
14+
</body>
15+
</html>
16+
"""
17+
And I have a "rosey/locales/manuka.json" file with the content:
18+
"""
19+
{
20+
"seal": "Pollen From A Tree"
21+
}
22+
"""
23+
When I run my program with the flags:
24+
| build |
25+
Then I should see a selector '[data-rosey="seal"]' in "dist/translated_site/manuka/index.html" with the attributes:
26+
| data-rosey | seal |
27+
| innerText | Pollen From A Tree |
28+
Then I should see a selector 'pre > code' in "dist/translated_site/manuka/index.html" with the attributes:
29+
| innerText | <configuration><setting v='true'/></configuration> |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Feature: Rosey Generate Code Blocks
2+
Background:
3+
Given I have the environment variables:
4+
| ROSEY_SOURCE | dist/site |
5+
| ROSEY_DEST | dist/translated_site |
6+
7+
Scenario: Rosey generates base.json files with escaped code
8+
Given I have a "dist/site/index.html" file with the content:
9+
"""
10+
<html>
11+
<body data-rosey-ns='home'>
12+
<div data-rosey="seal">Kiss From A Rose</div>
13+
<pre><code data-rosey="code">&lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt;</code></pre>
14+
</body>
15+
</html>
16+
"""
17+
When I run my program with the flags:
18+
| generate |
19+
Then I should see "rosey/base.json" containing the values:
20+
| version | int:2 |
21+
| keys.home:seal.original | Kiss From A Rose |
22+
| keys.home:code.original | &lt;configuration&gt;&lt;setting v='true'/&gt;&lt;/configuration&gt; |

rosey/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,14 @@ pub fn inline_templates(dom: &kuchiki::NodeRef) {
342342
}
343343
})
344344
}
345+
346+
pub fn escape_source_text(dom: &kuchiki::NodeRef) {
347+
dom.children().for_each(|node| {
348+
if let Some(text_node) = node.as_text() {
349+
text_node.replace_with(|old| old.replace('<', "&lt;").replace('>', "&gt;"));
350+
} else {
351+
node.descendants()
352+
.for_each(|node| escape_source_text(&node));
353+
}
354+
});
355+
}

rosey/src/runners/builder/html.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<'a> RoseyPage<'a> {
246246
) -> Self {
247247
let dom = kuchiki::parse_html().one(content);
248248
crate::inline_templates(&dom);
249+
crate::escape_source_text(&dom);
249250

250251
RoseyPage {
251252
dom,

0 commit comments

Comments
 (0)