@lrnwebcomponents/utils
Advanced tools
Comparing version 3.0.0 to 4.0.0
{ | ||
"name": "@lrnwebcomponents/utils", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Helper functions to clean up web component data handling.", | ||
@@ -20,3 +20,3 @@ "repository": { | ||
}, | ||
"gitHead": "8fcf8efcf719302c9f59c6cf952840b39bfde0b5" | ||
"gitHead": "e877f54ece91fb0b2f7aba62f9f9fd15060600f2" | ||
} |
171
utils.js
@@ -5,2 +5,85 @@ /** | ||
/** | ||
* Mix of solutions from https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data | ||
*/ | ||
export function CSVtoArray(text) { | ||
let p = "", | ||
row = [""], | ||
ret = [row], | ||
i = 0, | ||
r = 0, | ||
s = !0, | ||
l; | ||
for (l in text) { | ||
l = text[l]; | ||
if ('"' === l) { | ||
if (s && l === p) row[i] += l; | ||
s = !s; | ||
} else if ("," === l && s) l = row[++i] = ""; | ||
else if ("\n" === l && s) { | ||
if ("\r" === p) row[i] = row[i].slice(0, -1); | ||
row = ret[++r] = [(l = "")]; | ||
i = 0; | ||
} else row[i] += l; | ||
p = l; | ||
} | ||
return ret; | ||
} | ||
/** | ||
* Check source of the video, potentially correcting bad links. | ||
*/ | ||
export function cleanVideoSource(input) { | ||
// strip off the ? modifier for youtube/vimeo so we can build ourselves | ||
var tmp = input.split("?"); | ||
var v = ""; | ||
input = tmp[0]; | ||
if (tmp.length == 2) { | ||
let tmp2 = tmp[1].split("&"), | ||
args = tmp2[0].split("="), | ||
qry = Array.isArray(tmp2.shift()) ? tmp2.shift().join("") : tmp2.shift(); | ||
if (args[0] == "v") { | ||
let q = qry !== undefined && qry !== "" ? "?" + qry : ""; | ||
v = args[1] + q; | ||
} | ||
} | ||
// link to the vimeo video instead of the embed player address | ||
if ( | ||
input.indexOf("player.vimeo.com") == -1 && | ||
input.indexOf("vimeo.com") != -1 | ||
) { | ||
// normalize what the API will return since it is API based | ||
// and needs cleaned up for front-end | ||
if (input.indexOf("/videos/") != -1) { | ||
input = input.replace("/videos/", "/"); | ||
} | ||
return input.replace("vimeo.com/", "player.vimeo.com/video/"); | ||
} | ||
// copy and paste from the URL | ||
else if (input.indexOf("youtube.com/watch") != -1) { | ||
return input.replace("youtube.com/watch", "youtube.com/embed/") + v; | ||
} | ||
// copy and paste from the URL | ||
else if (input.indexOf("youtube-no-cookie.com/") != -1) { | ||
return input.replace("youtube-no-cookie.com/", "youtube.com/") + v; | ||
} | ||
// weird share-ly style version | ||
else if (input.indexOf("youtu.be") != -1) { | ||
return input.replace("youtu.be/", "www.youtube.com/embed/") + v; | ||
} | ||
// copy and paste from the URL for sketchfab | ||
else if ( | ||
input.indexOf("sketchfab.com") != -1 && | ||
input.indexOf("/embed") == -1 | ||
) { | ||
return input + "/embed"; | ||
} | ||
// copy and paste from the URL for sketchfab | ||
else if ( | ||
input.indexOf("dailymotion.com") != -1 && | ||
input.indexOf("/embed") == -1 | ||
) { | ||
return input.replace("/video/", "/embed/video/"); | ||
} | ||
return input; | ||
} | ||
// wrap an element with another; super basic but makes it consistent across our apps | ||
@@ -14,2 +97,14 @@ function wrap(el, wrapper) { | ||
/** | ||
* Wrap an array of items all at once | ||
*/ | ||
function wrapAll(ary, wrapper) { | ||
if (ary && ary.length) { | ||
ary[0].parentNode.insertBefore(wrapper, ary[0]); | ||
for (var i in ary) { | ||
wrapper.appendChild(ary[i]); | ||
} | ||
} | ||
} | ||
// unwrap away from an element; super basic but makes it consistent across our apps | ||
@@ -222,4 +317,4 @@ function unwrap(el) { | ||
// ensure that HAX tags aren't leaking in here | ||
html = html.replace(/<hax[\s\S]*?>/gi, ""); | ||
html = html.replace(/<\/hax[\s\S]*?>/gi, ""); | ||
html = html.replace(/<hax-(body|tray|store)[\s\S]*?>/gi, ""); | ||
html = html.replace(/<\/hax-(body|tray|store)[\s\S]*?>/gi, ""); | ||
html = html.replace(/<h-a-x[\s\S]*?>/gi, ""); | ||
@@ -302,2 +397,16 @@ html = html.replace(/<\/h-a-x*?>/gi, ""); | ||
} | ||
//converts unicode to HTML entity | ||
export function utf2Html(str) { | ||
return [...str] | ||
.map((char) => | ||
char.codePointAt() > 127 ? `&#${char.codePointAt()};` : char | ||
) | ||
.join(""); | ||
} | ||
export function htmlEntities(s) { | ||
return s.replace(/[\u00A0-\u9999<>\&]/gim, function (i) { | ||
return "&#" + i.charCodeAt(0) + ";"; | ||
}); | ||
} | ||
/** | ||
@@ -328,3 +437,3 @@ * Strip word BS as well as GDocs, box notes, Medium and some others as best we can | ||
output = output.replace( | ||
/<(\/)*(meta|link|html|head|body|span|font|br|\\\\?xml:|xml|st1:|o:|w:|m:|v:)(\s|.)*?>/gim, | ||
/<(\/)*(meta|link|title|html|head|body|span|font|br|\\\\?xml:|xml|st1:|o:|w:|m:|v:)(\s|.)*?>/gim, | ||
"" | ||
@@ -346,2 +455,14 @@ ); | ||
output = output.replace(/ start='.*?'/g, ""); | ||
// remove line-height; commonly set via html copy and paste in google docs | ||
output = output.replace(/line-height:.*?\"/g, '"'); | ||
output = output.replace(/line-height:.*?;/g, ""); | ||
// normal font cause... obviously | ||
output = output.replace(/font-weight:normal;/g, ""); | ||
// text decoration in a link... | ||
output = output.replace(/text-decoration:none;/g, ""); | ||
// margin clean up that is in point values; only machines make these | ||
output = output.replace(/margin-.*?:.*?\"/g, '"'); | ||
output = output.replace(/margin-.*?:.*?;/g, ""); | ||
// empty style tags | ||
output = output.replace(/ style=""/g, ""); | ||
// ID's wont apply meaningfully on a paste | ||
@@ -358,2 +479,5 @@ output = output.replace(/ id="(\s|.)*?"/gim, ""); | ||
output = output.replace(/ class="(\s|.)*?"/gim, ""); | ||
output = output.replace(/<pstyle/gm, "<p style"); | ||
// HIGHLY specific to certain platforms, empty link tag | ||
output = output.replace(/<a name=\"_GoBack\"><\/a>/gm, ""); | ||
// 7. clean out empty paragraphs and endlines that cause weird spacing | ||
@@ -373,5 +497,16 @@ output = output.replace(/ /gm, " "); | ||
output = output.replace(/<\/p><br \/><\/b>/gm, "</p></b>"); | ||
// some other things we know not to allow to wrap | ||
// some other things we know not to allow to wrap and | ||
// some things bold stuff like crazy for some odd reason | ||
output = output.replace(/<b><p>/gm, "<p>"); | ||
output = output.replace(/<\/p><\/b>/gm, "</p>"); | ||
output = output.replace(/<b>/gm, "<strong>"); | ||
output = output.replace(/<\/b>/gm, "</strong>"); | ||
// clean up in lists because they get messy for no real reason...ever. | ||
// tables as well | ||
output = output.replace(/<p style=\".*?\">/gm, "<p>"); | ||
output = output.replace(/<ul style=\".*?\">/gm, "<ul>"); | ||
output = output.replace(/<ol style=\".*?\">/gm, "<ol>"); | ||
output = output.replace(/<li style=\".*?\">/gm, "<li>"); | ||
output = output.replace(/<td style=\".*?\">/gm, "<td>"); | ||
output = output.replace(/<tr style=\".*?\">/gm, "<tr>"); | ||
// drop list wrappers | ||
@@ -454,3 +589,3 @@ output = output.replace(/<li><p>/gm, "<li>"); | ||
*/ | ||
function nodeToHaxElement(node, eventName = "insert-element") { | ||
async function nodeToHaxElement(node, eventName = "insert-element") { | ||
if (!node) { | ||
@@ -552,3 +687,3 @@ return null; | ||
} | ||
let slotContent = window.HaxStore.instance.getHAXSlot(node); | ||
let slotContent = await window.HaxStore.instance.getHAXSlot(node); | ||
// support fallback on inner text if there were no nodes | ||
@@ -620,2 +755,3 @@ if (slotContent == "") { | ||
wrap, | ||
wrapAll, | ||
unwrap, | ||
@@ -741,2 +877,25 @@ formatHTML, | ||
/** | ||
* detect if an element is currently in the viewport / visible | ||
* @param {Node} el | ||
* @returns Boolean | ||
*/ | ||
export function isElementInViewport( | ||
el, | ||
bounds = { | ||
top: 0, | ||
right: window.innerWidth, | ||
bottom: window.innerHeight, | ||
left: 0, | ||
} | ||
) { | ||
var rect = el.getBoundingClientRect(); | ||
return ( | ||
rect.top >= bounds.top && | ||
rect.left >= bounds.left && | ||
rect.bottom <= bounds.bottom && | ||
rect.right <= bounds.right | ||
); | ||
} | ||
/** | ||
* @param {!Selection} s the window selection to use | ||
@@ -743,0 +902,0 @@ * @param {!Node} node the node to walk from |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107361
9
2869
2