@stencil/mock-doc 5.0.0-rc.1 → 5.0.0-rc.11

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.d.mts CHANGED
@@ -750,6 +750,7 @@ declare class MockElement extends MockNode {
750
750
  set textContent(value: string);
751
751
  get title(): string;
752
752
  set title(value: string);
753
+ toggleAttribute(attrName: string, force?: boolean): boolean;
753
754
  animate(): void;
754
755
  onanimationstart(): void;
755
756
  onanimationend(): void;
package/dist/index.mjs CHANGED
@@ -234,7 +234,7 @@ var MockCSSStyleDeclaration = class {
234
234
  this._styles.clear();
235
235
  return;
236
236
  }
237
- cssText.split(";").forEach((rule) => {
237
+ splitDeclarations(cssText).forEach((rule) => {
238
238
  rule = rule.trim();
239
239
  if (rule.length > 0) {
240
240
  const splt = rule.split(":");
@@ -247,6 +247,41 @@ var MockCSSStyleDeclaration = class {
247
247
  });
248
248
  }
249
249
  };
250
+ /**
251
+ * Splits a CSS declaration list at its top-level semicolons. A semicolon inside
252
+ * parentheses (e.g. the `;base64,` in `url(data:image/png;base64,...)`) or inside
253
+ * a quoted string does not end a declaration.
254
+ *
255
+ * @param cssText the declaration list, such as the value of a `style` attribute
256
+ * @returns the individual declarations, without their separating semicolons
257
+ */
258
+ function splitDeclarations(cssText) {
259
+ const declarations = [];
260
+ let current = "";
261
+ let depth = 0;
262
+ let quote = "";
263
+ for (let i = 0; i < cssText.length; i++) {
264
+ const char = cssText[i];
265
+ if (char === "\\" && i + 1 < cssText.length) {
266
+ current += char + cssText[++i];
267
+ continue;
268
+ }
269
+ if (quote) {
270
+ if (char === quote) quote = "";
271
+ } else if (char === "\"" || char === "'") quote = char;
272
+ else if (char === "(") depth++;
273
+ else if (char === ")") {
274
+ if (depth > 0) depth--;
275
+ } else if (char === ";" && depth === 0) {
276
+ declarations.push(current);
277
+ current = "";
278
+ continue;
279
+ }
280
+ current += char;
281
+ }
282
+ declarations.push(current);
283
+ return declarations;
284
+ }
250
285
  function createCSSStyleDeclaration() {
251
286
  return new Proxy(new MockCSSStyleDeclaration(), cssProxyHandler);
252
287
  }
@@ -1009,10 +1044,10 @@ function* streamToHtml(node, opts, output) {
1009
1044
  if (node.hasAttribute("style")) {
1010
1045
  const cssText = node.style.cssText;
1011
1046
  if (opts.approximateLineWidth && opts.approximateLineWidth > 0 && output.currentLineWidth + cssText.length + 10 > opts.approximateLineWidth) {
1012
- yield `\nstyle="${cssText}">`;
1047
+ yield `\nstyle="${escapeString(cssText, true)}">`;
1013
1048
  output.currentLineWidth = 0;
1014
1049
  } else {
1015
- yield ` style="${cssText}">`;
1050
+ yield ` style="${escapeString(cssText, true)}">`;
1016
1051
  output.currentLineWidth += cssText.length + 10;
1017
1052
  }
1018
1053
  } else {
@@ -1027,7 +1062,7 @@ function* streamToHtml(node, opts, output) {
1027
1062
  yield* streamToHtml(shadowRoot, opts, output);
1028
1063
  output.indent = output.indent - (opts.indentSpaces ?? 0);
1029
1064
  const childNodes = getChildNodes(node);
1030
- if (opts.newLines && (childNodes.length === 0 || childNodes.length === 1 && childNodes[0].nodeType === 3 && childNodes[0].nodeValue?.trim() === "")) {
1065
+ if (opts.newLines && (childNodes.length === 0 || childNodes.length === 1 && childNodes[0].nodeType === 3 && childNodes[0].nodeValue?.trim() === "" || hasOnlyRemovedComments(childNodes, opts))) {
1031
1066
  yield "\n";
1032
1067
  output.currentLineWidth = 0;
1033
1068
  for (let i = 0; i < output.indent; i++) yield " ";
@@ -1039,7 +1074,7 @@ function* streamToHtml(node, opts, output) {
1039
1074
  const childNodes = tagName === "template" ? node.content.childNodes : getChildNodes(node);
1040
1075
  const childNodeLength = childNodes.length;
1041
1076
  if (childNodeLength > 0) {
1042
- if (childNodeLength === 1 && childNodes[0].nodeType === 3 && (typeof childNodes[0].nodeValue !== "string" || childNodes[0].nodeValue.trim() === "")) {} else {
1077
+ if (childNodeLength === 1 && childNodes[0].nodeType === 3 && (typeof childNodes[0].nodeValue !== "string" || childNodes[0].nodeValue.trim() === "")) {} else if (hasOnlyRemovedComments(childNodes, opts)) {} else {
1043
1078
  const isWithinWhitespaceSensitiveNode = opts.newLines || (opts.indentSpaces ?? 0) > 0 ? isWithinWhitespaceSensitive(node) : false;
1044
1079
  if (!isWithinWhitespaceSensitiveNode && (opts.indentSpaces ?? 0) > 0 && ignoreTag === false) output.indent = output.indent + (opts.indentSpaces ?? 0);
1045
1080
  for (let i = 0; i < childNodeLength; i++) {
@@ -1130,12 +1165,11 @@ function* streamToHtml(node, opts, output) {
1130
1165
  }
1131
1166
  } else if (node.nodeType === 8) {
1132
1167
  const nodeValue = node.nodeValue;
1133
- const isHydrateAnnotation = nodeValue?.startsWith("r.") || nodeValue?.startsWith("o.") || nodeValue?.startsWith("s.") || nodeValue?.startsWith("t.");
1134
1168
  /**
1135
1169
  * remove comments from stringified output if user set the `removeHtmlComments` e.g.
1136
1170
  * in the `renderToString` function and we are no dealing with a hydrate annotation
1137
1171
  */
1138
- if (opts.removeHtmlComments && !isHydrateAnnotation) return;
1172
+ if (isRemovedComment(node, opts)) return;
1139
1173
  const isWithinWhitespaceSensitiveNode = opts.newLines || (opts.indentSpaces ?? 0) > 0 ? isWithinWhitespaceSensitive(node) : false;
1140
1174
  if (opts.newLines && !isWithinWhitespaceSensitiveNode) {
1141
1175
  yield "\n";
@@ -1181,6 +1215,38 @@ function isWithinWhitespaceSensitive(node) {
1181
1215
  return false;
1182
1216
  }
1183
1217
  /**
1218
+ * Determine whether a comment node is left out of the serialized output, which
1219
+ * happens when `removeHtmlComments` is set and the comment is not a hydrate
1220
+ * annotation.
1221
+ *
1222
+ * @param node a node to check
1223
+ * @param opts serialization options
1224
+ * @returns whether or not the node is a comment that will not be serialized
1225
+ */
1226
+ function isRemovedComment(node, opts) {
1227
+ if (node.nodeType !== 8 || !opts.removeHtmlComments) return false;
1228
+ const nodeValue = node.nodeValue;
1229
+ return !(nodeValue?.startsWith("r.") || nodeValue?.startsWith("o.") || nodeValue?.startsWith("s.") || nodeValue?.startsWith("t."));
1230
+ }
1231
+ /**
1232
+ * Determine whether a list of child nodes contains at least one comment that is
1233
+ * removed from the output and otherwise only whitespace text, so that serializing
1234
+ * it produces no content.
1235
+ *
1236
+ * @param childNodes the child nodes to check
1237
+ * @param opts serialization options
1238
+ * @returns whether or not the child nodes serialize to nothing
1239
+ */
1240
+ function hasOnlyRemovedComments(childNodes, opts) {
1241
+ let hasRemovedComment = false;
1242
+ for (let i = 0; i < childNodes.length; i++) {
1243
+ const childNode = childNodes[i];
1244
+ if (isRemovedComment(childNode, opts)) hasRemovedComment = true;
1245
+ else if (childNode.nodeType !== 3 || typeof childNode.nodeValue === "string" && childNode.nodeValue.trim() !== "") return false;
1246
+ }
1247
+ return hasRemovedComment;
1248
+ }
1249
+ /**
1184
1250
  * Normalizes the `childNodes` of a node due to if `experimentalSlotFixes` is enabled, `
1185
1251
  * childNodes` will only return 'slotted' / lightDOM nodes
1186
1252
  *
@@ -1987,6 +2053,15 @@ var MockElement = class extends MockNode {
1987
2053
  set title(value) {
1988
2054
  this.setAttributeNS(null, "title", value);
1989
2055
  }
2056
+ toggleAttribute(attrName, force) {
2057
+ const has = this.hasAttribute(attrName);
2058
+ if (force ?? !has) {
2059
+ if (!has) this.setAttribute(attrName, "");
2060
+ return true;
2061
+ }
2062
+ if (has) this.removeAttribute(attrName);
2063
+ return false;
2064
+ }
1990
2065
  animate() {}
1991
2066
  onanimationstart() {}
1992
2067
  onanimationend() {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stencil/mock-doc",
3
- "version": "5.0.0-rc.1",
3
+ "version": "5.0.0-rc.11",
4
4
  "description": "A minimal mock DOM implementation for SSR and testing",
5
5
  "keywords": [
6
6
  "dom",