cypress-controls-ext
Version:
Extension to embed controls to controls panel in cypress app
37 lines (36 loc) • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setStyle = void 0;
const common_1 = require("./common");
const setStyle = (wrapperId, styleDef) => {
var _a;
const startLine = `/* cypress controls extension ${wrapperId} */`;
const endLine = `/* cypress controls extension end ${wrapperId} */`;
const styleTagLocator = `html head style[data-id="${wrapperId}"]`;
// when no style tag add
if ((0, common_1.cypressAppSelect)(styleTagLocator).length === 0) {
(0, common_1.cypressAppSelect)('html head').append(`<style type="text/css" data-id="${wrapperId}"></style>`);
}
// should be last as css is cascading tables
const style = (0, common_1.cypressAppSelect)(styleTagLocator).eq((0, common_1.cypressAppSelect)(styleTagLocator).length - 1);
const html = style === null || style === void 0 ? void 0 : style.html();
const commonStyleControlWrapper = ``;
const addStyleCss = `\n${startLine}
${commonStyleControlWrapper}
${styleDef}
${endLine}`;
// add style first time for session
if ((html === null || html === void 0 ? void 0 : html.indexOf(startLine)) === -1) {
style.append(addStyleCss);
}
else {
// change style
const start = html.indexOf(startLine);
const end = html.indexOf(endLine + endLine.length);
const toReplace = html.slice(start, end);
if ((_a = style.get()) === null || _a === void 0 ? void 0 : _a[0]) {
style.get()[0].innerHTML = html.replace('\n' + toReplace, addStyleCss);
}
}
};
exports.setStyle = setStyle;