formacao
This commit is contained in:
+57
-10
@@ -295,7 +295,25 @@ function nodeIsComment(node) {
|
||||
function nodeIsFragment(node) {
|
||||
return node.nodeType === FRAGMENT_NODE;
|
||||
}
|
||||
const serialize$3 = (node, config, indentation, depth, refs, printer) => {
|
||||
function filterChildren(children, filterNode) {
|
||||
// Filter out text nodes that only contain whitespace to prevent empty lines
|
||||
// This is done regardless of whether a filterNode is provided
|
||||
let filtered = children.filter((node) => {
|
||||
// Filter out text nodes that are only whitespace
|
||||
if (node.nodeType === TEXT_NODE) {
|
||||
const text = node.data || "";
|
||||
// Keep text nodes that have non-whitespace content
|
||||
return text.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
// Apply additional user-provided filter if specified
|
||||
if (filterNode) {
|
||||
filtered = filtered.filter(filterNode);
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
function serializeDOM(node, config, indentation, depth, refs, printer, filterNode) {
|
||||
if (nodeIsText(node)) {
|
||||
return printText(node.data, config);
|
||||
}
|
||||
@@ -306,11 +324,22 @@ const serialize$3 = (node, config, indentation, depth, refs, printer) => {
|
||||
if (++depth > config.maxDepth) {
|
||||
return printElementAsLeaf(type, config);
|
||||
}
|
||||
const children = Array.prototype.slice.call(node.childNodes || node.children);
|
||||
const shadowChildren = nodeIsFragment(node) || !node.shadowRoot ? [] : Array.prototype.slice.call(node.shadowRoot.children);
|
||||
const resolvedChildren = filterNode ? filterChildren(children, filterNode) : children;
|
||||
const resolvedShadowChildren = filterNode ? filterChildren(shadowChildren, filterNode) : shadowChildren;
|
||||
return printElement(type, printProps(nodeIsFragment(node) ? [] : Array.from(node.attributes, (attr) => attr.name).sort(), nodeIsFragment(node) ? {} : [...node.attributes].reduce((props, attribute) => {
|
||||
props[attribute.name] = attribute.value;
|
||||
return props;
|
||||
}, {}), config, indentation + config.indent, depth, refs, printer), (nodeIsFragment(node) || !node.shadowRoot ? "" : printShadowRoot(Array.prototype.slice.call(node.shadowRoot.children), config, indentation + config.indent, depth, refs, printer)) + printChildren(Array.prototype.slice.call(node.childNodes || node.children), config, indentation + config.indent, depth, refs, printer), config, indentation);
|
||||
};
|
||||
}, {}), config, indentation + config.indent, depth, refs, printer), (resolvedShadowChildren.length > 0 ? printShadowRoot(resolvedShadowChildren, config, indentation + config.indent, depth, refs, printer) : "") + printChildren(resolvedChildren, config, indentation + config.indent, depth, refs, printer), config, indentation);
|
||||
}
|
||||
const serialize$3 = (node, config, indentation, depth, refs, printer) => serializeDOM(node, config, indentation, depth, refs, printer);
|
||||
function createDOMElementFilter(filterNode) {
|
||||
return {
|
||||
test: test$3,
|
||||
serialize: (node, config, indentation, depth, refs, printer) => serializeDOM(node, config, indentation, depth, refs, printer, filterNode)
|
||||
};
|
||||
}
|
||||
const plugin$3 = {
|
||||
serialize: serialize$3,
|
||||
test: test$3
|
||||
@@ -892,15 +921,30 @@ function findPlugin(plugins, val) {
|
||||
return null;
|
||||
}
|
||||
function printer(val, config, indentation, depth, refs, hasCalledToJSON) {
|
||||
let result;
|
||||
const plugin = findPlugin(config.plugins, val);
|
||||
if (plugin !== null) {
|
||||
return printPlugin(plugin, val, config, indentation, depth, refs);
|
||||
result = printPlugin(plugin, val, config, indentation, depth, refs);
|
||||
} else {
|
||||
const basicResult = printBasicValue(val, config.printFunctionName, config.escapeRegex, config.escapeString);
|
||||
if (basicResult !== null) {
|
||||
result = basicResult;
|
||||
} else {
|
||||
result = printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON);
|
||||
}
|
||||
}
|
||||
const basicResult = printBasicValue(val, config.printFunctionName, config.escapeRegex, config.escapeString);
|
||||
if (basicResult !== null) {
|
||||
return basicResult;
|
||||
// Per-depth output budget (inspired by Node's util.inspect).
|
||||
// Each depth level tracks output independently, so nested results
|
||||
// don't inflate a single counter (which would undercount by ~Nx for
|
||||
// N levels of nesting). Nodes at the same depth produce disjoint spans
|
||||
// in the output string, so each bucket accurately reflects output at
|
||||
// that level. Total output is bounded by maxDepth × maxOutputLength.
|
||||
config._outputLengthPerDepth[depth] ??= 0;
|
||||
config._outputLengthPerDepth[depth] += result.length;
|
||||
if (config._outputLengthPerDepth[depth] > config.maxOutputLength) {
|
||||
config.maxDepth = 0;
|
||||
}
|
||||
return printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON);
|
||||
return result;
|
||||
}
|
||||
const DEFAULT_THEME = {
|
||||
comment: "gray",
|
||||
@@ -918,6 +962,7 @@ const DEFAULT_OPTIONS = {
|
||||
highlight: false,
|
||||
indent: 2,
|
||||
maxDepth: Number.POSITIVE_INFINITY,
|
||||
maxOutputLength: 1e6,
|
||||
maxWidth: Number.POSITIVE_INFINITY,
|
||||
min: false,
|
||||
plugins: [],
|
||||
@@ -982,7 +1027,9 @@ function getConfig(options) {
|
||||
printFunctionName: getPrintFunctionName(options),
|
||||
printShadowRoot: options?.printShadowRoot ?? true,
|
||||
spacingInner: options?.min ? " " : "\n",
|
||||
spacingOuter: options?.min ? "" : "\n"
|
||||
spacingOuter: options?.min ? "" : "\n",
|
||||
maxOutputLength: options?.maxOutputLength ?? DEFAULT_OPTIONS.maxOutputLength,
|
||||
_outputLengthPerDepth: []
|
||||
};
|
||||
}
|
||||
function createIndent(indent) {
|
||||
@@ -1019,4 +1066,4 @@ const plugins = {
|
||||
Error: ErrorPlugin
|
||||
};
|
||||
|
||||
export { DEFAULT_OPTIONS, format, plugins };
|
||||
export { DEFAULT_OPTIONS, createDOMElementFilter, format, plugins };
|
||||
|
||||
Reference in New Issue
Block a user