UNPKG

@lynx-js/web-core

Version:

This is an internal experimental package, do not use

75 lines 2.64 kB
// Copyright 2023 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import { ErrorCode, IdentifierType } from '../../../constants.js'; import { __QuerySelector } from '../elementAPIs/pureElementPAPIs.js'; export function queryNodes(lynxViewInstance, type, identifier, component_id, first_only, root_unique_id, callback, error) { let queryRoot = lynxViewInstance.rootDom; if (root_unique_id) { const root = lynxViewInstance.mtsWasmBinding.getElementByUniqueId(root_unique_id); if (root) { queryRoot = root; } else { console.error(`[lynx-web] cannot find dom for root_unique_id: ${root_unique_id}`); error?.(ErrorCode.NODE_NOT_FOUND); return; } } else if (component_id) { const root = lynxViewInstance.mtsWasmBinding.getElementByComponentId(component_id); if (root) { queryRoot = root; } else { console.error(`[lynx-web] cannot find dom for component_id: ${component_id}`); error?.(ErrorCode.NODE_NOT_FOUND); return; } } let selector; if (type === IdentifierType.ID_SELECTOR) { selector = identifier; } else if (type === IdentifierType.UNIQUE_ID) { const element = lynxViewInstance.mtsWasmBinding.getElementByUniqueId(Number(identifier)); if (element) { callback(element); return; } else { console.error(`[lynx-web] cannot find dom for unique_id: ${identifier}`); error?.(ErrorCode.NODE_NOT_FOUND); return; } } else { console.error(`[lynx-web] NYI: setnativeprops type ${type}`); error?.(ErrorCode.UNKNOWN); return; } if (first_only) { let targetElement = null; try { targetElement = __QuerySelector(queryRoot, selector); } catch (e) { console.error(`[lynx-web] cannot use selector: ${selector}`); error?.(ErrorCode.SELECTOR_NOT_SUPPORTED); return; } if (targetElement) { callback(targetElement); } else { console.error(`[lynx-web] cannot find from for selector ${identifier} under`, queryRoot); error?.(ErrorCode.NODE_NOT_FOUND); } } else { queryRoot.querySelectorAll(selector).forEach((element) => { callback(element); }); } } //# sourceMappingURL=queryNodes.js.map