@kit-data-manager/pid-component
Version:
The PID-Component is a web component that can be used to evaluate and display FAIR Digital Objects, PIDs, ORCiDs, and possibly other identifiers in a user-friendly way. It is easily extensible to support other identifier types.
106 lines (105 loc) • 3.46 kB
JavaScript
/*!
*
* Copyright 2024-2026 Karlsruhe Institute of Technology.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const SKIP_ELEMENTS = new Set([
'SCRIPT',
'STYLE',
'TEXTAREA',
'INPUT',
'SELECT',
'NOSCRIPT',
'PID-COMPONENT',
'CODE',
'PRE',
'SVG',
]);
const PROCESSED_ATTR = 'data-pid-auto-detected';
function shouldSkipElement(element, excludeSelector) {
if (SKIP_ELEMENTS.has(element.tagName)) {
return true;
}
if (element.hasAttribute('contenteditable')) {
return true;
}
if (element.hasAttribute(PROCESSED_ATTR)) {
return true;
}
if (element.closest('.pid-auto-detect-wrapper')) {
return true;
}
if (excludeSelector) {
try {
if (element.matches(excludeSelector)) {
return true;
}
}
catch (_a) {
}
}
return false;
}
export function scanDom(root, excludeSelector, batchSize = 50) {
return new Promise((resolve) => {
const results = [];
let nextId = 0;
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, {
acceptNode(node) {
var _a;
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node;
if (shouldSkipElement(element, excludeSelector)) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_SKIP;
}
const text = (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.trim();
if (!text || text.length < 2) {
return NodeFilter.FILTER_SKIP;
}
return NodeFilter.FILTER_ACCEPT;
},
});
const requestIdle = typeof requestIdleCallback !== 'undefined'
? requestIdleCallback
: (cb) => setTimeout(() => cb({ timeRemaining: () => 16 }), 0);
function processNextBatch(deadline) {
let count = 0;
while (count < batchSize && deadline.timeRemaining() > 0) {
const node = walker.nextNode();
if (node === null) {
resolve(results);
return;
}
if (node.nodeType === Node.TEXT_NODE) {
const textNode = node;
const text = textNode.textContent || '';
if (text.trim().length >= 2) {
results.push({
id: nextId++,
textNode,
text,
});
}
count++;
}
}
requestIdle(processNextBatch);
}
requestIdle(processNextBatch);
});
}
//# sourceMappingURL=DomScanner.js.map