<commit_before>/**
 * Copyright 2020 Google Inc. All rights reserved.
 *
 * 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
 *
 *     http://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.
 */

import DOMNode from './DOMNode';

export default class DOMModel {
  public document: DOMNode | null;
  private _nodesById: Map<number, DOMNode>;

  constructor() {
    this._nodesById = new Map();
    this.document = null;
  }

  load(document) {
    this.loadNode(document.root);
  }

  loadNode = (root) => {
    const stack = [root];
    while (stack.length) {
      const data = stack.shift();

      const node = DOMNode.create(data);
      this._nodesById.set(data.nodeId, node);
      if (!this.document) {
        this.document = node;
      }

      if (data.parentId) {
        const parent = this._nodesById.get(data.parentId);
        parent.appendChild(node);
      }

      stack.push(...(data.children || []));
      stack.push(...(data.shadowRoots || []));
      stack.push(...(data.pseudoElements || []));
    }
  };

  setChildNodes({parentId, nodes}) {
    const parent = this._nodesById.get(parentId);
    if (!parent) return;

    for (const node of nodes) {
      this.loadNode(node);
    }<commit_msg>Avoid pushing 0 elements<commit_after>/**
 * Copyright 2020 Google Inc. All rights reserved.
 *
 * 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
 *
 *     http://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.
 */

import DOMNode from './DOMNode';

export default class DOMModel {
  public document: DOMNode | null;
  private _nodesById: Map<number, DOMNode>;

  constructor() {
    this._nodesById = new Map();
    this.document = null;
  }

  load(document) {
    this.loadNode(document.root);
  }

  loadNode = (root) => {
    const stack = [root];
    while (stack.length) {
      const data = stack.shift();

      const node = DOMNode.create(data);
      this._nodesById.set(data.nodeId, node);
      if (!this.document) {
        this.document = node;
      }

      if (data.parentId) {
        const parent = this._nodesById.get(data.parentId);
        parent.appendChild(node);
      }

      if (data.children) stack.push(...data.children);
      if (data.shadowRoots) stack.push(...data.shadowRoots);
      if (data.pseudoElements) stack.push(...data.pseudoElements);
    }
  };

  setChildNodes({parentId, nodes}) {
    const parent = this._nodesById.get(parentId);
    if (!parent) return;

    for (const node of nodes) {
      this.loadNode(node);
    }
{'fraction_non_alphanumeric': 0.12028512028512028, 'fraction_numerical': 0.005049005049005049, 'mean_word_length': 2.911730545876887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4961924', 'n_tokens_mistral': 991, 'n_tokens_neox': 962, 'n_words': 313}