You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockvote/node_modules/min-document/document.js

96 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 引入所需的模块
var domWalk = require("dom-walk");
var Comment = require("./dom-comment.js"); // 用于创建注释节点
var DOMText = require("./dom-text.js"); // 用于创建文本节点
var DOMElement = require("./dom-element.js"); // 用于创建元素节点
var DocumentFragment = require("./dom-fragment.js"); // 用于创建文档片段
var Event = require("./event.js"); // 用于创建事件
var dispatchEvent = require("./event/dispatch-event.js"); // 用于派发事件
var addEventListener = require("./event/add-event-listener.js"); // 用于添加事件监听器
var removeEventListener = require("./event/remove-event-listener.js"); // 用于移除事件监听器
// 模块导出一个 Document 构造函数
module.exports = Document;
// Document 构造函数
function Document() {
// 确保通过构造函数来创建实例
if (!(this instanceof Document)) {
return new Document();
}
// 创建 head, body 和 html 元素,并构建文档结构
this.head = this.createElement("head");
this.body = this.createElement("body");
this.documentElement = this.createElement("html");
// 将 head 和 body 添加到 html 元素中
this.documentElement.appendChild(this.head);
this.documentElement.appendChild(this.body);
// childNodes 是一个包含文档根元素的数组
this.childNodes = [this.documentElement];
// 定义 nodeType 属性为 9表示文档节点
this.nodeType = 9;
}
// 定义 Document 原型对象,包含文档对象的各种方法
var proto = Document.prototype;
// 创建文本节点
proto.createTextNode = function createTextNode(value) {
return new DOMText(value, this);
};
// 创建命名空间元素
proto.createElementNS = function createElementNS(namespace, tagName) {
var ns = namespace === null ? null : String(namespace);
return new DOMElement(tagName, this, ns);
};
// 创建元素节点
proto.createElement = function createElement(tagName) {
return new DOMElement(tagName, this);
};
// 创建文档片段
proto.createDocumentFragment = function createDocumentFragment() {
return new DocumentFragment(this);
};
// 创建事件对象
proto.createEvent = function createEvent(family) {
return new Event(family);
};
// 创建注释节点
proto.createComment = function createComment(data) {
return new Comment(data, this);
};
// 通过 ID 获取元素
proto.getElementById = function getElementById(id) {
id = String(id); // 确保 ID 是字符串
// 使用 domWalk 遍历文档的所有节点,找到匹配的节点
var result = domWalk(this.childNodes, function (node) {
if (String(node.id) === id) {
return node; // 找到匹配的节点后返回
}
});
return result || null; // 如果没有找到则返回 null
};
// 引用 DOMElement 中的方法
proto.getElementsByClassName = DOMElement.prototype.getElementsByClassName;
proto.getElementsByTagName = DOMElement.prototype.getElementsByTagName;
proto.contains = DOMElement.prototype.contains;
// 引入事件监听相关的方法
proto.removeEventListener = removeEventListener;
proto.addEventListener = addEventListener;
proto.dispatchEvent = dispatchEvent;