chromium/third_party/blink/web_tests/fast/xpath/4XPath/Core/test_core_functions.html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../../../resources/js-test.js"></script>
<script src="test.js"></script>
<script src="../../xpath-test-pre.js"></script>
</head>
<body>
<div id="console"></div>

<script>
    function nsResolver(prefix) {
        if (prefix == 'f')
            return 'http://foo.com';
        return null;
    }

    shouldBe('DOM.evaluate("last()", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '1');
    shouldBe('DOM.evaluate("position()", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '1');
    shouldBe('DOM.evaluate("count(/ROOT | /ROOT/CHILD1)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '2');

    result = DOM.evaluate("id(1)", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id(1)", result, [CHILD2]);

    result = DOM.evaluate("id('1 1')", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id('1 1')", result, [CHILD2]);

    result = DOM.evaluate("id('0')", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id('0')", result, []);

    result = DOM.evaluate("id('0 1')", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id('0 1')", result, [CHILD2]);

    result = DOM.evaluate("id('0 1 1')", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id('0 1 1')", result, [CHILD2]);

    result = DOM.evaluate("id('0 0 1 1')", CHILD1, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("id('0 0 1 1')", result, [CHILD2]);

    shouldBe('DOM.evaluate("local-name(/empty)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("local-name(//f:CHILD3)", CHILD1, nsResolver, XPathResult.ANY_TYPE, null).stringValue', '"CHILD3"');
    shouldBe('DOM.evaluate("namespace-uri(/empty)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("namespace-uri(//f:CHILD3)", CHILD1, nsResolver, XPathResult.ANY_TYPE, null).stringValue', '"http://foo.com"');
    shouldBe('DOM.evaluate("name(//f:CHILD3)", CHILD1, nsResolver, XPathResult.ANY_TYPE, null).stringValue', '"foo:CHILD3"');

    strNodeset3 = "            Text1  ";

    shouldBe('DOM.evaluate("string(//CHILD1)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', 'strNodeset3');
    shouldBe('DOM.evaluate("concat(//CHILD1, \'3.14\', \'Hi\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', 'strNodeset3 + "3.14Hi"');
    shouldBe('DOM.evaluate("starts-with(//CHILD1, \'3.14\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("starts-with(//CHILD1, //CHILD1)", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("starts-with(//CHILD1, \'\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("contains(//CHILD1, \'3.14\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("contains(//CHILD1, //CHILD1)", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("contains(//CHILD1, \'\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("substring-before(\'3.14Hi\', \'Hi\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"3.14"');
    shouldBe('DOM.evaluate("substring-before(\'3.14Hi\', \'\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("substring-after(\'3.14Hi\', \'3.14\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"Hi"');
    shouldBe('DOM.evaluate("substring-after(\'3.14Hi\', \'\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"3.14Hi"');
    shouldBe('DOM.evaluate("substring(\'3.14Hi\', \'3.14\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"14Hi"');
    shouldBe('DOM.evaluate("substring(\'3.14Hi\', \'3.14\', 1)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"1"');
    shouldBe('DOM.evaluate("substring(\'12345\', 2, 3)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"234"');
    shouldBe('DOM.evaluate("substring(\'12345\', 2)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"2345"');
    shouldBe('DOM.evaluate("substring(\'12345\', 1.5, 2.6)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"234"');
    shouldBe('DOM.evaluate("substring(\'12345\', 0, 3)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"12"');
    shouldBe('DOM.evaluate("substring(\'12345\', number(\'NaN\'), 3)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("substring(\'12345\', 1, number(\'NaN\'))", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("substring(\'12345\', -42, 1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"12345"');
    shouldBe('DOM.evaluate("substring(\'12345\', -1 div 0, 1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '""');
    shouldBe('DOM.evaluate("string-length(\'3.14Hi\')", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '6');
    shouldBe('DOM.evaluate("normalize-space(\'Ht    \t There\t   Mike\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"Ht There Mike"');
    shouldBe('DOM.evaluate("translate(\'Ht    \t There\t   Mike\', \'abcdefg\', \'ABCDEFG\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"Ht    \t ThErE\t   MikE"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'e\', \'a\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"hallo world"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'e\', \'abc\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"hallo world"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'el\', \'a\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"hao word"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'abcdefgabc\', \'ABCDEFG123\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"hEllo worlD"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'abcdefghhe\', \'ABCDEFGH\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"HEllo worlD"');
    shouldBe('DOM.evaluate("translate(\'hello world\', \'abcdefgh\', \'\')", CHILD1, null, XPathResult.ANY_TYPE, null).stringValue', '"llo worl"');

    shouldBe('DOM.evaluate("boolean(\'3.14\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("not(\'3.14Hi\')", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("true()", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("false()", CHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');

    shouldBe('DOM.evaluate("number(\'NaN\')", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'NaN');
    shouldBe('DOM.evaluate("floor(3.14)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '3');
    shouldBe('DOM.evaluate("floor(number())", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'NaN');
    shouldBe('DOM.evaluate("floor(1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'Infinity');
    shouldBe('DOM.evaluate("floor(-1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '-Infinity');
    shouldBe('DOM.evaluate("floor(0.5)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '0');
    shouldBe('DOM.evaluate("floor(-0.5)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '-1');
    shouldBe('DOM.evaluate("ceiling(3.14)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '4');
    shouldBe('DOM.evaluate("ceiling(number(\'NaN\'))", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'NaN');
    shouldBe('DOM.evaluate("ceiling(1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'Infinity');
    shouldBe('DOM.evaluate("ceiling(0.5)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '1');
    shouldBe('DOM.evaluate("ceiling(-0.5)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '-0');
    shouldBe('DOM.evaluate("round(3.14)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '3');
    shouldBe('DOM.evaluate("round(-4.5)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '-4');
    shouldBe('DOM.evaluate("round(number(\'NaN\'))", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'NaN');
    shouldBe('DOM.evaluate("round(1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', 'Infinity');
    shouldBe('DOM.evaluate("round(-1 div 0)", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '-Infinity');
    shouldBe('DOM.evaluate("round(\'12345\')", CHILD1, null, XPathResult.ANY_TYPE, null).numberValue', '12345');
    shouldBe('DOM.evaluate("lang(\'en\')", LCHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("lang(\'en\')", LCHILD2, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("lang(\'\')", LCHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'true');
    shouldBe('DOM.evaluate("lang(\'\')", LCHILD2, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("lang(\'foo\')", LCHILD1, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');
    shouldBe('DOM.evaluate("lang(\'foo\')", LCHILD2, null, XPathResult.ANY_TYPE, null).booleanValue', 'false');

</script>
</body>
</html>