@ultraq/dom-utils
Advanced tools
+5
-5
@@ -85,3 +85,3 @@ "use strict"; | ||
| /** | ||
| * Serialize a document fragment into an HTML string. | ||
| * Serialize a document or document fragment into an HTML string. | ||
| * | ||
@@ -94,15 +94,15 @@ * @param {Document|DocumentFragment} documentOrFragment | ||
| function serialize(documentOrFragment) { | ||
| if (documentOrFragment instanceof Document) { | ||
| if (documentOrFragment.nodeType === Node.DOCUMENT_NODE) { | ||
| var result = ''; | ||
| var contentType = documentOrFragment.contentType, | ||
| docType = documentOrFragment.docType, | ||
| firstChild = documentOrFragment.firstChild; | ||
| firstElementChild = documentOrFragment.firstElementChild; | ||
| if (docType) { | ||
| result += "<!DOCTYPE ".concat(docType.name, ">"); | ||
| } else if (firstChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| } else if (firstElementChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| result += "<!DOCTYPE html>"; | ||
| } | ||
| result += firstChild.outerHTML; | ||
| result += firstElementChild.outerHTML; | ||
| return result; | ||
@@ -109,0 +109,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["dom-utils.js"],"names":[],"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;AASO,SAAS,gBAAT,CAA0B,OAA1B,EAAmC,SAAnC,EAA8C,QAA9C,EAAwD,OAAxD,EAAiE;AACvE,EAAA,OAAO,CAAC,gBAAR,CAAyB,SAAzB,EAAoC,UAAA,KAAK,EAAI;AAC5C,QAAI,KAAK,CAAC,MAAN,CAAa,OAAb,CAAqB,QAArB,CAAJ,EAAoC;AACnC,MAAA,OAAO,CAAC,KAAD,CAAP;AACA;AACD,GAJD;AAKA;AAED;;;;;;;AAKO,SAAS,aAAT,CAAuB,OAAvB,EAAgC;AACtC,SAAO,OAAO,CAAC,UAAf,EAA2B;AAC1B,IAAA,OAAO,CAAC,WAAR,CAAoB,OAAO,CAAC,UAA5B;AACA;AACD;AAED;;;;;;;;AAMO,SAAS,WAAT,CAAqB,UAArB,EAAiC;AACvC,SAAO,IAAI,SAAJ,GAAgB,eAAhB,CAAgC,UAAhC,EAA4C,WAA5C,CAAP;AACA;AAED;;;;;;;;;;;;AAUO,SAAS,oBAAT,CAA8B,QAA9B,EAAwC;AAAA;;AAC9C,SAAO,IAAI,CAAC,KAAL,CAAW,0BAAA,QAAQ,CAAC,aAAT,CAAuB,QAAvB,2GAAkC,WAAlC,kFAA+C,IAA/C,OAAyD,IAApE,CAAP;AACA;AAED;;;;;;;;AAMO,SAAS,SAAT,CAAmB,kBAAnB,EAAuC;AAC7C,MAAI,kBAAkB,YAAY,QAAlC,EAA4C;AAC3C,QAAI,MAAM,GAAG,EAAb;AAD2C,QAEtC,WAFsC,GAEF,kBAFE,CAEtC,WAFsC;AAAA,QAEzB,OAFyB,GAEF,kBAFE,CAEzB,OAFyB;AAAA,QAEhB,UAFgB,GAEF,kBAFE,CAEhB,UAFgB;;AAG3C,QAAI,OAAJ,EAAa;AACZ,MAAA,MAAM,wBAAiB,OAAO,CAAC,IAAzB,MAAN;AACA,KAFD,MAGK,IAAI,UAAU,CAAC,OAAX,KAAuB,MAAvB,IAAiC,WAAW,KAAK,WAArD,EAAkE;AACtE,MAAA,MAAM,qBAAN;AACA;;AACD,IAAA,MAAM,IAAI,UAAU,CAAC,SAArB;AACA,WAAO,MAAP;AACA;;AACD,SAAO,kBAAkB,CAAC,SAA1B;AACA","file":"dom-utils.cjs.js","sourcesContent":["/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Add an event listener to `element` that only fires when the target of the\n * event matches `selector`.\n * \n * @param {Element} element\n * @param {String} eventName\n * @param {String} selector\n * @param {Function} handler\n */\nexport function addEventDelegate(element, eventName, selector, handler) {\n\telement.addEventListener(eventName, event => {\n\t\tif (event.target.matches(selector)) {\n\t\t\thandler(event);\n\t\t}\n\t});\n}\n\n/**\n * Removes all of an element's child nodes.\n * \n * @param {Element} element\n */\nexport function clearChildren(element) {\n\twhile (element.firstChild) {\n\t\telement.removeChild(element.firstChild);\n\t}\n}\n\n/**\n * Deserialize an HTML string into a document fragment.\n * \n * @param {String} htmlString\n * @return {Document}\n */\nexport function deserialize(htmlString) {\n\treturn new DOMParser().parseFromString(htmlString, 'text/html');\n}\n\n/**\n * Parse the text content of the element picked out by the given selector as\n * JSON data, returning it as an object.\n * \n * @param {String} selector\n * A CSS selector for picking out the HTML element that contains the JSON data\n * to load.\n * @return {Object}\n * The JSON data converted to an object, or `null` if no data could be read.\n */\nexport function parseJsonFromElement(selector) {\n\treturn JSON.parse(document.querySelector(selector)?.textContent?.trim() || null);\n}\n\n/**\n * Serialize a document fragment into an HTML string.\n * \n * @param {Document|DocumentFragment} documentOrFragment\n * @return {String}\n */\nexport function serialize(documentOrFragment) {\n\tif (documentOrFragment instanceof Document) {\n\t\tlet result = '';\n\t\tlet {contentType, docType, firstChild} = documentOrFragment;\n\t\tif (docType) {\n\t\t\tresult += `<!DOCTYPE ${docType.name}>`;\n\t\t}\n\t\telse if (firstChild.tagName === 'HTML' || contentType === 'text/html') {\n\t\t\tresult += `<!DOCTYPE html>`;\n\t\t}\n\t\tresult += firstChild.outerHTML;\n\t\treturn result;\n\t}\n\treturn documentOrFragment.outerHTML;\n}\n"]} | ||
| {"version":3,"sources":["dom-utils.js"],"names":[],"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;AASO,SAAS,gBAAT,CAA0B,OAA1B,EAAmC,SAAnC,EAA8C,QAA9C,EAAwD,OAAxD,EAAiE;AACvE,EAAA,OA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;AACA;;AACD,SAAO,kBAAkB,CAAC,SAA1B;AACA","file":"dom-utils.cjs.js","sourcesContent":["/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Add an event listener to `element` that only fires when the target of the\n * event matches `selector`.\n * \n * @param {Element} element\n * @param {String} eventName\n * @param {String} selector\n * @param {Function} handler\n */\nexport function addEventDelegate(element, eventName, selector, handler) {\n\telement.addEventListener(eventName, event => {\n\t\tif (event.target.matches(selector)) {\n\t\t\thandler(event);\n\t\t}\n\t});\n}\n\n/**\n * Removes all of an element's child nodes.\n * \n * @param {Element} element\n */\nexport function clearChildren(element) {\n\twhile (element.firstChild) {\n\t\telement.removeChild(element.firstChild);\n\t}\n}\n\n/**\n * Deserialize an HTML string into a document fragment.\n * \n * @param {String} htmlString\n * @return {Document}\n */\nexport function deserialize(htmlString) {\n\treturn new DOMParser().parseFromString(htmlString, 'text/html');\n}\n\n/**\n * Parse the text content of the element picked out by the given selector as\n * JSON data, returning it as an object.\n * \n * @param {String} selector\n * A CSS selector for picking out the HTML element that contains the JSON data\n * to load.\n * @return {Object}\n * The JSON data converted to an object, or `null` if no data could be read.\n */\nexport function parseJsonFromElement(selector) {\n\treturn JSON.parse(document.querySelector(selector)?.textContent?.trim() || null);\n}\n\n/**\n * Serialize a document or document fragment into an HTML string.\n * \n * @param {Document|DocumentFragment} documentOrFragment\n * @return {String}\n */\nexport function serialize(documentOrFragment) {\n\tif (documentOrFragment.nodeType === Node.DOCUMENT_NODE) {\n\t\tlet result = '';\n\t\tlet {contentType, docType, firstElementChild} = documentOrFragment;\n\t\tif (docType) {\n\t\t\tresult += `<!DOCTYPE ${docType.name}>`;\n\t\t}\n\t\telse if (firstElementChild.tagName === 'HTML' || contentType === 'text/html') {\n\t\t\tresult += `<!DOCTYPE html>`;\n\t\t}\n\t\tresult += firstElementChild.outerHTML;\n\t\treturn result;\n\t}\n\treturn documentOrFragment.outerHTML;\n}\n"]} |
+5
-5
@@ -71,3 +71,3 @@ /* | ||
| /** | ||
| * Serialize a document fragment into an HTML string. | ||
| * Serialize a document or document fragment into an HTML string. | ||
| * | ||
@@ -79,15 +79,15 @@ * @param {Document|DocumentFragment} documentOrFragment | ||
| export function serialize(documentOrFragment) { | ||
| if (documentOrFragment instanceof Document) { | ||
| if (documentOrFragment.nodeType === Node.DOCUMENT_NODE) { | ||
| var result = ''; | ||
| var contentType = documentOrFragment.contentType, | ||
| docType = documentOrFragment.docType, | ||
| firstChild = documentOrFragment.firstChild; | ||
| firstElementChild = documentOrFragment.firstElementChild; | ||
| if (docType) { | ||
| result += "<!DOCTYPE ".concat(docType.name, ">"); | ||
| } else if (firstChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| } else if (firstElementChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| result += "<!DOCTYPE html>"; | ||
| } | ||
| result += firstChild.outerHTML; | ||
| result += firstElementChild.outerHTML; | ||
| return result; | ||
@@ -94,0 +94,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["dom-utils.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;AASA,OAAO,SAAS,gBAAT,CAA0B,OAA1B,EAAmC,SAAnC,EAA8C,QAA9C,EAAwD,OAAxD,EAAiE;AACvE,EAAA,OAAO,CAAC,gBAAR,CAAyB,SAAzB,EAAoC,UAAA,KAAK,EAAI;AAC5C,QAAI,KAAK,CAAC,MAAN,CAAa,OAAb,CAAqB,QAArB,CAAJ,EAAoC;AACnC,MAAA,OAAO,CAAC,KAAD,CAAP;AACA;AACD,GAJD;AAKA;AAED;;;;;;AAKA,OAAO,SAAS,aAAT,CAAuB,OAAvB,EAAgC;AACtC,SAAO,OAAO,CAAC,UAAf,EAA2B;AAC1B,IAAA,OAAO,CAAC,WAAR,CAAoB,OAAO,CAAC,UAA5B;AACA;AACD;AAED;;;;;;;AAMA,OAAO,SAAS,WAAT,CAAqB,UAArB,EAAiC;AACvC,SAAO,IAAI,SAAJ,GAAgB,eAAhB,CAAgC,UAAhC,EAA4C,WAA5C,CAAP;AACA;AAED;;;;;;;;;;;AAUA,OAAO,SAAS,oBAAT,CAA8B,QAA9B,EAAwC;AAAA;;AAC9C,SAAO,IAAI,CAAC,KAAL,CAAW,0BAAA,QAAQ,CAAC,aAAT,CAAuB,QAAvB,2GAAkC,WAAlC,kFAA+C,IAA/C,OAAyD,IAApE,CAAP;AACA;AAED;;;;;;;AAMA,OAAO,SAAS,SAAT,CAAmB,kBAAnB,EAAuC;AAC7C,MAAI,kBAAkB,YAAY,QAAlC,EAA4C;AAC3C,QAAI,MAAM,GAAG,EAAb;AAD2C,QAEtC,WAFsC,GAEF,kBAFE,CAEtC,WAFsC;AAAA,QAEzB,OAFyB,GAEF,kBAFE,CAEzB,OAFyB;AAAA,QAEhB,UAFgB,GAEF,kBAFE,CAEhB,UAFgB;;AAG3C,QAAI,OAAJ,EAAa;AACZ,MAAA,MAAM,wBAAiB,OAAO,CAAC,IAAzB,MAAN;AACA,KAFD,MAGK,IAAI,UAAU,CAAC,OAAX,KAAuB,MAAvB,IAAiC,WAAW,KAAK,WAArD,EAAkE;AACtE,MAAA,MAAM,qBAAN;AACA;;AACD,IAAA,MAAM,IAAI,UAAU,CAAC,SAArB;AACA,WAAO,MAAP;AACA;;AACD,SAAO,kBAAkB,CAAC,SAA1B;AACA","file":"dom-utils.es.js","sourcesContent":["/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Add an event listener to `element` that only fires when the target of the\n * event matches `selector`.\n * \n * @param {Element} element\n * @param {String} eventName\n * @param {String} selector\n * @param {Function} handler\n */\nexport function addEventDelegate(element, eventName, selector, handler) {\n\telement.addEventListener(eventName, event => {\n\t\tif (event.target.matches(selector)) {\n\t\t\thandler(event);\n\t\t}\n\t});\n}\n\n/**\n * Removes all of an element's child nodes.\n * \n * @param {Element} element\n */\nexport function clearChildren(element) {\n\twhile (element.firstChild) {\n\t\telement.removeChild(element.firstChild);\n\t}\n}\n\n/**\n * Deserialize an HTML string into a document fragment.\n * \n * @param {String} htmlString\n * @return {Document}\n */\nexport function deserialize(htmlString) {\n\treturn new DOMParser().parseFromString(htmlString, 'text/html');\n}\n\n/**\n * Parse the text content of the element picked out by the given selector as\n * JSON data, returning it as an object.\n * \n * @param {String} selector\n * A CSS selector for picking out the HTML element that contains the JSON data\n * to load.\n * @return {Object}\n * The JSON data converted to an object, or `null` if no data could be read.\n */\nexport function parseJsonFromElement(selector) {\n\treturn JSON.parse(document.querySelector(selector)?.textContent?.trim() || null);\n}\n\n/**\n * Serialize a document fragment into an HTML string.\n * \n * @param {Document|DocumentFragment} documentOrFragment\n * @return {String}\n */\nexport function serialize(documentOrFragment) {\n\tif (documentOrFragment instanceof Document) {\n\t\tlet result = '';\n\t\tlet {contentType, docType, firstChild} = documentOrFragment;\n\t\tif (docType) {\n\t\t\tresult += `<!DOCTYPE ${docType.name}>`;\n\t\t}\n\t\telse if (firstChild.tagName === 'HTML' || contentType === 'text/html') {\n\t\t\tresult += `<!DOCTYPE html>`;\n\t\t}\n\t\tresult += firstChild.outerHTML;\n\t\treturn result;\n\t}\n\tretur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Z,MAAA,MAAM,wBAAiB,OAAO,CAAC,IAAzB,MAAN;AACA,KAFD,MAGK,IAAI,iBAAiB,CAAC,OAAlB,KAA8B,MAA9B,IAAwC,WAAW,KAAK,WAA5D,EAAyE;AAC7E,MAAA,MAAM,qBAAN;AACA;;AACD,IAAA,MAAM,IAAI,iBAAiB,CAAC,SAA5B;AACA,WAAO,MAAP;AACA;;AACD,SAAO,kBAAkB,CAAC,SAA1B;AACA","file":"dom-utils.es.js","sourcesContent":["/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Add an event listener to `element` that only fires when the target of the\n * event matches `selector`.\n * \n * @param {Element} element\n * @param {String} eventName\n * @param {String} selector\n * @param {Function} handler\n */\nexport function addEventDelegate(element, eventName, selector, handler) {\n\telement.addEventListener(eventName, event => {\n\t\tif (event.target.matches(selector)) {\n\t\t\thandler(event);\n\t\t}\n\t});\n}\n\n/**\n * Removes all of an element's child nodes.\n * \n * @param {Element} element\n */\nexport function clearChildren(element) {\n\twhile (element.firstChild) {\n\t\telement.removeChild(element.firstChild);\n\t}\n}\n\n/**\n * Deserialize an HTML string into a document fragment.\n * \n * @param {String} htmlString\n * @return {Document}\n */\nexport function deserialize(htmlString) {\n\treturn new DOMParser().parseFromString(htmlString, 'text/html');\n}\n\n/**\n * Parse the text content of the element picked out by the given selector as\n * JSON data, returning it as an object.\n * \n * @param {String} selector\n * A CSS selector for picking out the HTML element that contains the JSON data\n * to load.\n * @return {Object}\n * The JSON data converted to an object, or `null` if no data could be read.\n */\nexport function parseJsonFromElement(selector) {\n\treturn JSON.parse(document.querySelector(selector)?.textContent?.trim() || null);\n}\n\n/**\n * Serialize a document or document fragment into an HTML string.\n * \n * @param {Document|DocumentFragment} documentOrFragment\n * @return {String}\n */\nexport function serialize(documentOrFragment) {\n\tif (documentOrFragment.nodeType === Node.DOCUMENT_NODE) {\n\t\tlet result = '';\n\t\tlet {contentType, docType, firstElementChild} = documentOrFragment;\n\t\tif (docType) {\n\t\t\tresult += `<!DOCTYPE ${docType.name}>`;\n\t\t}\n\t\telse if (firstElementChild.tagName === 'HTML' || contentType === 'text/html') {\n\t\t\tresult += `<!DOCTYPE html>`;\n\t\t}\n\t\tresult += firstElementChild.outerHTML;\n\t\treturn result;\n\t}\n\treturn documentOrFragment.outerHTML;\n}\n"]} |
+5
-5
@@ -70,3 +70,3 @@ /* | ||
| /** | ||
| * Serialize a document fragment into an HTML string. | ||
| * Serialize a document or document fragment into an HTML string. | ||
| * | ||
@@ -77,12 +77,12 @@ * @param {Document|DocumentFragment} documentOrFragment | ||
| export function serialize(documentOrFragment) { | ||
| if (documentOrFragment instanceof Document) { | ||
| if (documentOrFragment.nodeType === Node.DOCUMENT_NODE) { | ||
| let result = ''; | ||
| let {contentType, docType, firstChild} = documentOrFragment; | ||
| let {contentType, docType, firstElementChild} = documentOrFragment; | ||
| if (docType) { | ||
| result += `<!DOCTYPE ${docType.name}>`; | ||
| } | ||
| else if (firstChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| else if (firstElementChild.tagName === 'HTML' || contentType === 'text/html') { | ||
| result += `<!DOCTYPE html>`; | ||
| } | ||
| result += firstChild.outerHTML; | ||
| result += firstElementChild.outerHTML; | ||
| return result; | ||
@@ -89,0 +89,0 @@ } |
+27
-3
@@ -18,6 +18,11 @@ /* | ||
| /* eslint-env jest */ | ||
| import {parseJsonFromElement} from './dom-utils.js'; | ||
| import { | ||
| parseJsonFromElement, | ||
| serialize | ||
| } from './dom-utils.js'; | ||
| import h from 'hyperscript'; | ||
| import hh from 'hyperscript-helpers'; | ||
| import {$} from 'dumb-query-selector'; | ||
| import h from 'hyperscript'; | ||
| import hh from 'hyperscript-helpers'; | ||
| import {JSDOM} from 'jsdom'; | ||
@@ -31,2 +36,3 @@ const {div} = hh(h); | ||
| // TODO: Be able to specify the document so we can test separate DOMs | ||
| describe('#parseJsonFromElement', function() { | ||
@@ -64,2 +70,20 @@ | ||
| }); | ||
| describe('#serialize', function() { | ||
| const htmlString = '<!DOCTYPE html><html><head></head><body><p>Hi!</p></body></html>'; | ||
| test('Full document', function() { | ||
| let doc = new JSDOM(htmlString).window.document; | ||
| let result = serialize(doc); | ||
| expect(result).toBe(htmlString); | ||
| }); | ||
| test('Partial document', function() { | ||
| let doc = new JSDOM(htmlString).window.document; | ||
| let fragment = $('p', doc); | ||
| let result = serialize(fragment); | ||
| expect(result).toBe('<p>Hi!</p>'); | ||
| }); | ||
| }); | ||
| }); |
+4
-2
| { | ||
| "name": "@ultraq/dom-utils", | ||
| "version": "0.3.1", | ||
| "version": "0.3.2", | ||
| "description": "A collection of utilities for working with the DOM", | ||
@@ -35,2 +35,3 @@ "author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)", | ||
| "coveralls": "^3.1.0", | ||
| "dumb-query-selector": "^3.3.0", | ||
| "eslint": "^6.8.0", | ||
@@ -40,3 +41,4 @@ "eslint-config-ultraq": "^2.3.0", | ||
| "hyperscript-helpers": "^3.0.3", | ||
| "jest": "^26.0.1" | ||
| "jest": "^26.0.1", | ||
| "jsdom": "^16.2.2" | ||
| }, | ||
@@ -43,0 +45,0 @@ "engines": { |
+3
-3
@@ -53,6 +53,6 @@ | ||
| ### serialize(documentFragment) | ||
| ### serialize(documentOrFragment) | ||
| Serialize a document fragment into an HTML string. | ||
| Serialize a document or document fragment into an HTML string. | ||
| - **documentFragment** | ||
| - **documentOrFragment** |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
33747
3.18%349
6.08%13
18.18%2
Infinity%