Repository URL to install this package:
|
Version:
1.2.9 ▾
|
"use strict";
// @flow
/* These are helpers that deal with the insertRule (aka speedy) API
* They are used in the StyleTags and specifically the speedy tag
*/
Object.defineProperty(exports, "__esModule", { value: true });
/* retrieve a sheet for a given style tag */
exports.sheetForTag = (tag) => {
// $FlowFixMe
if (tag.sheet)
return tag.sheet;
/* Firefox quirk requires us to step through all stylesheets to find one owned by the given tag */
const size = document.styleSheets.length;
for (let i = 0; i < size; i += 1) {
const sheet = document.styleSheets[i];
// $FlowFixMe
if (sheet.ownerNode === tag)
return sheet;
}
/* we should always be able to find a tag */
throw new Error();
};
/* insert a rule safely and return whether it was actually injected */
exports.safeInsertRule = (sheet, cssRule, index) => {
/* abort early if cssRule string is falsy */
if (!cssRule)
return false;
const maxIndex = sheet.cssRules.length;
try {
/* use insertRule and cap passed index with maxIndex (no of cssRules) */
sheet.insertRule(cssRule, index <= maxIndex ? index : maxIndex);
}
catch (err) {
/* any error indicates an invalid rule */
return false;
}
return true;
};
/* deletes `size` rules starting from `removalIndex` */
exports.deleteRules = (sheet, removalIndex, size) => {
const lowerBound = removalIndex - size;
for (let i = removalIndex; i > lowerBound; i -= 1) {
sheet.deleteRule(i);
}
};
//# sourceMappingURL=insertRuleHelpers.js.map