@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
80 lines (67 loc) • 2 kB
JavaScript
;
const {
detectPagerNumbersSelector,
transformPagerNumbersSelector,
} = require('../_shared.cjs');
function transformScriptContent(content, j) {
let root;
try {
root = j(content);
} catch {
return content;
}
let changed = false;
root.find(j.StringLiteral).forEach(path => {
const oldValue = path.value.value;
if (detectPagerNumbersSelector(oldValue)) {
path.value.value = transformPagerNumbersSelector(oldValue);
if (path.value.extra) {
path.value.extra.rawValue = path.value.value;
}
changed = true;
}
});
root.find(j.Literal, n => typeof n.value === 'string').forEach(path => {
const oldValue = path.node.value;
if (detectPagerNumbersSelector(oldValue)) {
path.node.value = transformPagerNumbersSelector(oldValue);
if (path.node.extra) {
path.node.extra.rawValue = path.node.value;
}
changed = true;
}
});
root.find(j.TemplateLiteral).forEach(path => {
path.value.quasis.forEach(quasi => {
const oldValue = quasi.value.cooked;
if (detectPagerNumbersSelector(oldValue)) {
const newValue = transformPagerNumbersSelector(oldValue);
quasi.value.cooked = newValue;
quasi.value.raw = newValue;
changed = true;
}
});
});
if (!changed) {
return content;
}
return root.toSource({ quote: 'single' });
}
const SCRIPT_RE = /(<script(?![^>]*\bsrc\b)[^>]*>)([\s\S]*?)(<\/script>)/gi;
module.exports = function transformer(fileInfo, api) {
if (!/\.html?$/i.test(fileInfo.path)) {
return undefined;
}
const j = api.jscodeshift;
let source = fileInfo.source;
let changed = false;
source = source.replace(SCRIPT_RE, (match, open, content, close) => {
const transformed = transformScriptContent(content, j);
if (transformed !== content) {
changed = true;
return open + transformed + close;
}
return match;
});
return changed ? source : undefined;
};