get-selection-more
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -1,2 +0,2 @@ | ||
function e(e=window){const t=(e.getSelection()||"").toString().trim();if(t)return t;const n=e.document.activeElement;if(n&&("INPUT"===n.tagName||"TEXTAREA"===n.tagName)){const e=n;return e.value.slice(e.selectionStart||0,e.selectionEnd||0)}return""}function t(e=window){const t=e.getSelection();if(!t||t.rangeCount<=0)return"";const n=t.toString();if(!n.trim())return"";const r=t.getRangeAt(0);return r?(c(r)+n+s(r)).replace(/\s+/g," ").trim():""}function n(e=window){const t=e.getSelection();if(!t||t.rangeCount<=0)return"";const n=t.toString();if(!n.trim())return"";const r=t.getRangeAt(0);return r?(function(e){if(e){const t=/[.?!。?!…]/,n=/[^\s.?!。?!…]/;for(let c=e.length-1;c>=0;c--){const s=e[c];if(t.test(s)){if("."===s&&n.test(e[c+1]))continue;return e.slice(c+1)}}}return e}(c(r))+n+function(e){const t=/^((\.(?![\s.?!。?!…]))|[^.?!。?!…])*([.?!。?!…]){0,3}/.exec(e);return t?t[0]:""}(s(r))).replace(/\s+/g," ").trim():""}function c(e){let t=e.startContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:{const c=t.textContent;c&&(n=c.slice(0,e.startOffset));break}case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.startOffset]}for(let e=t;o(e);e=e.parentElement)for(let t=e.previousSibling;o(t);t=t.previousSibling)n=r(t)+n;return n}function s(e){let t=e.endContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:{const c=t.textContent;c&&(n=c.slice(e.endOffset));break}case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.endOffset-1]}for(let e=t;o(e);e=e.parentElement)for(let t=e.nextSibling;o(t);t=t.nextSibling)n+=r(t);return n}function r(e){return e.nodeType===Node.TEXT_NODE?e.nodeValue:e.nodeType===Node.ELEMENT_NODE&&e.innerText||""}function o(e){if(!e)return!1;switch(e.nodeType){case Node.TEXT_NODE:case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:return!0;case Node.ELEMENT_NODE:switch(e.tagName){case"A":case"ABBR":case"B":case"BDI":case"BDO":case"BR":case"CITE":case"CODE":case"DATA":case"DFN":case"EM":case"I":case"KBD":case"MARK":case"Q":case"RP":case"RT":case"RTC":case"RUBY":case"S":case"SAMP":case"SMALL":case"SPAN":case"STRONG":case"SUB":case"SUP":case"TIME":case"U":case"VAR":case"WBR":return!0}}return!1}export{t as getParagraph,n as getSentence,e as getText}; | ||
function e(e,t=window){if(e){const t=e.toString().trim();if(t)return t}const n=t.document.activeElement;if(n&&("INPUT"===n.tagName||"TEXTAREA"===n.tagName)){const e=n;return e.value.slice(e.selectionStart||0,e.selectionEnd||0)}return""}function t(t=window){return e(t.getSelection(),t)}function n(e){if(!e||e.rangeCount<=0)return"";const t=e.toString();if(!t.trim())return"";const n=e.getRangeAt(0);return n?(o(n)+t+a(n)).replace(/\s+/g," ").trim():""}function c(e=window){return n(e.getSelection())}function s(e){if(!e||e.rangeCount<=0)return"";const t=e.toString();if(!t.trim())return"";const n=e.getRangeAt(0);return n?(function(e){if(e){const t=/[.?!。?!…]/,n=/[^\s.?!。?!…]/;for(let c=e.length-1;c>=0;c--){const s=e[c];if(t.test(s)){if("."===s&&n.test(e[c+1]))continue;return e.slice(c+1)}}}return e}(o(n))+t+function(e){const t=/^((\.(?![\s.?!。?!…]))|[^.?!。?!…])*([.?!。?!…]){0,3}/.exec(e);return t?t[0]:""}(a(n))).replace(/\s+/g," ").trim():""}function r(e=window){return s(e.getSelection())}function o(e){let t=e.startContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:{const c=t.textContent;c&&(n=c.slice(0,e.startOffset));break}case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.startOffset]}for(let e=t;u(e);e=e.parentElement)for(let t=e.previousSibling;u(t);t=t.previousSibling)n=i(t)+n;return n}function a(e){let t=e.endContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:{const c=t.textContent;c&&(n=c.slice(e.endOffset));break}case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.endOffset-1]}for(let e=t;u(e);e=e.parentElement)for(let t=e.nextSibling;u(t);t=t.nextSibling)n+=i(t);return n}function i(e){return e.nodeType===Node.TEXT_NODE?e.nodeValue||"":e.nodeType===Node.ELEMENT_NODE&&e.innerText||""}function u(e){if(!e)return!1;switch(e.nodeType){case Node.TEXT_NODE:case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:return!0;case Node.ELEMENT_NODE:switch(e.tagName){case"A":case"ABBR":case"B":case"BDI":case"BDO":case"BR":case"CITE":case"CODE":case"DATA":case"DFN":case"EM":case"I":case"KBD":case"MARK":case"Q":case"RP":case"RT":case"RTC":case"RUBY":case"S":case"SAMP":case"SMALL":case"SPAN":case"STRONG":case"SUB":case"SUP":case"TIME":case"U":case"VAR":case"WBR":return!0}}return!1}export{c as getParagraph,n as getParagraphFromSelection,r as getSentence,s as getSentenceFromSelection,t as getText,e as getTextFromSelection}; | ||
//# sourceMappingURL=get-selection-more.es.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).getSelectionMore={})}(this,function(e){"use strict";function t(e){var t=e.startContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:var c=t.textContent;c&&(n=c.slice(0,e.startOffset));break;case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.startOffset]}for(var s=t;a(s);s=s.parentElement)for(var i=s.previousSibling;a(i);i=i.previousSibling)n=r(i)+n;return n}function n(e){var t=e.endContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:var c=t.textContent;c&&(n=c.slice(e.endOffset));break;case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.endOffset-1]}for(var s=t;a(s);s=s.parentElement)for(var i=s.nextSibling;a(i);i=i.nextSibling)n+=r(i);return n}function r(e){return e.nodeType===Node.TEXT_NODE?e.nodeValue:e.nodeType===Node.ELEMENT_NODE&&e.innerText||""}function a(e){if(!e)return!1;switch(e.nodeType){case Node.TEXT_NODE:case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:return!0;case Node.ELEMENT_NODE:switch(e.tagName){case"A":case"ABBR":case"B":case"BDI":case"BDO":case"BR":case"CITE":case"CODE":case"DATA":case"DFN":case"EM":case"I":case"KBD":case"MARK":case"Q":case"RP":case"RT":case"RTC":case"RUBY":case"S":case"SAMP":case"SMALL":case"SPAN":case"STRONG":case"SUB":case"SUP":case"TIME":case"U":case"VAR":case"WBR":return!0}}return!1}e.getParagraph=function(e){void 0===e&&(e=window);var r=e.getSelection();if(!r||r.rangeCount<=0)return"";var a=r.toString();if(!a.trim())return"";var c=r.getRangeAt(0);return c?(t(c)+a+n(c)).replace(/\s+/g," ").trim():""},e.getSentence=function(e){void 0===e&&(e=window);var r=e.getSelection();if(!r||r.rangeCount<=0)return"";var a=r.toString();if(!a.trim())return"";var c,s,i=r.getRangeAt(0);return i?(function(e){if(e)for(var t=/[.?!。?!…]/,n=/[^\s.?!。?!…]/,r=e.length-1;r>=0;r--){var a=e[r];if(t.test(a)){if("."===a&&n.test(e[r+1]))continue;return e.slice(r+1)}}return e}(t(i))+a+(c=n(i),s=/^((\.(?![\s.?!。?!…]))|[^.?!。?!…])*([.?!。?!…]){0,3}/.exec(c),s?s[0]:"")).replace(/\s+/g," ").trim():""},e.getText=function(e){void 0===e&&(e=window);var t=(e.getSelection()||"").toString().trim();if(t)return t;var n=e.document.activeElement;if(n&&("INPUT"===n.tagName||"TEXTAREA"===n.tagName)){var r=n;return r.value.slice(r.selectionStart||0,r.selectionEnd||0)}return""},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).getSelectionMore={})}(this,function(e){"use strict";function t(e,t){if(void 0===t&&(t=window),e){var n=e.toString().trim();if(n)return n}var r=t.document.activeElement;if(r&&("INPUT"===r.tagName||"TEXTAREA"===r.tagName)){var a=r;return a.value.slice(a.selectionStart||0,a.selectionEnd||0)}return""}function n(e){if(!e||e.rangeCount<=0)return"";var t=e.toString();if(!t.trim())return"";var n=e.getRangeAt(0);return n?(a(n)+t+c(n)).replace(/\s+/g," ").trim():""}function r(e){if(!e||e.rangeCount<=0)return"";var t=e.toString();if(!t.trim())return"";var n,r,o=e.getRangeAt(0);return o?(function(e){if(e)for(var t=/[.?!。?!…]/,n=/[^\s.?!。?!…]/,r=e.length-1;r>=0;r--){var a=e[r];if(t.test(a)){if("."===a&&n.test(e[r+1]))continue;return e.slice(r+1)}}return e}(a(o))+t+(n=c(o),r=/^((\.(?![\s.?!。?!…]))|[^.?!。?!…])*([.?!。?!…]){0,3}/.exec(n),r?r[0]:"")).replace(/\s+/g," ").trim():""}function a(e){var t=e.startContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:var r=t.textContent;r&&(n=r.slice(0,e.startOffset));break;case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.startOffset]}for(var a=t;i(a);a=a.parentElement)for(var c=a.previousSibling;i(c);c=c.previousSibling)n=o(c)+n;return n}function c(e){var t=e.endContainer,n="";switch(t.nodeType){case Node.TEXT_NODE:var r=t.textContent;r&&(n=r.slice(e.endOffset));break;case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:break;default:t=t.childNodes[e.endOffset-1]}for(var a=t;i(a);a=a.parentElement)for(var c=a.nextSibling;i(c);c=c.nextSibling)n+=o(c);return n}function o(e){return e.nodeType===Node.TEXT_NODE?e.nodeValue||"":e.nodeType===Node.ELEMENT_NODE&&e.innerText||""}function i(e){if(!e)return!1;switch(e.nodeType){case Node.TEXT_NODE:case Node.COMMENT_NODE:case Node.CDATA_SECTION_NODE:return!0;case Node.ELEMENT_NODE:switch(e.tagName){case"A":case"ABBR":case"B":case"BDI":case"BDO":case"BR":case"CITE":case"CODE":case"DATA":case"DFN":case"EM":case"I":case"KBD":case"MARK":case"Q":case"RP":case"RT":case"RTC":case"RUBY":case"S":case"SAMP":case"SMALL":case"SPAN":case"STRONG":case"SUB":case"SUP":case"TIME":case"U":case"VAR":case"WBR":return!0}}return!1}e.getParagraph=function(e){return void 0===e&&(e=window),n(e.getSelection())},e.getParagraphFromSelection=n,e.getSentence=function(e){return void 0===e&&(e=window),r(e.getSelection())},e.getSentenceFromSelection=r,e.getText=function(e){return void 0===e&&(e=window),t(e.getSelection(),e)},e.getTextFromSelection=t,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=get-selection-more.umd.js.map |
/** | ||
* Returns the selected text | ||
*/ | ||
export function getText(win = window) { | ||
export function getTextFromSelection(selection, win = window) { | ||
// When called on an <iframe> that is not displayed (eg. where display: none is set) | ||
// Firefox will return null, whereas other browsers will return a Selection object | ||
// with Selection.type set to None. | ||
const selection = (win.getSelection() || '').toString().trim(); | ||
if (selection) { | ||
return selection; | ||
const text = selection.toString().trim(); | ||
if (text) { | ||
return text; | ||
} | ||
} | ||
@@ -25,6 +27,11 @@ // Currently getSelection() doesn't work on the content of <input> elements in Firefox | ||
/** | ||
* Returns the selected text | ||
*/ | ||
export function getText(win = window) { | ||
return getTextFromSelection(win.getSelection(), win); | ||
} | ||
/** | ||
* Returns the paragraph containing the selection text. | ||
*/ | ||
export function getParagraph(win = window) { | ||
const selection = win.getSelection(); | ||
export function getParagraphFromSelection(selection) { | ||
if (!selection || selection.rangeCount <= 0) { | ||
@@ -48,6 +55,11 @@ return ''; | ||
/** | ||
* Returns the paragraph containing the selection text. | ||
*/ | ||
export function getParagraph(win = window) { | ||
return getParagraphFromSelection(win.getSelection()); | ||
} | ||
/** | ||
* Returns the sentence containing the selection text. | ||
*/ | ||
export function getSentence(win = window) { | ||
const selection = win.getSelection(); | ||
export function getSentenceFromSelection(selection) { | ||
if (!selection || selection.rangeCount <= 0) { | ||
@@ -72,2 +84,8 @@ return ''; | ||
} | ||
/** | ||
* Returns the sentence containing the selection text. | ||
*/ | ||
export function getSentence(win = window) { | ||
return getSentenceFromSelection(win.getSelection()); | ||
} | ||
function extractParagraphHead(range) { | ||
@@ -150,3 +168,3 @@ let startNode = range.startContainer; | ||
if (node.nodeType === Node.TEXT_NODE) { | ||
return node.nodeValue; | ||
return node.nodeValue || /* istanbul ignore next */ ''; | ||
} | ||
@@ -153,0 +171,0 @@ else if (node.nodeType === Node.ELEMENT_NODE) { |
/** | ||
* Returns the selected text | ||
*/ | ||
export declare function getText(win?: Window): string; | ||
export declare function getTextFromSelection(selection: Selection | null, win?: Window & typeof globalThis): string; | ||
/** | ||
* Returns the selected text | ||
*/ | ||
export declare function getText(win?: Window & typeof globalThis): string; | ||
/** | ||
* Returns the paragraph containing the selection text. | ||
*/ | ||
export declare function getParagraph(win?: Window): string; | ||
export declare function getParagraphFromSelection(selection: Selection | null): string; | ||
/** | ||
* Returns the paragraph containing the selection text. | ||
*/ | ||
export declare function getParagraph(win?: Window & typeof globalThis): string; | ||
/** | ||
* Returns the sentence containing the selection text. | ||
*/ | ||
export declare function getSentence(win?: Window): string; | ||
export declare function getSentenceFromSelection(selection: Selection | null): string; | ||
/** | ||
* Returns the sentence containing the selection text. | ||
*/ | ||
export declare function getSentence(win?: Window & typeof globalThis): string; |
{ | ||
"name": "get-selection-more", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Get text and context sentence from window.getSelection()", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
# Get Selection More [](https://www.npmjs.com/package/get-selection-more) [](https://travis-ci.org/crimx/get-selection-more) [](https://coveralls.io/github/crimx/get-selection-more?branch=master) | ||
# Get Selection More [](https://www.npmjs.com/package/get-selection-more) [](https://travis-ci.com/crimx/get-selection-more) [](https://coveralls.io/github/crimx/get-selection-more?branch=master) | ||
@@ -9,11 +9,14 @@ ## APIs | ||
*/ | ||
export declare function getText(win?: Window): string; | ||
function getText(win?: Window): string | ||
function getTextFromSelection(selection: Selection | null, win?: Window): string | ||
/** | ||
* Returns the paragraph containing the selection text. | ||
*/ | ||
export declare function getParagraph(win?: Window): string; | ||
function getParagraph(win?: Window): string | ||
function getParagraphFromSelection(selection: Selection | null): string | ||
/** | ||
* Returns the sentence containing the selection text. | ||
*/ | ||
export declare function getSentence(win?: Window): string; | ||
function getSentence(win?: Window): string | ||
function getSentenceFromSelection(selection: Selection | null): string | ||
``` | ||
@@ -20,0 +23,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43542
265
36