一、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
例子:
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 对象。
支持的类型如下:
-
"text/html",
-
"text/xml",
-
"application/xml",
-
"application/xhtml+xml",
-
"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);
}