Chromium 中window.DOMParser接口说明c++

news/2024/10/19 17:02:57/
htmledit_views">

一、DOMParser

DOMParser 可以将存储在字符串中的 XML 或 HTML 源代码解析为一个 DOM Document。

备注: XMLHttpRequest 支持从 URL 可寻址资源解析 XML 和 HTML,在其response 属性中返回Document

你可以使用XMLSerializer 接口执行相反的操作 - 将 DOM 树转换为 XML 或 HTML 源。

对于 HTML 文档,你还可以通过设置 Element.innerHTML 和outerHTML 属性的值,将部分 DOM 替换为从 HTML 构建的新 DOM 树。还可以读取这些属性以获取对应于相应 DOM 子树的 HTML 片段。

更多参考:DOMParser - Web API | MDN (mozilla.org)

二、DOMParser接口c++定义:

1、dom_parser.idl定义

third_party\blink\renderer\core\xml\dom_parser.idl

// https://w3c.github.io/DOM-Parsing/#the-domparser-interfaceenum SupportedType {"text/html","text/xml","application/xml","application/xhtml+xml","image/svg+xml"
};[Exposed=Window
] interface DOMParser {[CallWith=ScriptState] constructor();[NewObject] Document parseFromString(HTMLString str, SupportedType type, optional ParseFromStringOptions options = {});
};

注意:parseFromString返回的是document 

例子:

html" title=javascript>javascript">let parser = new DOMParser();
let doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
// 返回一个 Document 对象,但不是 SVGDocument,也不是 HTMLDocumentparser = new DOMParser();
doc = parser.parseFromString(stringContainingXMLSource, "image/svg+xml");
// 返回一个 SVGDocument 对象,同时也是一个 Document 对象。parser = new DOMParser();
doc = parser.parseFromString(stringContainingHTMLSource, "text/html");
// 返回一个 HTMLDocument 对象,同时也是一个 Document 对象。

 支持的类型如下:

  1. "text/html",

  2. "text/xml",

  3. "application/xml",

  4. "application/xhtml+xml",

  5. "image/svg+xml"

 2、dom_parser.idl接口实现blink:

     third_party\blink\renderer\core\xml\dom_parser.h

     third_party\blink\renderer\core\xml\dom_parser.cc

namespace blink {class Document;
class ParseFromStringOptions;
class LocalDOMWindow;
class ScriptState;class CORE_EXPORT DOMParser final : public ScriptWrappable {DEFINE_WRAPPERTYPEINFO();public:static DOMParser* Create(ScriptState* script_state) {return MakeGarbageCollected<DOMParser>(script_state);}explicit DOMParser(ScriptState*);Document* parseFromString(const String&,const String& type,const ParseFromStringOptions* options);void Trace(Visitor*) const override;LocalDOMWindow* GetWindow() const { return window_.Get(); }private:WeakMember<LocalDOMWindow> window_;
};}  // namespace blink

 3、dom_parser.idl接口实现v8:

    out\Debug\gen\third_party\blink\renderer\bindings\core\v8\v8_dom_parser.h

    out\Debug\gen\third_party\blink\renderer\bindings\core\v8\v8_dom_parser.cc

   

void ParseFromStringOperationCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {RUNTIME_CALL_TIMER_SCOPE_DISABLED_BY_DEFAULT(info.GetIsolate(), "Blink_DOMParser_parseFromString");
BLINK_BINDINGS_TRACE_EVENT("DOMParser.parseFromString");v8::Isolate* isolate = info.GetIsolate();
const ExceptionContextType exception_context_type = ExceptionContextType::kOperationInvoke;
const char* const class_like_name = "DOMParser";
const char* const property_name = "parseFromString";
ExceptionState exception_state(isolate, exception_context_type, class_like_name, property_name);
if (UNLIKELY(info.Length() < 2)) {exception_state.ThrowTypeError(ExceptionMessages::NotEnoughArguments(2, info.Length()));
return;
}v8::Local<v8::Object> v8_receiver = info.This();
DOMParser* blink_receiver = V8DOMParser::ToWrappableUnsafe(isolate, v8_receiver);
ExecutionContext* execution_context_of_document_tree = bindings::ExecutionContextFromV8Wrappable(blink_receiver);
auto&& arg1_str = NativeValueTraits<IDLStringStringContextTrustedHTML>::ArgumentValue(isolate, 0, info[0], exception_state, execution_context_of_document_tree);
if (UNLIKELY(exception_state.HadException())) {return;
}
auto&& arg2_type = NativeValueTraits<V8SupportedType>::ArgumentValue(isolate, 1, info[1], exception_state);
if (UNLIKELY(exception_state.HadException())) {return;
}
decltype(NativeValueTraits<ParseFromStringOptions>::NativeValue(std::declval<v8::Isolate*>(), std::declval<v8::Local<v8::Value>>(), std::declval<ExceptionState&>())) arg3_options;
if (info[2]->IsUndefined()) {arg3_options = ParseFromStringOptions::Create();
} else {arg3_options = NativeValueTraits<ParseFromStringOptions>::ArgumentValue(isolate, 2, info[2], exception_state);
if (UNLIKELY(exception_state.HadException())) {return;
}
}
auto&& return_value = blink_receiver->parseFromString(arg1_str, arg2_type, arg3_options);
bindings::V8SetReturnValue(info, return_value, blink_receiver);
}

三、更多xml解析具体实现参考 third_party\blink\renderer\core\xml\文件夹下


http://www.ppmy.cn/news/1540296.html

相关文章

QT的文件操作类 QFile

QFile 是 Qt 框架中用于文件处理的一个类。它提供了读取和写入文件的功能&#xff0c;支持文本和二进制文 件。 QFile 继承自 QIODevice &#xff0c;因此它可以像其他IO设备一样使用。 主要功能 文件读写&#xff1a; QFile 支持打开文件进行读取或写入操作文件信息&#x…

Android Studio开发Kotlin项目中遇到的问题解决集

背景&#xff1a;Android Studio 2022.3.1 1.Unexpected tokens (use ; to separate expressions on the same line) 无法在同一行声明一个变量并实例化。 解决&#xff1a;分开 &#xff08;1&#xff09; var aaCo:Runoob<String>aaCoRunoob("aa") &…

Linux驱动开发——platform平台总线

bus_type 一、主要作用 设备管理 bus_type负责管理连接在特定总线上的设备。它维护一个设备链表&#xff0c;其中包含了所有注册到该总线上的设备。通过这个链表&#xff0c;内核可以方便地遍历和管理连接在该总线上的设备。例如&#xff0c;对于 PCI 总线&#xff0c;bus_typ…

掌握Go语言`runtime`包:性能优化与实战指南

掌握Go语言runtime包&#xff1a;性能优化与实战指南 引言第一部分&#xff1a;初识runtime包runtime包概述runtime包的核心功能 第二部分&#xff1a;常用功能详解Goroutine管理runtime.Goexitruntime.Goschedruntime.NumGoroutine 内存管理runtime.MemStatsruntime.GC 系统信…

10 django管理系统 - 管理员管理 - 新建管理员(通过模态框和ajax实现)

在文章“04 django管理系统 - 部门管理 - 新增部门”中&#xff0c;我们通过传统的新增页面来实现部门的添加。 在本文中&#xff0c;我们通过模态框和ajax来实现管理员的新增。 首先在admin_list.html中新建入口&#xff0c;使用按钮 <div class"panel-heading&quo…

UDP——Socket

UDP——Socket只是与本地的ip和端口号相捆绑——不与对方ip和端口捆绑 发送包括&#xff08;我的Socket值发的内容对方ip和端口&#xff09; 然后任何一个守候在UDP端口的节点收&#xff08;对方发的内容对方端节点&#xff09; UDP和IP都是数据报发送

1.Springboot之ApplicationContextListenerConfig

Springboot框架中提供了两种类型的应用上下文ApplicationContext&#xff0c;分别为&#xff1a; AnnotationConfigServletWebServerApplicationContext。AnnotationConfigReactiveWebServerApplicationContext。 public class SpringApplication {public SpringApplication(…

Olap数据处理

一、OLAP 是什么 1. OLAP的定义 OLAP&#xff08;Online Analytical Processing&#xff0c;联机分析处理&#xff09;是一种软件技术&#xff0c;它主要专注于复杂的分析操作&#xff0c;帮助分析人员、管理人员或执行人员从多角度对信息进行快速、一致、交互地存取&#xf…