single-file-core 1.14.3 → 1.15.0

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/helper.js CHANGED
@@ -70,6 +70,7 @@ const STYLESHEET_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "stylesheet";
70
70
  const LINK_STYLESHEET_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "link-stylesheet";
71
71
  const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "disabled-noscript";
72
72
  const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "selected-content";
73
+ const SELECTED_TEXT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "selected-text";
73
74
  const INVALID_ELEMENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "invalid-element";
74
75
  const ASYNC_SCRIPT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "async-script";
75
76
  const CUSTOM_STYLESHEET_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "custom-stylesheet";
@@ -254,6 +255,7 @@ export {
254
255
  STYLESHEET_ATTRIBUTE_NAME,
255
256
  LINK_STYLESHEET_ATTRIBUTE_NAME,
256
257
  SELECTED_CONTENT_ATTRIBUTE_NAME,
258
+ SELECTED_TEXT_ATTRIBUTE_NAME,
257
259
  INVALID_ELEMENT_ATTRIBUTE_NAME,
258
260
  ASYNC_SCRIPT_ATTRIBUTE_NAME,
259
261
  COMMENT_HEADER,
package/core/index.js CHANGED
@@ -594,6 +594,9 @@ class Processor {
594
594
  util.fixInvalidNesting(this.doc, true, { recreateOnly: true });
595
595
  removeInsertedParagraphs(this.doc);
596
596
  util.fixInvalidNesting(this.doc, true, { mergeCopies: true });
597
+ if (this.options.selected) {
598
+ removeUnselectedTexts(this.doc);
599
+ }
597
600
  if (this.options.saveRawPage) {
598
601
  let charset;
599
602
  this.doc.querySelectorAll("meta[charset]").forEach(element => {
@@ -1731,6 +1734,9 @@ class Processor {
1731
1734
  util.fixInvalidNesting(doc, true, { rootElement: templateElement, recreateOnly: true });
1732
1735
  removeInsertedParagraphs(templateElement);
1733
1736
  util.fixInvalidNesting(doc, true, { rootElement: templateElement, mergeCopies: true });
1737
+ if (options.selected) {
1738
+ removeUnselectedTexts(templateElement);
1739
+ }
1734
1740
  if (templateElement.querySelector(`[${util.NESTING_TRACK_ID_ATTRIBUTE_NAME}]`)) {
1735
1741
  templateElement.setAttribute(SHADOWROOT_ATTRIBUTE_NAME, "open");
1736
1742
  }
@@ -2163,6 +2169,29 @@ function getManifestOptions(options) {
2163
2169
  return manifestOptions;
2164
2170
  }
2165
2171
 
2172
+ function removeUnselectedTexts(rootNode) {
2173
+ rootNode.querySelectorAll("[" + util.SELECTED_TEXT_ATTRIBUTE_NAME + "]").forEach(element => {
2174
+ const selectedTextIndexes = element.getAttribute(util.SELECTED_TEXT_ATTRIBUTE_NAME).split(" ");
2175
+ element.removeAttribute(util.SELECTED_TEXT_ATTRIBUTE_NAME);
2176
+ let textIndex = 0, textNodes = [], textFound = false;
2177
+ Array.from(element.childNodes).concat([null]).forEach(node => {
2178
+ if (node && node.nodeType == 3) {
2179
+ textNodes.push(node);
2180
+ textFound = textFound || /\S/.test(node.textContent);
2181
+ } else {
2182
+ if (textFound) {
2183
+ if (!selectedTextIndexes.includes(String(textIndex))) {
2184
+ textNodes.forEach(textNode => textNode.remove());
2185
+ }
2186
+ textIndex++;
2187
+ }
2188
+ textNodes = [];
2189
+ textFound = false;
2190
+ }
2191
+ });
2192
+ });
2193
+ }
2194
+
2166
2195
  function removeInsertedParagraphs(doc) {
2167
2196
  getInsertedParagraphs(doc).forEach((_, paragraph) => paragraph.remove());
2168
2197
  }
package/core/infobar.js CHANGED
@@ -220,7 +220,7 @@ const INFOBAR_ANIMATIONS_STYLES = `
220
220
  }
221
221
  `;
222
222
 
223
- export { displayIcon, appendInfobar, refreshInfobarInfo, extractInfobarData, INFOBAR_TAGNAME };
223
+ export { displayIcon, appendInfobar, refreshInfobarInfo, extractInfobarData, updateInfobarContent, INFOBAR_TAGNAME };
224
224
 
225
225
  function appendInfobar(doc, options, useShadowRoot) {
226
226
  if (!doc.querySelector(INFOBAR_TAGNAME)) {
@@ -346,6 +346,22 @@ function extractInfobarData(doc, signature = SINGLE_FILE_SIGNATURE) {
346
346
  }
347
347
  }
348
348
 
349
+ function updateInfobarContent(doc, infobarContent, signature = SINGLE_FILE_SIGNATURE) {
350
+ const result = doc.evaluate("//comment()", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
351
+ const singleFileComment = result && result.singleNodeValue;
352
+ if (singleFileComment && singleFileComment.nodeType == Node.COMMENT_NODE && singleFileComment.textContent.includes(signature)) {
353
+ const info = singleFileComment.textContent.split("\n");
354
+ const headerLength = info[3] !== undefined && info[3].startsWith(" saved date: ") ? 4 : 3;
355
+ if (info.length > headerLength) {
356
+ const content = (infobarContent || "").split("\n").map(line => line.trim()).join("\n").trim();
357
+ const commentText = info.slice(0, headerLength).join("\n").trimEnd() + (content ? " \n info: " + content : "") + "\n";
358
+ singleFileComment.textContent = commentText.replace(/--(!?)>/g, "--$1 >");
359
+ return true;
360
+ }
361
+ }
362
+ return false;
363
+ }
364
+
349
365
  function refreshInfobarInfo(doc, { saveUrl, infobarContent, saveDate }) {
350
366
  if (saveUrl) {
351
367
  const infobarElement = doc.querySelector("single-file-infobar");
package/core/util.js CHANGED
@@ -242,6 +242,7 @@ function getInstance(utilOptions) {
242
242
  LINK_STYLESHEET_ATTRIBUTE_NAME: helper.LINK_STYLESHEET_ATTRIBUTE_NAME,
243
243
  CUSTOM_STYLESHEET_ATTRIBUTE_NAME: helper.CUSTOM_STYLESHEET_ATTRIBUTE_NAME,
244
244
  SELECTED_CONTENT_ATTRIBUTE_NAME: helper.SELECTED_CONTENT_ATTRIBUTE_NAME,
245
+ SELECTED_TEXT_ATTRIBUTE_NAME: helper.SELECTED_TEXT_ATTRIBUTE_NAME,
245
246
  INVALID_ELEMENT_ATTRIBUTE_NAME: helper.INVALID_ELEMENT_ATTRIBUTE_NAME,
246
247
  COMMENT_HEADER: helper.COMMENT_HEADER,
247
248
  COMMENT_HEADER_LEGACY: helper.COMMENT_HEADER_LEGACY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.14.3",
3
+ "version": "1.15.0",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -44,6 +44,9 @@ const helper = {
44
44
  extractInfobarData(doc) {
45
45
  return infobar.extractInfobarData(doc);
46
46
  },
47
+ updateInfobarContent(doc, infobarContent) {
48
+ return infobar.updateInfobarContent(doc, infobarContent);
49
+ },
47
50
  displayIcon(doc, useShadowRoot, options = {}) {
48
51
  return infobar.displayIcon(doc, useShadowRoot, options);
49
52
  },