1import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
2
3// for performance considerations, it only uses performant syntax
4function attachDirAuto(el) {
5 if (el.type !== 'hidden' &&
6 el.type !== 'checkbox' &&
7 el.type !== 'radio' &&
8 el.type !== 'range' &&
9 el.type !== 'color') {
10 el.dir = 'auto';
11 }
12}
13
14export function initDirAuto() {
15 const observer = new MutationObserver((mutationList) => {
16 const len = mutationList.length;
17 for (let i = 0; i < len; i++) {
18 const mutation = mutationList[i];
19 const len = mutation.addedNodes.length;
20 for (let i = 0; i < len; i++) {
21 const addedNode = mutation.addedNodes[i];
22 if (!isDocumentFragmentOrElementNode(addedNode)) continue;
23 if (addedNode.nodeName === 'INPUT' || addedNode.nodeName === 'TEXTAREA') attachDirAuto(addedNode);
24 const children = addedNode.querySelectorAll('input, textarea');
25 const len = children.length;
26 for (let childIdx = 0; childIdx < len; childIdx++) {
27 attachDirAuto(children[childIdx]);
28 }
29 }
30 }
31 });
32
33 const docNodes = document.querySelectorAll('input, textarea');
34 const len = docNodes.length;
35 for (let i = 0; i < len; i++) {
36 attachDirAuto(docNodes[i]);
37 }
38
39 observer.observe(document, {subtree: true, childList: true});
40}