@scrider/formatter 1.10.6 → 1.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +71 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2740,6 +2740,10 @@ var imageFormat = {
|
|
|
2740
2740
|
if (element.tagName.toLowerCase() !== "img") return null;
|
|
2741
2741
|
const src = element.getAttribute("src");
|
|
2742
2742
|
if (!src) return null;
|
|
2743
|
+
const style = element.getAttribute("style") || "";
|
|
2744
|
+
if (/(?:^|;)\s*opacity\s*:\s*0(?:\.0+)?\s*(?:;|$)/i.test(style)) {
|
|
2745
|
+
return null;
|
|
2746
|
+
}
|
|
2743
2747
|
const attrs = {};
|
|
2744
2748
|
const alt = element.getAttribute("alt");
|
|
2745
2749
|
const width = element.getAttribute("width");
|
|
@@ -4013,15 +4017,6 @@ function renderInlineText(text, attributes) {
|
|
|
4013
4017
|
if (!text) return "";
|
|
4014
4018
|
let html = escapeHtml(text);
|
|
4015
4019
|
if (!attributes) return html;
|
|
4016
|
-
const styles = [];
|
|
4017
|
-
for (const [format, cssProperty] of Object.entries(INLINE_STYLE_FORMATS)) {
|
|
4018
|
-
if (format in attributes) {
|
|
4019
|
-
styles.push(`${cssProperty}: ${String(attributes[format])}`);
|
|
4020
|
-
}
|
|
4021
|
-
}
|
|
4022
|
-
if (styles.length > 0) {
|
|
4023
|
-
html = `<span style="${styles.join("; ")}">${html}</span>`;
|
|
4024
|
-
}
|
|
4025
4020
|
for (let i = INLINE_FORMAT_ORDER.length - 1; i >= 0; i--) {
|
|
4026
4021
|
const format = INLINE_FORMAT_ORDER[i];
|
|
4027
4022
|
if (!format) continue;
|
|
@@ -4035,6 +4030,15 @@ function renderInlineText(text, attributes) {
|
|
|
4035
4030
|
html = `<${tag}>${html}</${tag}>`;
|
|
4036
4031
|
}
|
|
4037
4032
|
}
|
|
4033
|
+
const styles = [];
|
|
4034
|
+
for (const [format, cssProperty] of Object.entries(INLINE_STYLE_FORMATS)) {
|
|
4035
|
+
if (format in attributes) {
|
|
4036
|
+
styles.push(`${cssProperty}: ${String(attributes[format])}`);
|
|
4037
|
+
}
|
|
4038
|
+
}
|
|
4039
|
+
if (styles.length > 0) {
|
|
4040
|
+
html = `<span style="${styles.join("; ")}">${html}</span>`;
|
|
4041
|
+
}
|
|
4038
4042
|
return html;
|
|
4039
4043
|
}
|
|
4040
4044
|
function renderEmbed(value, attributes, renderers, blockHandlers, options) {
|
|
@@ -4080,6 +4084,44 @@ function renderEmbed(value, attributes, renderers, blockHandlers, options) {
|
|
|
4080
4084
|
|
|
4081
4085
|
// src/conversion/html/html-to-delta.ts
|
|
4082
4086
|
var import_delta9 = require("@scrider/delta");
|
|
4087
|
+
function isCssWideKeyword(value) {
|
|
4088
|
+
return /^(inherit|initial|unset|revert|revert-layer)$/i.test(value.trim());
|
|
4089
|
+
}
|
|
4090
|
+
function cleanPastedFontFamily(value) {
|
|
4091
|
+
if (value == null) return null;
|
|
4092
|
+
const trimmed = value.trim();
|
|
4093
|
+
if (!trimmed || isCssWideKeyword(trimmed)) return null;
|
|
4094
|
+
const first = trimmed.split(",").map((part) => part.trim().replace(/^["']|["']$/g, "")).find((part) => part && !isCssWideKeyword(part) && !part.startsWith("var("));
|
|
4095
|
+
if (!first) return null;
|
|
4096
|
+
const lower = first.toLowerCase();
|
|
4097
|
+
if (lower === "serif" || lower === "sans-serif" || lower === "monospace" || lower === "cursive" || lower === "fantasy" || lower === "system-ui") {
|
|
4098
|
+
return null;
|
|
4099
|
+
}
|
|
4100
|
+
return first;
|
|
4101
|
+
}
|
|
4102
|
+
function cleanPastedFontSize(value) {
|
|
4103
|
+
if (value == null) return null;
|
|
4104
|
+
const trimmed = value.trim();
|
|
4105
|
+
if (!trimmed || isCssWideKeyword(trimmed)) return null;
|
|
4106
|
+
if (/^(medium|smaller|larger|xx-small|x-small|small|large|x-large|xx-large)$/i.test(trimmed)) {
|
|
4107
|
+
return null;
|
|
4108
|
+
}
|
|
4109
|
+
const px = trimmed.match(/^([\d.]+)\s*px$/i);
|
|
4110
|
+
if (px?.[1]) {
|
|
4111
|
+
const pt = Math.round(Number(px[1]) * 72 / 96);
|
|
4112
|
+
return pt > 0 ? `${pt}pt` : null;
|
|
4113
|
+
}
|
|
4114
|
+
return trimmed;
|
|
4115
|
+
}
|
|
4116
|
+
function isSelectionBackgroundColor(value) {
|
|
4117
|
+
const m = value.trim().match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/i);
|
|
4118
|
+
if (!m) return false;
|
|
4119
|
+
const r = Number(m[1]);
|
|
4120
|
+
const g = Number(m[2]);
|
|
4121
|
+
const b = Number(m[3]);
|
|
4122
|
+
if (![r, g, b].every((n) => Number.isFinite(n))) return false;
|
|
4123
|
+
return r >= 180 && g >= 180 && b >= 180 && b >= r - 5 && b >= g - 5 && b - Math.min(r, g) <= 40;
|
|
4124
|
+
}
|
|
4083
4125
|
function htmlToDelta(html, options = {}) {
|
|
4084
4126
|
const adapter = options.adapter ?? getAdapter();
|
|
4085
4127
|
const normalizeWhitespace = options.normalizeWhitespace ?? true;
|
|
@@ -4428,20 +4470,30 @@ function htmlToDelta(html, options = {}) {
|
|
|
4428
4470
|
flushText();
|
|
4429
4471
|
const prevAttrs = { ...currentAttributes };
|
|
4430
4472
|
const color = element.style?.color || element.style?.getPropertyValue?.("color");
|
|
4431
|
-
if (color) {
|
|
4473
|
+
if (color && !isCssWideKeyword(color)) {
|
|
4432
4474
|
currentAttributes.color = color;
|
|
4433
4475
|
}
|
|
4434
4476
|
const bg = element.style?.backgroundColor || element.style?.getPropertyValue?.("background-color");
|
|
4435
|
-
if (bg) {
|
|
4477
|
+
if (bg && !isCssWideKeyword(bg) && !isSelectionBackgroundColor(bg)) {
|
|
4436
4478
|
currentAttributes.background = bg;
|
|
4437
4479
|
}
|
|
4438
4480
|
const fontFamily = element.style?.fontFamily || element.style?.getPropertyValue?.("font-family");
|
|
4439
|
-
|
|
4440
|
-
|
|
4481
|
+
const cleanedFont = cleanPastedFontFamily(fontFamily);
|
|
4482
|
+
if (cleanedFont) {
|
|
4483
|
+
currentAttributes.font = cleanedFont;
|
|
4441
4484
|
}
|
|
4442
4485
|
const fontSize = element.style?.fontSize || element.style?.getPropertyValue?.("font-size");
|
|
4443
|
-
|
|
4444
|
-
|
|
4486
|
+
const cleanedSize = cleanPastedFontSize(fontSize);
|
|
4487
|
+
if (cleanedSize) {
|
|
4488
|
+
currentAttributes.size = cleanedSize;
|
|
4489
|
+
}
|
|
4490
|
+
const fontWeight = element.style?.fontWeight || element.style?.getPropertyValue?.("font-weight");
|
|
4491
|
+
if (fontWeight === "bold" || fontWeight === "bolder" || fontWeight != null && Number.parseInt(String(fontWeight), 10) >= 600) {
|
|
4492
|
+
currentAttributes.bold = true;
|
|
4493
|
+
}
|
|
4494
|
+
const fontStyle = element.style?.fontStyle || element.style?.getPropertyValue?.("font-style");
|
|
4495
|
+
if (fontStyle === "italic" || fontStyle === "oblique") {
|
|
4496
|
+
currentAttributes.italic = true;
|
|
4445
4497
|
}
|
|
4446
4498
|
processChildren(element);
|
|
4447
4499
|
flushText();
|
|
@@ -4450,6 +4502,10 @@ function htmlToDelta(html, options = {}) {
|
|
|
4450
4502
|
function processImageElement(element) {
|
|
4451
4503
|
const src = element.getAttribute("src");
|
|
4452
4504
|
if (!src) return;
|
|
4505
|
+
const style = element.getAttribute("style") || "";
|
|
4506
|
+
if (/(?:^|;)\s*opacity\s*:\s*0(?:\.0+)?\s*(?:;|$)/i.test(style)) {
|
|
4507
|
+
return;
|
|
4508
|
+
}
|
|
4453
4509
|
const attrs = {};
|
|
4454
4510
|
const alt = element.getAttribute("alt");
|
|
4455
4511
|
const width = element.getAttribute("width");
|