getHtmlFromNode merges multiple paragraphs in a list item (stripPtag unwraps every li > p)

Aperta Adatta ai principianti
#130 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react, typescript
Ambito
frontend

Direzione di ricerca

Parti da stripPtag in src/utils/index.ts, che rimuove il wrapper di ogni corrispondenza di li > p nel div ricevuto. Esegui lo script di riproduzione dell'issue, che stampa l'HTML di onChange e i paragrafi dopo un ricaricamento da JSON. È fatto quando un li con due elementi p mantiene entrambi i paragrafi nell'HTML di onChange e il ricaricamento da JSON restituisce [ 'first', 'second' ], mentre i list item con un solo paragrafo continuano a perdere il wrapper.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When a list_item contains multiple paragraphs (allowed by ListItem's paragraph block* schema, e.g. <ul><li><p>first</p><p>second</p></li></ul>), getHtmlFromNode unwraps every <p> inside <li> and concatenates their text with no separator in onChange HTML, autoSave, and json initialization.

Reproduction
npm install [email protected] react@18 react-dom@18 jsdom
node repro.mjs

repro.mjs:

import { JSDOM } from "jsdom";
import React, { act } from "react";
import { createRoot } from "react-dom/client";

const dom = new JSDOM("<!doctype html><html><body></body></html>", { url: "http://localhost/" });
for (const k of ["window", "document", "HTMLElement", "Element", "Node", "navigator", "location", "localStorage"]) {
  Object.defineProperty(globalThis, k, { value: dom.window[k], configurable: true, writable: true });
}
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
const zeroRect = () => ({ top: 0, left: 0, right: 0, bottom: 0, width: 0, height: 0 });
dom.window.Element.prototype.getClientRects = () => [];
dom.window.Element.prototype.getBoundingClientRect = zeroRect;
dom.window.Range.prototype.getClientRects = () => [];
dom.window.Range.prototype.getBoundingClientRect = zeroRect;

const sb = await import("smartblock");
const { SmartBlock, Extensions } = sb.default ?? sb;

let firstOut, reloadOut;
const el1 = document.body.appendChild(document.createElement("div"));
await act(async () => {
  createRoot(el1).render(
    React.createElement(SmartBlock, {
      extensions: Extensions,
      html: "<ul><li><p>first</p><p>second</p></li></ul>",
      onChange: (v) => { firstOut = v; },
    })
  );
});

const el2 = document.body.appendChild(document.createElement("div"));
await act(async () => {
  createRoot(el2).render(
    React.createElement(SmartBlock, {
      extensions: Extensions,
      json: firstOut.json,
      onChange: (v) => { reloadOut = v; },
    })
  );
});

console.log("onChange html:", firstOut.html.replace(/ id="[^"]*"/g, ""));
console.log("before reload paragraphs:", firstOut.json.content[0].content[0].content.map((p) => p.content[0].text));
console.log("after json reload paragraphs:", reloadOut.json.content[0].content[0].content.map((p) => p.content[0].text));
Actual vs Expected

Actual output:

onChange html: <ul><li>firstsecond</li></ul><p></p>
before reload paragraphs: [ 'first', 'second' ]
after json reload paragraphs: [ 'firstsecond' ]

Expected: onChange HTML keeps the paragraph tags when an <li> contains more than one paragraph (<ul><li><p>first</p><p>second</p></li></ul><p></p>), and reloading firstOut.json via the json prop preserves [ 'first', 'second' ].

In src/utils/index.ts, stripPtag unwraps every div.querySelectorAll('li > p') match instead of only unwrapping when the <p> is the sole paragraph in its <li> (for example li > p:only-of-type).

Versions
  • smartblock: 2.0.3 (npm latest) and master at efcd14ef9069ca1f1929ee6e761bb5224312d536
  • Node.js: v26.13.0

Credit: https://ucsbplse.github.io/

Lingua principale
TypeScript
Stelle
318
Fork
27
Merge medio
4m
PR unite (30g)
16

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di appleple/smartblock

Tutte le issue di appleple/smartblock

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.