diff --git a/README.en.md b/README.en.md
index 1472c86..3ac73a9 100644
--- a/README.en.md
+++ b/README.en.md
@@ -7,7 +7,7 @@
Turns your local git repositories and their GitHub issues into static HTML, and serves them through Cloudflare Pages + Access as a code browser only you can read.
- **Read every repo in one place**: your local repos listed by root, with the code highlighted
-- **HTML documents render in place**: a `.html` file in a repo is shown rendered, with its source on the same page. Its scripts run in a sandbox, cut off from codeview
+- **HTML documents render in place**: a `.html` file in a repo is shown rendered, with its source on the same page. Its scripts run in a sandbox, cut off from codeview. The frame grows to fit its content, so a phone scrolls the page as one
- **All issues in one place too**: open issues from every repo in a single list
- **Multiple GitHub accounts**: pull issues from separate accounts, such as personal and work, and keep them apart
- **Light on a phone**: static HTML that reads without JavaScript, so it opens quickly while you're away
diff --git a/README.md b/README.md
index 54de1a7..98310d3 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@
手元の git リポジトリ群と、その GitHub Issue を静的 HTML にまとめ、自分だけが読めるコードブラウザとして Cloudflare Pages + Access で配信する。
- **全リポを1か所で読める**: 手元のリポを置き場ごとに並べ、コードをハイライトして読める
-- **HTML の資料もそのまま読める**: リポに置いた `.html` は描画した結果を出し、ソースも同じページで読める。中のスクリプトは sandbox で codeview から切り離す
+- **HTML の資料もそのまま読める**: リポに置いた `.html` は描画した結果を出し、ソースも同じページで読める。中のスクリプトは sandbox で codeview から切り離す。iframe は中身の高さまで伸びるので、スマホでもスクロールが1本で済む
- **Issue も1か所にまとまる**: 全リポの未消化の Issue を1つの一覧にする
- **GitHub の複数アカウントに対応**: 個人用と仕事用のように、別々のアカウントの Issue を分けて取り込める
- **スマホで軽い**: JavaScript なしでも読める静的 HTML なので、外出先でもすぐ開ける
diff --git a/codeview/layout.py b/codeview/layout.py
index 6c8f948..8ebc542 100644
--- a/codeview/layout.py
+++ b/codeview/layout.py
@@ -222,6 +222,28 @@ def cell(item, cls, arrow_l, arrow_r):
if (mark(n)) history.replaceState(null, '', '#ln-' + n);
});
+ // iframe を中身の高さまで伸ばし、スクロールを外側の1本にまとめる。届くまでは CSS の 80vh のまま
+ const frames = [...document.querySelectorAll('iframe.htmlview')];
+ if (frames.length) {
+ const CAP = 50000;
+ const state = new Map();
+ window.addEventListener('message', (ev) => {
+ const f = frames.find((x) => x.contentWindow === ev.source);
+ const h = ev.data && ev.data.codeviewHeight;
+ if (!f || typeof h !== 'number' || !isFinite(h) || h <= 0) return;
+ // 100vh に余白を足した中身は、伸ばすたびに同じ幅だけ伸び返す。同じ伸びが続いたら止める
+ const s = state.get(f) || { cur: f.offsetHeight, step: 0, same: 0, frozen: false };
+ if (s.frozen) return;
+ const step = h - s.cur;
+ s.same = step > 0 && step === s.step ? s.same + 1 : 0;
+ s.step = step;
+ if (s.same >= 2) { s.frozen = true; state.set(f, s); return; }
+ s.cur = Math.min(h, CAP);
+ state.set(f, s);
+ f.style.height = s.cur + 'px';
+ });
+ }
+
const wrap = document.getElementById('wrap');
if (wrap) {
const KEY = 'codeview.wrap';
diff --git a/codeview/render.py b/codeview/render.py
index 4a1de71..09f0245 100644
--- a/codeview/render.py
+++ b/codeview/render.py
@@ -81,18 +81,34 @@ def is_html(path: str) -> bool:
return path.lower().endswith((".html", ".htm"))
+# sandbox の外からは中身の高さを測れないので、中から親へ送らせる。送るのは数値だけ。
+# 末尾に足すので、閉じていない "
+)
+
+
def render_html_page(path: str, data: bytes) -> tuple[str, str | None]:
"""描画した HTML の iframe と、折りたたんだソースを返す。
allow-same-origin を付けないので、中のスクリプトは origin が不透明になり、
- codeview の Cookie や親ページに触れない。
+ codeview の Cookie や親ページに触れない。中身の末尾には高さを親へ知らせる
+ `HTML_FIT_SCRIPT` を足し、親(FILE_SCRIPT)が iframe を中身の高さまで伸ばす。
"""
text = data.decode("utf-8", "replace")
code, lang = render_code_lang(path, data)
body = (
''
+ f'srcdoc="{html.escape(text + HTML_FIT_SCRIPT, quote=True)}">'
f'Source
{code} '
)
return body, lang
diff --git a/codeview/style.py b/codeview/style.py
index 8440743..d717433 100644
--- a/codeview/style.py
+++ b/codeview/style.py
@@ -213,7 +213,7 @@
.nowrap .hl pre > span.hit { min-width: calc(100% + 2rem); }
/* ---------- html ---------- */
-/* 中身の高さは sandbox の外から測れないので、画面に対する固定の高さにして中で送る */
+/* 中身の高さは FILE_SCRIPT が中から受け取って伸ばす。JS が無いときと届くまでは画面に対する固定の高さで中を送る */
.htmlview {
display: block; width: 100%; height: 80vh; margin: 1em 0;
border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff;
diff --git a/docs/guarantees/build.md b/docs/guarantees/build.md
index 309b87c..80e8db9 100644
--- a/docs/guarantees/build.md
+++ b/docs/guarantees/build.md
@@ -67,7 +67,8 @@
| 画像ファイルのページはその画像を `
` で表示する | `test_images_are_copied_and_shown` |
| 5 MiB を超える画像のページは画像を出さず `Too large to display` を出す | `test_images_are_copied_and_shown` |
| `.svg` のページはコードとして描く | `test_images_are_copied_and_shown`, `test_html_file_page_renders_in_sandboxed_iframe` |
-| `.html` と `.htm` のファイルページは、ファイルの中身をそのまま `srcdoc` に入れた `