libxml htmlParseDocument ignoring htmlParseOption flags

≡放荡痞女 提交于 2019-12-11 07:27:59

问题


Looking for someone who uses libxml through an environment other than packaged with PHP to confirm the HTML_PARSE_NOWARNING flag is ignored. Warnings are still generated.

Source code from PHP, implementing libxml in C:

//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

回答1:


libxml2 does not ignore the HTML_PARSE_NOWARNING flag. Calling htmlCtxtUseOptions with HTML_PARSE_NOWARNING causes the warning handlers to be unregistered (set to NULL). But the PHP code then proceeds to install its own handlers unconditionally, rendering the flag useless. The PHP code should either add a check whether to install the handlers:

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);

Or call htmlCtxtUseOptions after setting the handlers:

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);


来源:https://stackoverflow.com/questions/41860683/libxml-htmlparsedocument-ignoring-htmlparseoption-flags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!