HtmlElement *HtmlParser::FindElementByNameNS(const char *name, const char *ns, HtmlElement *from) { HtmlElement *el = from ? from : rootElement; if (from) goto FindNext; if (!el) return NULL; CheckNext: if (el->NameIs(name) || ns && el->NameIsNS(name, ns)) return el; FindNext: if (el->down) { el = el->down; goto CheckNext; } if (el->next) { el = el->next; goto CheckNext; } // backup in the tree HtmlElement *parent = el->up; while (parent) { if (parent->next) { el = parent->next; goto CheckNext; } parent = parent->up; } return NULL; }
static void HtmlParser10() { HtmlParser p; HtmlElement *root = p.Parse("<!xml version='1.0'?><x:a xmlns:x='http://example.org/ns/x'><x:b attr='val'/></x:a>"); utassert(2 == p.ElementsCount()); utassert(2 == p.TotalAttrCount()); utassert(root->NameIs("x:a") && root->NameIsNS("a", "http://example.org/ns/x")); HtmlElement *node = p.FindElementByName("b"); utassert(!node); node = p.FindElementByNameNS("b", "http://example.org/ns/x"); utassert(node); utassert(node->NameIs("x:b") && node->NameIsNS("b", "http://example.org/ns/x")); ScopedMem<WCHAR> val(node->GetAttribute("attr")); utassert(str::Eq(val, L"val")); // TODO: XML tags are case sensitive (HTML tags aren't) node = p.FindElementByName("X:B"); utassert(node && node->NameIs("X:B")); }