Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

insert-css

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

insert-css - npm Package Compare versions

Comparing version 0.2.0 to 1.0.0

example.js

52

index.js

@@ -1,22 +0,46 @@

var inserted = {};
var containers = []; // will store container HTMLElement references
var styleElements = []; // will store {prepend: HTMLElement, append: HTMLElement}
module.exports = function (css, options) {
if (inserted[css]) return;
inserted[css] = true;
var elem = document.createElement('style');
elem.setAttribute('type', 'text/css');
options = options || {};
if ('textContent' in elem) {
elem.textContent = css;
var position = options.prepend === true ? 'prepend' : 'append';
var container = options.container !== undefined ? options.container : document.querySelector('head');
var containerId = containers.indexOf(container);
// first time we see this container, create the necessary entries
if (containerId === -1) {
containerId = containers.push(container) - 1;
styleElements[containerId] = {};
}
// try to get the correponding container + position styleElement, create it otherwise
var styleElement;
if (styleElements[containerId] !== undefined && styleElements[containerId][position] !== undefined) {
styleElement = styleElements[containerId][position];
} else {
elem.styleSheet.cssText = css;
styleElement = styleElements[containerId][position] = createStyleElement();
if (position === 'prepend') {
container.insertBefore(styleElement, container.childNodes[0]);
} else {
container.appendChild(styleElement);
}
}
var head = document.getElementsByTagName('head')[0];
if (options && options.prepend) {
head.insertBefore(elem, head.childNodes[0]);
// actually add the stylesheet
if (styleElement.styleSheet) {
styleElement.styleSheet.cssText += css
} else {
head.appendChild(elem);
styleElement.textContent += css;
}
return styleElement;
};
function createStyleElement() {
var styleElement = document.createElement('style');
styleElement.setAttribute('type', 'text/css');
return styleElement;
}
{
"name": "insert-css",
"version": "0.2.0",
"version": "1.0.0",
"description": "insert a string of css into the <head>",
"main": "index.js",
"dependencies": {},
"scripts": {
"test": "browserify test.js | tape-run",
"example": "budo example.js"
},
"devDependencies": {
"tape": "^2.13.3",
"computed-style": "~0.1.3"
"browserify": "^13.0.1",
"budo": "^8.3.0",
"computed-style": "^0.3.0",
"dom-event": "^1.0.0",
"tape": "^4.6.0",
"tape-run": "^2.1.4"
},

@@ -16,3 +24,3 @@ "repository": {

"testling" : {
"files" : "test/*.js",
"files" : "test.js",
"browsers" : [

@@ -33,3 +41,10 @@ "ie/6..latest",

"browser",
"browserify"
"browserify",
"inject",
"styles",
"stylesheet",
"style",
"html",
"head",
"link"
],

@@ -36,0 +51,0 @@ "author": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc