weml-monaco 0.4.2 → 0.4.3

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.mjs CHANGED
@@ -6130,7 +6130,13 @@ var require_wemlContentNormalizeV2 = __commonJS({
6130
6130
  "wbr",
6131
6131
  "param"
6132
6132
  ]);
6133
- var VOID_TAG_RE = /<[ \t\n\f\r]*(area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr|param)(?=[ \t\n\f\r/>])((?:[^<>"']|"[^"]*"|'[^']*')*?)>/gi;
6133
+ var EMPTY_TEXT_WRAPPERS = /* @__PURE__ */ new Set([
6134
+ "w-format",
6135
+ "w-lang",
6136
+ "w-color",
6137
+ "w-non-egw",
6138
+ "w-sent"
6139
+ ]);
6134
6140
  var ENTITY_RE2 = /&(#[xX][0-9a-fA-F]+|#[0-9]+|[a-zA-Z][a-zA-Z0-9]*);?/g;
6135
6141
  var PH_OPEN = "\uE000";
6136
6142
  var PH_CLOSE = "\uE001";
@@ -6171,6 +6177,7 @@ var require_wemlContentNormalizeV2 = __commonJS({
6171
6177
  function formatWeml(input) {
6172
6178
  const { html, entities: entities2, marker } = protectEntities(input);
6173
6179
  const doc = parse2(normalizeVoidTagSyntax(html));
6180
+ removeEmptyWrappers(doc);
6174
6181
  const title = doc.querySelector("head title");
6175
6182
  if (title) processInlineContent(title, true);
6176
6183
  const body = doc.querySelector("body");
@@ -6216,47 +6223,135 @@ var require_wemlContentNormalizeV2 = __commonJS({
6216
6223
  }
6217
6224
  function normalizeVoidTagSyntax(input) {
6218
6225
  function slashBelongsToUnquotedValue(attrs) {
6219
- let index = 0;
6220
- const whitespace = /* @__PURE__ */ new Set([" ", " ", "\n", "\f", "\r"]);
6221
- while (index < attrs.length) {
6222
- while (index < attrs.length && whitespace.has(attrs[index])) index += 1;
6223
- if (index >= attrs.length || attrs[index] === "/") return false;
6224
- while (index < attrs.length && !whitespace.has(attrs[index]) && !["=", "/"].includes(attrs[index])) index += 1;
6225
- while (index < attrs.length && whitespace.has(attrs[index])) index += 1;
6226
- if (index >= attrs.length || attrs[index] !== "=") continue;
6227
- index += 1;
6228
- while (index < attrs.length && whitespace.has(attrs[index])) index += 1;
6229
- if (index >= attrs.length) return false;
6230
- if (attrs[index] === '"' || attrs[index] === "'") {
6231
- const quote = attrs[index];
6232
- index += 1;
6233
- while (index < attrs.length && attrs[index] !== quote) index += 1;
6234
- if (index < attrs.length) index += 1;
6226
+ let index2 = 0;
6227
+ const whitespace = /* @__PURE__ */ new Set([" ", " ", "\n", "\f", "\r", "\v"]);
6228
+ while (index2 < attrs.length) {
6229
+ while (index2 < attrs.length && whitespace.has(attrs[index2])) index2 += 1;
6230
+ if (index2 >= attrs.length || attrs[index2] === "/") return false;
6231
+ while (index2 < attrs.length && !whitespace.has(attrs[index2]) && !["=", "/"].includes(attrs[index2])) index2 += 1;
6232
+ while (index2 < attrs.length && whitespace.has(attrs[index2])) index2 += 1;
6233
+ if (index2 >= attrs.length || attrs[index2] !== "=") continue;
6234
+ index2 += 1;
6235
+ while (index2 < attrs.length && whitespace.has(attrs[index2])) index2 += 1;
6236
+ if (index2 >= attrs.length) return false;
6237
+ if (attrs[index2] === '"' || attrs[index2] === "'") {
6238
+ const quote = attrs[index2];
6239
+ index2 += 1;
6240
+ while (index2 < attrs.length && attrs[index2] !== quote) index2 += 1;
6241
+ if (index2 < attrs.length) index2 += 1;
6235
6242
  continue;
6236
6243
  }
6237
- const valueStart = index;
6238
- while (index < attrs.length && !whitespace.has(attrs[index])) index += 1;
6239
- if (index === attrs.length) {
6244
+ const valueStart = index2;
6245
+ while (index2 < attrs.length && !whitespace.has(attrs[index2])) index2 += 1;
6246
+ if (index2 === attrs.length) {
6240
6247
  return valueStart < attrs.length && attrs.endsWith("/");
6241
6248
  }
6242
6249
  }
6243
6250
  return false;
6244
6251
  }
6245
- return input.replace(VOID_TAG_RE, (_, name, rawAttrs) => {
6246
- let attrs = trimRawEndWhitespace(rawAttrs);
6247
- if (attrs.endsWith("/") && !slashBelongsToUnquotedValue(attrs)) {
6248
- attrs = trimRawEndWhitespace(attrs.slice(0, -1));
6252
+ const result = [];
6253
+ let index = 0;
6254
+ while (index < input.length) {
6255
+ if (input[index] !== "<") {
6256
+ result.push(input[index++]);
6257
+ continue;
6249
6258
  }
6250
- return `<${name.toLowerCase()}${attrs} />`;
6251
- });
6259
+ const start = index;
6260
+ if (input.startsWith("<!--", index) || input.startsWith("<?", index)) {
6261
+ const delimiter = input.startsWith("<!--", index) ? "-->" : "?>";
6262
+ let end2 = input.indexOf(delimiter, index + 2);
6263
+ end2 = end2 < 0 ? input.length : end2 + delimiter.length;
6264
+ result.push(input.slice(start, end2));
6265
+ index = end2;
6266
+ continue;
6267
+ }
6268
+ const { end } = findTagEnd(input, index + 1);
6269
+ if (end < 0) {
6270
+ result.push(input.slice(index));
6271
+ break;
6272
+ }
6273
+ let cursor = index + 1;
6274
+ if (input[cursor] === "!") {
6275
+ result.push(input.slice(start, end + 1));
6276
+ index = end + 1;
6277
+ continue;
6278
+ }
6279
+ const closing = input[cursor] === "/";
6280
+ if (closing) cursor++;
6281
+ if (cursor >= end || !isNameStart(input[cursor])) {
6282
+ result.push(input[index++]);
6283
+ continue;
6284
+ }
6285
+ const nameStart = cursor++;
6286
+ while (cursor < end && isNameChar(input[cursor])) cursor++;
6287
+ const name = input.slice(nameStart, cursor);
6288
+ const attributes = [];
6289
+ let quote = null;
6290
+ let inSpace = false;
6291
+ for (let offset = cursor; offset < end; offset++) {
6292
+ const character = input[offset];
6293
+ if (quote !== null) {
6294
+ attributes.push(character);
6295
+ if (character === quote) quote = null;
6296
+ continue;
6297
+ }
6298
+ if (isSourceSpace(character)) {
6299
+ if (!inSpace) attributes.push(" ");
6300
+ inSpace = true;
6301
+ } else {
6302
+ inSpace = false;
6303
+ attributes.push(character);
6304
+ if (character === '"' || character === "'") quote = character;
6305
+ }
6306
+ }
6307
+ let attrs = attributes.join("").replace(/ +$/, "");
6308
+ if (!closing && VOID_HTML_ELEMENTS.has(name.toLowerCase())) {
6309
+ if (attrs.endsWith("/") && !slashBelongsToUnquotedValue(attrs)) {
6310
+ attrs = attrs.slice(0, -1).replace(/ +$/, "");
6311
+ }
6312
+ result.push(`<${name.toLowerCase()}${attrs} />`);
6313
+ } else {
6314
+ result.push(input.slice(start, cursor), attrs, ">");
6315
+ }
6316
+ index = end + 1;
6317
+ }
6318
+ return result.join("");
6252
6319
  }
6253
- function processBlockElement(node) {
6320
+ function removeEmptyWrappers(node) {
6254
6321
  for (const child of [...node.childNodes]) {
6255
- if (child.nodeType !== 3) continue;
6256
- const normalized = trimWhitespace(child.rawText);
6257
- if (normalized) child.rawText = normalized;
6258
- else child.remove();
6322
+ if (child.nodeType === 8) {
6323
+ node.removeChild(child);
6324
+ continue;
6325
+ }
6326
+ if (child.nodeType !== 1) continue;
6327
+ removeEmptyWrappers(child);
6328
+ const hasId = Object.keys(child.rawAttributes).some((name) => name.toLowerCase() === "id");
6329
+ if (EMPTY_TEXT_WRAPPERS.has(child.rawTagName.toLowerCase()) && !hasId && child.childNodes.every((item) => item.nodeType === 3 && isWhitespaceOnly(item.rawText))) {
6330
+ const replacement = new TextNode(child.childNodes.some((item) => item.rawText.length > 0) ? " " : "", node);
6331
+ node.exchangeChild(child, replacement);
6332
+ }
6259
6333
  }
6334
+ let previous = null;
6335
+ for (const child of [...node.childNodes]) {
6336
+ if (child.nodeType === 3) {
6337
+ if (previous !== null) {
6338
+ previous.rawText += child.rawText;
6339
+ node.removeChild(child);
6340
+ } else previous = child;
6341
+ } else previous = null;
6342
+ }
6343
+ }
6344
+ function processBlockElement(node) {
6345
+ node.childNodes = node.childNodes.filter((child) => {
6346
+ if (child.nodeType !== 3) return true;
6347
+ const normalized = trimWhitespace(child.rawText);
6348
+ if (normalized) {
6349
+ child.rawText = normalized;
6350
+ return true;
6351
+ }
6352
+ child.parentNode = null;
6353
+ return false;
6354
+ });
6260
6355
  for (const child of [...node.childNodes]) {
6261
6356
  if (child.nodeType !== 1) continue;
6262
6357
  const name = child.rawTagName.toLowerCase();
@@ -6511,7 +6606,7 @@ var require_wemlContentNormalizeV2 = __commonJS({
6511
6606
  }
6512
6607
  canonical.set(
6513
6608
  name,
6514
- SPACE_SEPARATED_ATTRIBUTES.has(name) ? normalizeTokenList(value, entities2, marker) : value
6609
+ SPACE_SEPARATED_ATTRIBUTES.has(name) ? normalizeTokenList(value, entities2, marker) : trimWhitespace(value)
6515
6610
  );
6516
6611
  }
6517
6612
  return [...canonical.entries()].sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([name, value]) => ` ${name}="${encodeAttr(String(value))}"`).join("");
@@ -6741,7 +6836,7 @@ var require_wemlContentNormalizeV2 = __commonJS({
6741
6836
  return isNameStart(character) || character >= "0" && character <= "9" || character === "-" || character === "_" || character === ":" || character === ".";
6742
6837
  }
6743
6838
  function isSourceSpace(character) {
6744
- return character === " " || character === " " || character === "\n" || character === "\r" || character === "\f" || character === "\v";
6839
+ return isNormalizableWhitespace(character);
6745
6840
  }
6746
6841
  function withoutWhiteSpace(text) {
6747
6842
  return Array.from(text, (character) => WHITE_SPACE.has(character.codePointAt(0)) ? "" : character).join("");
@@ -6772,13 +6867,6 @@ var require_wemlContentNormalizeV2 = __commonJS({
6772
6867
  function trimWhitespace(text) {
6773
6868
  return collapseWhitespace(text).replace(/^ +| +$/g, "");
6774
6869
  }
6775
- function trimRawEndWhitespace(text) {
6776
- const characters = Array.from(text);
6777
- while (characters.length > 0 && isNormalizableWhitespace(characters.at(-1))) {
6778
- characters.pop();
6779
- }
6780
- return characters.join("");
6781
- }
6782
6870
  function isWhitespaceOnly(text) {
6783
6871
  return Array.from(text).every(isNormalizableWhitespace);
6784
6872
  }
@@ -30826,24 +30914,22 @@ function getCompletionContext(text, offset, nodeStart) {
30826
30914
  switch (token) {
30827
30915
  case TokenType.StartTagOpen:
30828
30916
  if (scanner.getTokenEnd() === offset) {
30829
- const end = scanNextEnd(scanner, offset, TokenType.StartTag);
30830
- return { kind: "tag", start: offset - 1, end, includesBracket: true };
30917
+ return { kind: "tag", start: offset - 1, end: offset, includesBracket: true };
30831
30918
  }
30832
30919
  break;
30833
30920
  case TokenType.EndTagOpen:
30834
30921
  if (scanner.getTokenEnd() === offset) {
30835
- const end = scanNextEnd(scanner, offset, TokenType.EndTag);
30836
- return { kind: "end-tag", start: offset, end };
30922
+ return { kind: "end-tag", start: offset, end: offset };
30837
30923
  }
30838
30924
  break;
30839
30925
  case TokenType.EndTag:
30840
30926
  if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
30841
- return { kind: "end-tag", start: scanner.getTokenOffset(), end: scanner.getTokenEnd() };
30927
+ return { kind: "end-tag", start: scanner.getTokenOffset(), end: offset };
30842
30928
  }
30843
30929
  break;
30844
30930
  case TokenType.StartTag:
30845
30931
  if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
30846
- return { kind: "tag", start: scanner.getTokenOffset(), end: scanner.getTokenEnd(), includesBracket: false };
30932
+ return { kind: "tag", start: scanner.getTokenOffset(), end: offset, includesBracket: false };
30847
30933
  }
30848
30934
  currentTag = scanner.getTokenText();
30849
30935
  break;
@@ -30881,8 +30967,7 @@ function getCompletionContext(text, offset, nodeStart) {
30881
30967
  switch (scanner.getScannerState()) {
30882
30968
  case ScannerState.AfterOpeningStartTag: {
30883
30969
  const start = scanner.getTokenOffset();
30884
- const end = scanNextEnd(scanner, offset, TokenType.StartTag);
30885
- return { kind: "tag", start, end, includesBracket: false };
30970
+ return { kind: "tag", start, end: offset, includesBracket: false };
30886
30971
  }
30887
30972
  case ScannerState.WithinTag:
30888
30973
  case ScannerState.AfterAttributeName: