fix table's borders

This commit is contained in:
Erza Bytyci
2026-05-22 09:46:54 +00:00
parent a3e023e1af
commit d6dab8e72e
2 changed files with 26 additions and 10 deletions
@@ -218,6 +218,14 @@ function isEffectivelyEmptyBlock(node) {
return meaningfulChildren.length === 0;
}
function hasExplicitBorderStyle(node) {
if (!node || typeof node.getAttribute !== "function") {
return false;
}
const style = String(node.getAttribute("style") || "");
return /(^|;)\s*border(?:-(top|right|bottom|left))?\s*:/i.test(style);
}
function normalizeTableSpacing(html) {
const dom = new JSDOM(`<div id="root">${String(html || "")}</div>`);
const { document } = dom.window;
@@ -226,6 +234,14 @@ function normalizeTableSpacing(html) {
return String(html || "");
}
// Keep user-intended border styles, but remove implicit border attributes
// (for example border="1") when no explicit CSS border was set.
root.querySelectorAll("table, td, th").forEach((node) => {
if (node.hasAttribute("border") && !hasExplicitBorderStyle(node)) {
node.removeAttribute("border");
}
});
const tables = root.querySelectorAll("table");
tables.forEach((table) => {
let prev = table.previousElementSibling;
+10 -10
View File
@@ -617,20 +617,20 @@ label {
max-width: 100%;
}
/* Keep published editor tables borderless unless explicitly restyled by page-specific CSS. */
.rendered-html table,
.rendered-html table tbody,
.rendered-html table thead,
.rendered-html table tr,
.rendered-html table td,
.rendered-html table th {
border: none !important;
}
/* Default to borderless tables, but keep any explicit per-cell/table border styling. */
.rendered-html table {
border-collapse: collapse;
}
.rendered-html table:not([style*="border"]) {
border: none;
}
.rendered-html table td:not([style*="border"]),
.rendered-html table th:not([style*="border"]) {
border: none;
}
.rendered-html {
display: flow-root;
width: 100%;