Skip to content

Commit

Permalink
Increase the probability that removeUnnecessarySpan and mergeNode wil…
Browse files Browse the repository at this point in the history
…l be triggered.
  • Loading branch information
miku1958 committed Jun 3, 2024
1 parent 293f512 commit 71f3606
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { enumerateComputedStyle, removeUnnecessaryAttribute, removeUnnecessarySpan, removeUnnecessaryStyle } from './removeUnnecessarySpan';
import { isEntityElement } from '../../domUtils/entityUtils';
import { mergeNode } from './mergeNode';
import { removeUnnecessarySpan } from './removeUnnecessarySpan';

/**
* @internal
*/
export function optimize(root: Node) {
export function optimize(root: Node, isRecursive = false) {

Check failure on line 8 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/optimize.ts

GitHub Actions / build

Expected a type annotation
/**
* Do no do any optimization to entity
*/
if (isEntityElement(root)) {
return;
}
if (!isRecursive && root.parentElement != null) {
let computedAttributes = {} as Record<string, Attr>;

Check failure on line 16 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/optimize.ts

GitHub Actions / build

'computedAttributes' is never reassigned. Use 'const' instead
// html doesn't provide computed attributes, use parent's attributes directly
Array.from(root.parentElement.attributes).forEach((attr) => {
computedAttributes[attr.name] = attr;
});
removeUnnecessaryAttribute(root, computedAttributes);

let computedStyle = {} as Record<string, Set<string>>;

Check failure on line 23 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/optimize.ts

GitHub Actions / build

'computedStyle' is never reassigned. Use 'const' instead
enumerateComputedStyle(root.parentElement, (key, values) => {
computedStyle[key] = values;
});
removeUnnecessaryStyle(root, computedStyle);
}

removeUnnecessarySpan(root);
mergeNode(root);

for (let child = root.firstChild; child; child = child.nextSibling) {
optimize(child);
optimize(child, true);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
import { isNodeOfType } from '../../domUtils/isNodeOfType';

/**
* @internal
*/
export function removeUnnecessaryAttribute(root: Node, computedAttributes: Record<string, Attr>) {
if (!isNodeOfType(root, 'ELEMENT_NODE')) {
return
}
let newComputedAttributes = {

Check failure on line 10 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/removeUnnecessarySpan.ts

GitHub Actions / build

'newComputedAttributes' is never reassigned. Use 'const' instead
...computedAttributes,
};
for (let i = root.attributes.length - 1; i >= 0; i--) {
const attr = root.attributes[i];
if (newComputedAttributes[attr.name]?.isEqualNode(attr) ?? false) {
root.removeAttribute(attr.name);
} else {
newComputedAttributes[attr.name] = attr;
}
}

for (let child = root.firstChild; child; child = child.nextSibling) {
removeUnnecessaryAttribute(child, newComputedAttributes);
}
}

/**
* @internal
*/
export function enumerateComputedStyle(element: HTMLElement, handler: (key: string, value: Set<string>) => void) {
element.style.cssText.split(";").forEach((value) => {
let [key, valueText] = value.split(":");

Check failure on line 32 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/removeUnnecessarySpan.ts

GitHub Actions / build

'valueText' is never reassigned. Use 'const' instead
if (!key || !valueText) {
return;
}
key = key.trim();
let values = new Set(valueText.split(",").map((value) => value.trim()));

Check failure on line 37 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/removeUnnecessarySpan.ts

GitHub Actions / build

'values' is never reassigned. Use 'const' instead

handler(key, values);
});
}

/**
* @internal
*/
export function removeUnnecessaryStyle(root: Node, computedCSS: Record<string, Set<string>>) {
if (!isNodeOfType(root, 'ELEMENT_NODE')) {
return
}
let newComputedCSS = {

Check failure on line 50 in packages/roosterjs-content-model-dom/lib/modelToDom/optimizers/removeUnnecessarySpan.ts

GitHub Actions / build

'newComputedCSS' is never reassigned. Use 'const' instead
...computedCSS
}
enumerateComputedStyle(root, (key, values) => {
if (computedCSS[key]?.size === values.size && [...computedCSS[key]].every((value) => values.has(value))) {
root.style.removeProperty(key);
} else {
newComputedCSS[key] = values;
}
});

if (root.style.cssText === "") {
root.removeAttribute("style");
}

for (let child = root.firstChild; child; child = child.nextSibling) {
removeUnnecessaryStyle(child, newComputedCSS);
}
}

/**
* @internal
*/

0 comments on commit 71f3606

Please sign in to comment.