single-file-core 1.14.1 → 1.14.2

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/core/index.js CHANGED
@@ -931,15 +931,16 @@ class Processor {
931
931
  const childElements = Array.from(element.childNodes).filter(node => node.nodeType == 1);
932
932
  const shadowRootElement = childElements.find(node => isUnmarkedShadowRoot(node));
933
933
  const partiallySelected = childElements.some(node => node != shadowRootElement && !isMarked(node));
934
- if (shadowRootElement && partiallySelected) {
935
- markAssignedSlots(element, shadowRootElement, childElements);
934
+ const shadowRootSelected = Boolean(shadowRootElement && shadowRootElement.querySelector("[" + util.SELECTED_CONTENT_ATTRIBUTE_NAME + "]"));
935
+ if (shadowRootElement && (partiallySelected || shadowRootSelected)) {
936
+ markAssignedSlots(element, shadowRootElement, childElements, shadowRootSelected);
936
937
  }
937
938
  Array.from(element.childNodes).forEach(node => {
938
939
  if (node.nodeType == 1) {
939
940
  const isSelectedElement = isMarked(node);
940
941
  selectedElementFound = selectedElementFound || isSelectedElement;
941
942
  if (node == shadowRootElement) {
942
- if (partiallySelected) {
943
+ if (partiallySelected || shadowRootSelected) {
943
944
  removeUnmarkedElements(node);
944
945
  }
945
946
  } else if (isSelectedElement) {
@@ -962,9 +963,9 @@ class Processor {
962
963
  return element.tagName.toUpperCase() == "TEMPLATE" && element.hasAttribute(SHADOWROOT_ATTRIBUTE_NAME) && !isMarked(element);
963
964
  }
964
965
 
965
- function markAssignedSlots(hostElement, shadowRootElement, childElements) {
966
+ function markAssignedSlots(hostElement, shadowRootElement, childElements, shadowRootSelected) {
966
967
  const slotNames = new Set(childElements.filter(isMarked).map(childElement => childElement.getAttribute("slot") || ""));
967
- if (Array.from(hostElement.childNodes).some(node => node.nodeType == 3 && node.textContent.trim())) {
968
+ if (!shadowRootSelected && Array.from(hostElement.childNodes).some(node => node.nodeType == 3 && node.textContent.trim())) {
968
969
  slotNames.add("");
969
970
  }
970
971
  shadowRootElement.querySelectorAll("slot").forEach(slotElement => {
@@ -66,6 +66,14 @@ const DEFAULT_IMAGE_QUALITY = 0.8;
66
66
  const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
67
67
  const XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
68
68
  const SVG_ROOT_ID_PREFIX = "sf-svg-";
69
+ const CSS_ESCAPE_CHARACTER = "\\";
70
+ const CSS_COMMENT_END = "*/";
71
+ const CSS_URL_END = ")";
72
+ const CSS_COMMENT_MIN_LENGTH = 4;
73
+ const CSS_BLOCK_END = "}";
74
+ const CSS_STATEMENT_END = ";";
75
+ const CSS_EMPTY_BLOCK = "{}";
76
+ const CSS_UNMATCHED_BLOCK_END = "]";
69
77
 
70
78
  let util, cssTree;
71
79
 
@@ -77,6 +85,7 @@ export {
77
85
  getCharset,
78
86
  getUrlFunctions,
79
87
  getImportFunctions,
88
+ closeStylesheetContent,
80
89
  isDataURL,
81
90
  replaceOriginalURLs,
82
91
  testIgnoredPath,
@@ -913,6 +922,85 @@ function getImportFunctions(declarationList) {
913
922
  return cssTree.findAll(declarationList, node => node.type == "Atrule" && node.name == "import");
914
923
  }
915
924
 
925
+ function closeStylesheetContent(content, nested) {
926
+ const tokenTypes = cssTree.tokenTypes;
927
+ const blockEnds = new Map([
928
+ [tokenTypes.Function, ")"],
929
+ [tokenTypes.LeftParenthesis, ")"],
930
+ [tokenTypes.LeftSquareBracket, "]"],
931
+ [tokenTypes.LeftCurlyBracket, CSS_BLOCK_END]
932
+ ]);
933
+ const blockEndTypes = new Map([
934
+ [tokenTypes.RightParenthesis, ")"],
935
+ [tokenTypes.RightSquareBracket, "]"],
936
+ [tokenTypes.RightCurlyBracket, CSS_BLOCK_END]
937
+ ]);
938
+ const ignoredTokenTypes = new Set([tokenTypes.WhiteSpace, tokenTypes.Comment, tokenTypes.CDO, tokenTypes.CDC]);
939
+ const openBlockEnds = [];
940
+ const unmatchedBlockEnds = [];
941
+ let lastTokenType, lastTokenStart = 0, openRule = false, openAtRule = false;
942
+ cssTree.tokenize(content, (tokenType, tokenStart) => {
943
+ lastTokenType = tokenType;
944
+ lastTokenStart = tokenStart;
945
+ if (!openBlockEnds.length && !ignoredTokenTypes.has(tokenType)) {
946
+ if (openRule && openAtRule && tokenType == tokenTypes.Semicolon) {
947
+ openRule = false;
948
+ } else if (!openRule) {
949
+ openRule = true;
950
+ openAtRule = tokenType == tokenTypes.AtKeyword;
951
+ }
952
+ }
953
+ if (blockEnds.has(tokenType)) {
954
+ openBlockEnds.push(blockEnds.get(tokenType));
955
+ } else if (blockEndTypes.has(tokenType) && openBlockEnds[openBlockEnds.length - 1] == blockEndTypes.get(tokenType)) {
956
+ openBlockEnds.pop();
957
+ if (!openBlockEnds.length && tokenType == tokenTypes.RightCurlyBracket) {
958
+ openRule = false;
959
+ }
960
+ } else if (!openBlockEnds.length && tokenType == tokenTypes.RightCurlyBracket) {
961
+ unmatchedBlockEnds.push(tokenStart);
962
+ }
963
+ });
964
+ if (nested) {
965
+ unmatchedBlockEnds.forEach(tokenStart => content = content.substring(0, tokenStart) + CSS_UNMATCHED_BLOCK_END + content.substring(tokenStart + 1));
966
+ }
967
+ const lastToken = content.substring(lastTokenStart);
968
+ let tokenEnd = "";
969
+ if (lastTokenType == tokenTypes.Comment) {
970
+ if (lastToken.length < CSS_COMMENT_MIN_LENGTH || !lastToken.endsWith(CSS_COMMENT_END)) {
971
+ tokenEnd = CSS_COMMENT_END;
972
+ }
973
+ } else if (lastTokenType !== undefined) {
974
+ const endsWithEscapeCharacter = isEscapedEnd(content);
975
+ if (endsWithEscapeCharacter) {
976
+ content = content.substring(0, content.length - 1);
977
+ }
978
+ if (lastTokenType == tokenTypes.String) {
979
+ const quote = lastToken[0];
980
+ if (endsWithEscapeCharacter || lastToken.length < 2 || !lastToken.endsWith(quote) || isEscapedEnd(lastToken.substring(0, lastToken.length - 1))) {
981
+ tokenEnd = quote;
982
+ }
983
+ } else if (lastTokenType == tokenTypes.Url || lastTokenType == tokenTypes.BadUrl) {
984
+ if (endsWithEscapeCharacter || !lastToken.endsWith(CSS_URL_END) || isEscapedEnd(lastToken.substring(0, lastToken.length - 1))) {
985
+ tokenEnd = CSS_URL_END;
986
+ }
987
+ }
988
+ }
989
+ let ruleEnd = "";
990
+ if (openRule && openBlockEnds[0] != CSS_BLOCK_END) {
991
+ ruleEnd = openAtRule ? CSS_STATEMENT_END : CSS_EMPTY_BLOCK;
992
+ }
993
+ return content + tokenEnd + openBlockEnds.reverse().join("") + ruleEnd;
994
+ }
995
+
996
+ function isEscapedEnd(content) {
997
+ let escapeCharactersCount = 0;
998
+ while (content[content.length - 1 - escapeCharactersCount] == CSS_ESCAPE_CHARACTER) {
999
+ escapeCharactersCount++;
1000
+ }
1001
+ return escapeCharactersCount % 2 == 1;
1002
+ }
1003
+
916
1004
  function findShortcutIcon(shortcutIcons) {
917
1005
  shortcutIcons = shortcutIcons.filter(linkElement => linkElement.href != util.EMPTY_RESOURCE);
918
1006
  shortcutIcons.sort((linkElement1, linkElement2) => (parseInt(linkElement2.sizes, 10) || 16) - (parseInt(linkElement1.sizes, 10) || 16));
@@ -69,6 +69,7 @@ import {
69
69
  getCharset,
70
70
  getUrlFunctions,
71
71
  getImportFunctions,
72
+ closeStylesheetContent,
72
73
  isDataURL,
73
74
  replaceOriginalURLs,
74
75
  testIgnoredPath,
@@ -230,14 +231,15 @@ function getProcessorHelperClass(utilInstance) {
230
231
  content.data = "";
231
232
  }
232
233
  const importedLayerName = getImportedLayerName(node);
234
+ const mediaQueryListNode = cssTree.find(node, node => node.type == "MediaQueryList");
235
+ const importedSupportsCondition = getImportedSupportsCondition(node);
236
+ content.data = closeStylesheetContent(content.data || "", Boolean(importedLayerName !== null || mediaQueryListNode || importedSupportsCondition));
233
237
  if (importedLayerName !== null) {
234
238
  content.data = this.wrapLayer(content.data, importedLayerName);
235
239
  }
236
- const mediaQueryListNode = cssTree.find(node, node => node.type == "MediaQueryList");
237
240
  if (mediaQueryListNode) {
238
241
  content.data = this.wrapMediaQuery(content.data, cssTree.generate(mediaQueryListNode));
239
242
  }
240
- const importedSupportsCondition = getImportedSupportsCondition(node);
241
243
  if (importedSupportsCondition) {
242
244
  content.data = "@supports " + importedSupportsCondition + " { " + content.data + " }";
243
245
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.14.1",
3
+ "version": "1.14.2",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",