Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNode::startPos 与CNode::endPos颠倒的问题 #23

Open
dptr opened this issue Dec 30, 2015 · 3 comments
Open

CNode::startPos 与CNode::endPos颠倒的问题 #23

dptr opened this issue Dec 30, 2015 · 3 comments

Comments

@dptr
Copy link

dptr commented Dec 30, 2015

问题场景:

给定这样一个page:

std::string page = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>Title</title></head><body><img src=\"file:///d:/test.png\" /><img src=\"file:///d:/test2.png\" /><img src=\"http://blablabla.png\" /><div><div><img src=\"http://asdfasfdsaf.com/asdf.png\" alt=\"alternative text\"/><img src=\"file:///d:/test3.png\" /></div><div1><div2><div3><img id=\"imgId2\" src=\"http://www.taobao.com\" /><pre><img src=\"file:///foo\" id=\"bar\" id=\"notexists\"/></pre></div3></div2></div1></div></body></html>";

在其中查找所有tag为img的node

auto nodes = doc.find("IMG");

然后取出这些node的 startPos 和 endPos ,
发现它们 startPos 是指向标签末尾的,endPos指向 标签开始,是不是这两个函数的实现搞反了?

@nifflin
Copy link

nifflin commented Dec 30, 2015

哥们儿,qq多少

@qyvlik
Copy link

qyvlik commented Apr 13, 2016

确实是反过来了。

@qyvlik
Copy link

qyvlik commented Apr 13, 2016

测试代码如下:

void test_image_tag() {
    std::string page = "<img />";
    CDocument doc;

    doc.parse(page.c_str());
    CNode node = doc.find("IMG").nodeAt(0);
    std::cout << "startPos:" << node.startPos() <<" endPos:"<< node.endPos() << std::endl;
    std::cout << "startPosOuter:" << node.startPosOuter() <<" endPosOuter:"<< node.endPosOuter() << std::endl;
}

输出如下:

startPos:7 endPos:0
startPosOuter:0 endPosOuter:0

另一个测试代码

void test_div_tag() {
    std::string page = "<div></div>";
    CDocument doc;

    doc.parse(page.c_str());
    CNode node = doc.find("div").nodeAt(0);
    std::cout << "startPos:" << node.startPos() <<" endPos:"<< node.endPos() << std::endl;
    std::cout << "startPosOuter:" << node.startPosOuter() <<" endPosOuter:"<< node.endPosOuter() << std::endl;
}

输出如下:

startPos:5 endPos:5
startPosOuter:0 endPosOuter:11

测试 input 标签和 img 标签结果类似。

@lazytiger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants