feat(editor): add lossless bounded CSV table editing
This commit is contained in:
1 parent
bd80ff6a0c
commit
2d6ceac0d7
19 files changed
+520
-40
No files matched your search
@@ -0,0 +1,150 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"html"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const csvEditorFixtureBody = "\ufeff\"name\",description\r\n日本語,\"a,b\r\nnext\"\r\n"
|
||||
|
||||
func TestCSVEditorRevisionAndBounds(t *testing.T) {
|
||||
s, token, domain, _ := editorFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/table.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := s.store.SaveRevisionContent(object, []byte(csvEditorFixtureBody), "", "und", "text/csv; charset=utf-8", "table.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, query := range []string{"", "?edittype=markdown", "?lang=und"} {
|
||||
w := configRequest(s, token, "GET", "/e/-/table.csv"+query, nil)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `data-csv="true"`) || !strings.Contains(w.Body.String(), `value="code"`) || strings.Contains(w.Body.String(), `data-editor-action="markdown"`) {
|
||||
t.Fatalf("editor %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
head, _ := s.store.GetRevisionForExactLanguage(object, "und")
|
||||
if head.UUID != first.UUID {
|
||||
t.Fatal("GET changed revision")
|
||||
}
|
||||
save := func(auth, base, body string) int {
|
||||
data, _ := json.Marshal(map[string]any{"object_uuid": object, "body": body, "lang": "und", "content_type": "text/csv; charset=utf-8", "filename": "table.csv", "base_revision_uuid": base})
|
||||
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(data))
|
||||
r.Header.Set("Authorization", "Bearer "+auth)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w.Code
|
||||
}
|
||||
changed := strings.Replace(csvEditorFixtureBody, "日本語", "更新", 1)
|
||||
if code := save(token, first.UUID, changed); code != 200 {
|
||||
t.Fatal("save", code)
|
||||
}
|
||||
if code := save(token, first.UUID, "stale"); code != 409 {
|
||||
t.Fatal("stale save", code)
|
||||
}
|
||||
head, _ = s.store.GetRevisionForExactLanguage(object, "und")
|
||||
if head.Body != changed || head.ContentType != "text/csv; charset=utf-8" || head.Filename != "table.csv" {
|
||||
t.Fatal("CSV altered")
|
||||
}
|
||||
state := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&lang=und&revision="+first.UUID, nil)
|
||||
var content struct {
|
||||
Body string
|
||||
Editable bool
|
||||
}
|
||||
if err = json.Unmarshal(state.Body.Bytes(), &content); err != nil || state.Code != 200 || !content.Editable || content.Body != csvEditorFixtureBody {
|
||||
t.Fatal("restore", state.Code, err)
|
||||
}
|
||||
reader, err := s.store.CreatePasswordAccount(domain, "csvreader", "a long test password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.SetPrincipalState(domain, reader.UUID, "readonly"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denied, _ := s.store.CreateAuthSession(domain, reader.UUID)
|
||||
if code := save(denied, head.UUID, "forbidden"); code != 403 {
|
||||
t.Fatal("readonly save", code)
|
||||
}
|
||||
// Initial CSV is embedded as an escaped JS string to preserve CRLF. A
|
||||
// field must not terminate that script or become executable markup.
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte("\"</script><script>alert(1)</script>\",value\r\n"), "", "und", "text/csv", "table.csv"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
escaped := configRequest(s, token, "GET", "/e/-/table.csv", nil)
|
||||
if escaped.Code != 200 || strings.Contains(escaped.Body.String(), "<script>alert(1)</script>") || !strings.Contains(escaped.Body.String(), `\u003c/script\u003e`) {
|
||||
t.Fatal("CSV script string was not escaped")
|
||||
}
|
||||
for _, input := range []struct {
|
||||
body []byte
|
||||
code int
|
||||
}{{[]byte(strings.Repeat("x", maxCSVMacroBytes+1)), 413}, {[]byte{0xff, 0xfe}, 415}, {[]byte("a,\x00"), 415}} {
|
||||
rev, e := s.store.SaveRevisionContent(object, input.body, "", "und", "text/csv", "table.csv")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
w := configRequest(s, token, "GET", "/e/-/table.csv", nil)
|
||||
if w.Code != input.code {
|
||||
t.Fatalf("editor bounds %d wanted %d", w.Code, input.code)
|
||||
}
|
||||
w = configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&lang=und&revision="+rev.UUID, nil)
|
||||
if w.Code != input.code {
|
||||
t.Fatalf("state bounds %d wanted %d", w.Code, input.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVEditorChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
for _, width := range []string{"1280", "390"} {
|
||||
t.Run(width, func(t *testing.T) {
|
||||
s, token, domain, _ := editorFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/table.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte(csvEditorFixtureBody), "", "und", "text/csv; charset=utf-8", "table.csv"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/csv-editor.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/e/-/table.csv" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for k, v := range rec.Header() {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, policy, _ := strings.Cut(rec.Header().Get("Content-Security-Policy"), "'nonce-")
|
||||
nonce, _, _ := strings.Cut(policy, "'")
|
||||
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", `<script nonce="`+html.EscapeString(nonce)+`">`+string(script)+"</script></body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
||||
defer cancel()
|
||||
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/e/-/table.csv", t.TempDir(), "/tmp/pageswiki-csv-editor-"+width+".png", width, "850").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("browser: %v %s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@ package server
|
||||
|
||||
func init() {
|
||||
for lang, entries := range map[string]map[string]string{
|
||||
"en": {"editor.tools": "Insert component / drafts", "editor.language": "Edit / fork language", "editor.restore": "Restore a revision into the editor"},
|
||||
"ja": {"editor.tools": "コンポーネント挿入・下書き", "editor.language": "言語の編集・分岐", "editor.restore": "過去のリビジョンをエディタに復元"},
|
||||
"en": {"editor.tools": "Insert component / drafts", "editor.language": "Edit / fork language", "editor.restore": "Restore a revision into the editor", "editor.csv_source": "CSV source", "editor.csv_table": "CSV table"},
|
||||
"ja": {"editor.tools": "コンポーネント挿入・下書き", "editor.language": "言語の編集・分岐", "editor.restore": "過去のリビジョンをエディタに復元", "editor.csv_source": "CSVソースを表示", "editor.csv_table": "CSVの表を表示"},
|
||||
} {
|
||||
for key, value := range entries {
|
||||
translations[lang][key] = value
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"pageswiki/internal/grammaratur"
|
||||
)
|
||||
@@ -73,7 +74,20 @@ func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
out := map[string]any{"revision_uuid": rev.UUID, "head_revision_uuid": head, "lang": rev.Language, "content_type": rev.ContentType, "filename": rev.Filename, "principal_uuid": owner}
|
||||
if r.URL.Query().Get("metadata") != "1" {
|
||||
if rev.UUID != "" {
|
||||
csv := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(strings.TrimSpace(strings.Split(rev.ContentType, ";")[0])), Filename: rev.Filename}).Kind == "csv"
|
||||
if csv {
|
||||
size, e := s.store.DocumentSizeContext(r.Context(), rev.DocumentHash)
|
||||
if e != nil || size > maxCSVMacroBytes {
|
||||
http.Error(w, "CSV editor size limit exceeded", 413)
|
||||
return
|
||||
}
|
||||
window, e := s.store.ReadDocumentWindowContext(r.Context(), rev.DocumentHash, 0, maxCSVMacroBytes)
|
||||
if e != nil || window.Truncated || !utf8.Valid(window.Data) || strings.ContainsRune(string(window.Data), 0) {
|
||||
http.Error(w, "CSV source unavailable or not UTF-8", 415)
|
||||
return
|
||||
}
|
||||
rev.Body = string(window.Data)
|
||||
} else if rev.UUID != "" {
|
||||
rev, err = s.store.GetRevision(rev.UUID)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -90,7 +104,7 @@ func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
out["body"], out["markdown"] = rev.Body, markdown
|
||||
kind := revisionContentView(rev, "").Kind
|
||||
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code"
|
||||
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code" || (csv && utf8.ValidString(rev.Body) && !strings.ContainsRune(rev.Body, 0))
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(out)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"pageswiki/internal/grammaratur"
|
||||
"pageswiki/internal/store"
|
||||
@@ -211,16 +212,43 @@ func (s *Server) handleSemanticEditor(
|
||||
}
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.latestRevisionForRequest(
|
||||
r,
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
body := ""
|
||||
// CSV editing needs the complete source, never the viewer's truncated window.
|
||||
// Check metadata before loading a potentially large document.
|
||||
metadata, metaErr := s.store.GetLatestRevisionMetadataForLanguagesContext(r.Context(), objectUUID, s.revisionLanguageCandidates(r))
|
||||
if lang := r.URL.Query().Get("lang"); lang != "" {
|
||||
latest, err = s.store.GetRevisionForExactLanguage(objectUUID, lang)
|
||||
metadata, metaErr = s.store.GetRevisionMetadataForExactLanguage(objectUUID, lang)
|
||||
}
|
||||
if metaErr != nil || metadata == nil {
|
||||
http.Error(w, "revision unavailable or invalid language", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
csvEditor := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(strings.TrimSpace(strings.Split(metadata.ContentType, ";")[0])), Filename: metadata.Filename}).Kind == "csv"
|
||||
if csvEditor {
|
||||
size, sizeErr := s.store.DocumentSizeContext(r.Context(), metadata.DocumentHash)
|
||||
if sizeErr != nil || size > maxCSVMacroBytes {
|
||||
http.Error(w, "CSV editor requires a complete document of at most 256 KiB", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var latest *store.Revision
|
||||
if csvEditor {
|
||||
// Load the immutable revision we checked; a concurrent new head is
|
||||
// handled by the normal save CAS, not by an unbounded second read.
|
||||
window, readErr := s.store.ReadDocumentWindowContext(r.Context(), metadata.DocumentHash, 0, maxCSVMacroBytes)
|
||||
if readErr != nil || window.Truncated {
|
||||
http.Error(w, "CSV source unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
latest = metadata
|
||||
latest.Body = string(window.Data)
|
||||
} else {
|
||||
latest, err = s.latestRevisionForRequest(r, objectUUID)
|
||||
if lang := r.URL.Query().Get("lang"); lang != "" {
|
||||
latest, err = s.store.GetRevisionForExactLanguage(objectUUID, lang)
|
||||
}
|
||||
}
|
||||
body := ""
|
||||
if err != nil {
|
||||
http.Error(w, "revision unavailable or invalid language", 400)
|
||||
return
|
||||
@@ -233,7 +261,13 @@ func (s *Server) handleSemanticEditor(
|
||||
if err == nil {
|
||||
if latest.UUID != "" {
|
||||
view := revisionContentView(latest, path)
|
||||
if view.Kind == "code" {
|
||||
if view.Kind == "csv" {
|
||||
if !csvEditor || !utf8.ValidString(latest.Body) || strings.ContainsRune(latest.Body, 0) || len(latest.Body) > maxCSVMacroBytes {
|
||||
http.Error(w, "CSV editor requires bounded UTF-8 text", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
editType = "code"
|
||||
} else if view.Kind == "code" {
|
||||
editType = "code"
|
||||
if strings.EqualFold(strings.TrimSpace(strings.Split(latest.ContentType, ";")[0]), "text/plain") {
|
||||
editType = "plain"
|
||||
@@ -294,6 +328,7 @@ func (s *Server) handleSemanticEditor(
|
||||
"BaseBody": baseBody,
|
||||
"BaseRevision": latest.UUID,
|
||||
"EditType": editType,
|
||||
"CSVEditor": csvEditor,
|
||||
"PageLanguage": latest.Language,
|
||||
"PageContentType": contentType,
|
||||
"PageFilename": filename,
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
(async()=>{
|
||||
const assert=(ok,msg)=>{if(!ok)throw Error(msg)},delay=ms=>new Promise(r=>setTimeout(r,ms));
|
||||
const wait=async fn=>{for(let n=0;n<150;n++){if(fn())return;await delay(25)}throw Error('Timeout '+fn)};
|
||||
await wait(()=>window.PagesWikiCodeMirror&&document.querySelector('.csv-edit-scroll textarea'));
|
||||
const e=window.PagesEditor;setEditorPreviewVisible(true);e.paused=true;
|
||||
if(innerWidth<=700){assert(getComputedStyle(document.querySelector('.editor-pane')).display==='none','Mobile table is not full width');setEditorPreviewVisible(false);assert(getComputedStyle(document.querySelector('.editor-pane')).display!=='none','Mobile source inaccessible');setEditorPreviewVisible(true);}
|
||||
const original=e.text(),base=e.baseRevision;
|
||||
const cell=(r,c)=>document.querySelector(`.csv-edit-scroll textarea[data-row="${r}"][data-column="${c}"]`);
|
||||
const button=text=>[...document.querySelectorAll('.csv-edit-toolbar button')].find(b=>b.textContent===text);
|
||||
let input=cell(1,0);input.focus();input.value='新しい,名前';input.dispatchEvent(new Event('input',{bubbles:true}));
|
||||
await delay(400);assert(document.activeElement===input,'Table caret lost; connected='+input.isConnected+' active='+document.activeElement?.className);
|
||||
assert(e.text()==='\ufeff"name",description\r\n"新しい,名前","a,b\r\nnext"\r\n','Untouched CSV changed');
|
||||
e.persist();assert(localStorage.getItem(e.keyPrefix()+e.tab),'CSV draft missing');
|
||||
button('↶').click();assert(e.text()===original,'Undo not lossless');button('↷').click();
|
||||
assert(await e.saveNow(),'CSV save failed');assert(e.baseRevision!==base,'Revision unchanged');
|
||||
let state=await e.state();assert(state.body===e.text()&&state.content_type==='text/csv; charset=utf-8'&&state.editable,'CSV metadata changed');
|
||||
// Markup and formulas are text, never document nodes or evaluated values.
|
||||
e.paused=true;input=cell(1,0);input.value='<img src=x onerror=alert(1)>';input.dispatchEvent(new Event('input',{bubbles:true}));
|
||||
assert(!document.querySelector('.csv-edit-pane img'),'CSV injected markup');
|
||||
assert(await e.saveNow(),'Markup-as-data save failed');
|
||||
// A competing change is always reviewed, even on different CSV fields.
|
||||
const remoteBody=PagesCSV.edit(e.text(),'cell',0,1,'remote');
|
||||
const remote=await e.post('/_/api/v1/revisions/save',{object_uuid:e.value('object-uuid'),body:remoteBody,lang:'und',content_type:e.value('page-content-type'),filename:'table.csv',base_revision_uuid:e.baseRevision,editor_principal_uuid:e.owner});
|
||||
e.paused=true;e.setText(PagesCSV.edit(e.text(),'cell',1,0,'local'));
|
||||
assert(!await e.saveNow(),'Conflicting CSV silently saved');
|
||||
assert(document.querySelector('#editor-dialog textarea[readonly]'),'CSV conflict comparison missing');
|
||||
assert((await e.state()).revision_uuid===remote.revision_uuid,'Remote CSV overwritten');
|
||||
const merged=PagesCSV.edit(remoteBody,'cell',1,0,'local');
|
||||
const areas=document.querySelectorAll('#editor-dialog textarea');areas[areas.length-1].value=merged;
|
||||
document.querySelector('#editor-dialog footer button:last-child').click();await wait(()=>!document.querySelector('#editor-dialog'));
|
||||
assert(await e.saveNow(),'Reviewed CSV failed to save');
|
||||
// Restore stages the old CSV, retains MIME and creates a new revision on save.
|
||||
e.paused=true;await e.historyDialog();const history=document.querySelector('#editor-dialog select');history.value=base;
|
||||
document.querySelector('#editor-dialog footer button:last-child').click();await wait(()=>!document.querySelector('#editor-dialog'));
|
||||
assert(e.text()===original,'History did not restore exact CSV');assert(await e.saveNow(),'Restore save failed');
|
||||
// Source syntax errors do not cause a truncated table to be written back.
|
||||
e.paused=true;e.setText('"unfinished');await e.renderPreview();assert(!document.querySelector('.csv-edit-scroll'),'Malformed CSV table editable');assert(e.text()==='"unfinished','Malformed source lost');
|
||||
e.setText(original);await e.renderPreview();
|
||||
for(let i=0;i<27;i++)button('+↧').click();
|
||||
assert(PagesCSV.parse(e.text()).rows.length===29,'Row append failed');
|
||||
assert(document.querySelectorAll('.csv-edit-scroll tbody tr').length<=25,'Table was not paged');
|
||||
button('‹').click();assert(document.querySelectorAll('.csv-edit-scroll tbody tr').length===25,'Previous page missing');
|
||||
button('+→').click();assert(PagesCSV.parse(e.text()).rows[0].cells.length===3,'Column append failed');
|
||||
cell(1,1).focus();button('−→').click();assert(PagesCSV.parse(e.text()).rows[0].cells.length===2,'Column delete failed');
|
||||
button('↶').click();cell(1,1).focus();button('−↧').click();assert(PagesCSV.parse(e.text()).rows.length===28,'Row delete failed');
|
||||
button('↶').click();
|
||||
const css=getComputedStyle(document.querySelector('.csv-edit-toolbar'));assert(css.color!==css.backgroundColor,'Toolbar lacks contrast');
|
||||
clearTimeout(e.autosaveTimer);clearInterval(e.stateTimer);e.paused=true;
|
||||
document.body.dataset.vimReady='true';
|
||||
})().catch(error=>{const p=document.createElement('pre');p.id='error';p.textContent=error.stack;document.body.append(p)});
|
||||
@@ -109,6 +109,9 @@
|
||||
e.tools();
|
||||
const style=w.getComputedStyle(d.querySelector('#editor-dialog'));
|
||||
assert(style.color!==style.backgroundColor&&style.backgroundColor!=='rgba(0, 0, 0, 0)','Dialog not themed');
|
||||
const components=d.querySelector('#editor-dialog select');components.selectedIndex=components.options.length-1;
|
||||
d.querySelector('#editor-dialog footer button:nth-child(2)').click();await wait(()=>!d.querySelector('#editor-dialog'));
|
||||
assert(e.text().includes('{{csv:/data/table.csv')&&!e.text().includes('{=html}'),'CSV macro insertion was wrapped in protected HTML');
|
||||
document.body.dataset.vimReady='true';
|
||||
} catch(error) {const box=document.createElement('div');box.id='error';box.textContent=error.stack||error.message;document.body.prepend(box)}
|
||||
})();
|
||||
Reference in new issue
Block a user