libxml htmlParseDocument 忽略 htmlParseOption 标志

libxml htmlParseDocument ignoring htmlParseOption flags

寻找通过 PHP 打包以外的环境使用 libxml 的人,以确认忽略 HTML_PARSE_NOWARNING 标志。 仍然会生成警告。

来自 PHP 的源代码,在 C:

中实现 libxml
//one of these options is 64 or HTML_PARSE_NOWARNING
htmlCtxtUseOptions(ctxt, (int)options);

ctxt->vctxt.error = php_libxml_ctx_error;
ctxt->vctxt.warning = php_libxml_ctx_warning;
if (ctxt->sax != NULL) {
    ctxt->sax->error = php_libxml_ctx_error;
    ctxt->sax->warning = php_libxml_ctx_warning;
}
htmlParseDocument(ctxt); //this still produces warnings

libxml2 不会忽略 HTML_PARSE_NOWARNING 标志。使用 HTML_PARSE_NOWARNING 调用 htmlCtxtUseOptions 会导致取消注册警告处理程序(设置为 NULL)。但是 PHP 代码然后继续无条件地安装它自己的处理程序,使标志变得无用。 PHP 代码应该添加检查是否安装处理程序:

htmlCtxtUseOptions(ctxt, (int)options);

if (!(options & HTML_PARSE_NOERROR)) {
    ctxt->vctxt.error = php_libxml_ctx_error;
    if (ctxt->sax != NULL)
        ctxt->sax->error = php_libxml_ctx_error;
}
if (!(options & HTML_PARSE_NOWARNING)) {
    ctxt->vctxt.warning = php_libxml_ctx_warning;
    if (ctxt->sax != NULL)
        ctxt->sax->warning = php_libxml_ctx_warning;
}
htmlParseDocument(ctxt);

或在设置处理程序后调用htmlCtxtUseOptions

ctxt->vctxt.error = php_libxml_ctx_error;
ctxt->vctxt.warning = php_libxml_ctx_warning;
if (ctxt->sax != NULL) {
    ctxt->sax->error = php_libxml_ctx_error;
    ctxt->sax->warning = php_libxml_ctx_warning;
}

htmlCtxtUseOptions(ctxt, (int)options);
htmlParseDocument(ctxt);