瀏覽代碼

LibWeb: Add DOMTreeModel::index_for_node()

This is needed for telling the DOM Inspector to select a given Node.
Sam Atkins 4 年之前
父節點
當前提交
8eef509c1b
共有 2 個文件被更改,包括 18 次插入0 次删除
  1. 16 0
      Userland/Libraries/LibWeb/DOMTreeModel.cpp
  2. 2 0
      Userland/Libraries/LibWeb/DOMTreeModel.h

+ 16 - 0
Userland/Libraries/LibWeb/DOMTreeModel.cpp

@@ -131,4 +131,20 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
     return {};
     return {};
 }
 }
 
 
+GUI::ModelIndex DOMTreeModel::index_for_node(DOM::Node* node) const
+{
+    if (!node)
+        return {};
+
+    DOM::Node* parent = node->parent();
+    if (!parent)
+        return {};
+
+    auto maybe_row = parent->index_of_child(*node);
+    if (maybe_row.has_value())
+        return create_index(maybe_row.value(), 0, node);
+
+    return {};
+}
+
 }
 }

+ 2 - 0
Userland/Libraries/LibWeb/DOMTreeModel.h

@@ -26,6 +26,8 @@ public:
     virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
     virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;
     virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
     virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
 
 
+    GUI::ModelIndex index_for_node(DOM::Node*) const;
+
 private:
 private:
     explicit DOMTreeModel(DOM::Document&);
     explicit DOMTreeModel(DOM::Document&);