|
498 | 498 | htree)]
|
499 | 499 | (is (= [] selection))))))
|
500 | 500 |
|
| 501 | +(deftest has-child-test |
| 502 | + (testing "has-child selector combinator" |
| 503 | + (let [docs ["<div id=\"outermost\"><div><span id=\"innermost\"></span></div></div>" |
| 504 | + "<div id=\"outermost\"><div><span id=\"innermost\"></span></div><span id=\"sib\"></span></div>" |
| 505 | + "<div id=\"outermost\"><span id=\"sib\"></span><div><span id=\"innermost\"></span></div></div>"]] |
| 506 | + (doseq [doc docs] |
| 507 | + (let [htree (-> doc |
| 508 | + hickory/parse hickory/as-hickory)] |
| 509 | + (let [selection (select/select (select/has-child |
| 510 | + (select/id :innermost)) |
| 511 | + htree)] |
| 512 | + (is (and (= 1 (count selection)) |
| 513 | + (every? true? (map #(= :div (-> % :tag)) selection))))) |
| 514 | + ;; Check that a descendant selector can peer up past the |
| 515 | + ;; node having its descendants examined. |
| 516 | + (let [selection (select/select (select/has-child |
| 517 | + (select/descendant (select/id :outermost) |
| 518 | + (select/id :innermost))) |
| 519 | + htree)] |
| 520 | + (is (and (= 1 (count selection)) |
| 521 | + (every? true? (map #(= :div (-> % :tag)) selection))))) |
| 522 | + (let [selection (select/select (select/has-child (select/tag :a)) |
| 523 | + htree)] |
| 524 | + (is (= [] selection)))))))) |
| 525 | + |
| 526 | +(deftest parent-test |
| 527 | + (testing "parent selector combinator" |
| 528 | + (let [htree (hickory/as-hickory (hickory/parse html1))] |
| 529 | + (let [selection (select/select (select/parent (select/el-not select/any)) |
| 530 | + htree)] |
| 531 | + (is (= [] selection))) |
| 532 | + (let [selection (select/select (select/parent (select/tag :html) |
| 533 | + (select/tag :div) |
| 534 | + (select/tag :span)) |
| 535 | + htree)] |
| 536 | + (is (= [] selection))) |
| 537 | + (let [selection (select/select (select/parent (select/tag :body) |
| 538 | + (select/tag :div) |
| 539 | + (select/tag :span)) |
| 540 | + htree)] |
| 541 | + (is (and (= 1 (count selection)) |
| 542 | + (every? true? (map #(= :body (:tag %)) selection))))) |
| 543 | + (let [selection (select/select (select/parent (select/tag :div) |
| 544 | + select/any) |
| 545 | + htree)] |
| 546 | + (is (and (= 1 (count selection)) |
| 547 | + (every? true? (map #(= :div (-> % :tag)) |
| 548 | + selection))))) |
| 549 | + ;; Find any element that is a parent of another element |
| 550 | + (let [selection (select/select (select/parent select/any select/any) |
| 551 | + htree)] |
| 552 | + (is (and (= 4 (count selection)) |
| 553 | + (every? true? (mapv #(or (= :html (-> % :tag)) |
| 554 | + (= :body (-> % :tag)) |
| 555 | + (= :div (-> % :tag)) |
| 556 | + (= :span (-> % :tag))) |
| 557 | + selection)))))) |
| 558 | + ;; Check examples from the doc string. |
| 559 | + (let [htree (-> "<div><span class=\"foo\"><input disabled></input></span></div>" |
| 560 | + hickory/parse hickory/as-hickory)] |
| 561 | + (let [selection (select/select (select/parent (select/tag :div) |
| 562 | + (select/class :foo) |
| 563 | + (select/attr :disabled)) |
| 564 | + htree)] |
| 565 | + (is (= :div (-> selection first :tag))))) |
| 566 | + (let [htree (-> "<div><span class=\"foo\"><b><input disabled></input></b></span></div>" |
| 567 | + hickory/parse hickory/as-hickory)] |
| 568 | + (let [selection (select/select (select/parent (select/tag :div) |
| 569 | + (select/class :foo) |
| 570 | + (select/attr :disabled)) |
| 571 | + htree)] |
| 572 | + (is (= [] selection)))))) |
| 573 | + |
501 | 574 | (deftest follow-adjacent-test
|
502 | 575 | (testing "follow-adjacent selector combinator"
|
503 | 576 | (let [htree (hickory/as-hickory (hickory/parse html1))]
|
|
591 | 664 | (is (and (= 1 (count selection))
|
592 | 665 | (= :input (-> selection first :tag))))))))
|
593 | 666 |
|
| 667 | +(deftest has-descendant-test |
| 668 | + (testing "has-descendant selector combinator" |
| 669 | + (let [docs ["<div id=\"outermost\"><div><span id=\"innermost\"></span></div></div>" |
| 670 | + "<div id=\"outermost\"><div><span id=\"innermost\"></span></div><span id=\"sib\"></span></div>" |
| 671 | + "<div id=\"outermost\"><span id=\"sib\"></span><div><span id=\"innermost\"></span></div></div>"]] |
| 672 | + (doseq [doc docs] |
| 673 | + (let [htree (-> doc |
| 674 | + hickory/parse hickory/as-hickory)] |
| 675 | + (let [selection (select/select (select/and (select/tag :div) |
| 676 | + (select/has-descendant |
| 677 | + (select/id :innermost))) |
| 678 | + htree)] |
| 679 | + (is (and (= 2 (count selection)) |
| 680 | + (every? true? (map #(= :div (-> % :tag)) selection))))) |
| 681 | + ;; Check that a descendant selector can peer up past the |
| 682 | + ;; node having its descendants examined. |
| 683 | + (let [selection (select/select (select/and (select/tag :div) |
| 684 | + (select/has-descendant |
| 685 | + (select/descendant (select/id :outermost) |
| 686 | + (select/tag :span)))) |
| 687 | + htree)] |
| 688 | + (is (and (= 2 (count selection)) |
| 689 | + (every? true? (map #(= :div (-> % :tag)) selection))))) |
| 690 | + (let [selection (select/select (select/has-descendant (select/tag :a)) |
| 691 | + htree)] |
| 692 | + (is (= [] selection)))))))) |
| 693 | + |
| 694 | +(deftest ancestor-test |
| 695 | + (testing "ancestor selector combinator" |
| 696 | + (let [htree (hickory/as-hickory (hickory/parse html1))] |
| 697 | + (let [selection (select/select (select/ancestor (select/tag :h1)) |
| 698 | + htree)] |
| 699 | + (is (and (= 1 (count selection)) |
| 700 | + (= :h1 (-> selection first :tag))))) |
| 701 | + (let [selection (select/select (select/ancestor (select/class "cool") |
| 702 | + (select/tag :div)) |
| 703 | + htree)] |
| 704 | + (is (= 1 (count selection)) |
| 705 | + (= "deepestdiv" (-> selection first :attrs :id)))) |
| 706 | + (let [selection (select/select (select/ancestor (select/tag :div) |
| 707 | + select/any) |
| 708 | + htree)] |
| 709 | + (is (= 1 (count selection)))) |
| 710 | + (let [selection (select/select (select/ancestor (select/tag :span)) |
| 711 | + htree)] |
| 712 | + (is (= 2 (count selection)))) |
| 713 | + ;; Find any element that is a parent of another element |
| 714 | + (let [selection (select/select (select/parent select/any select/any) |
| 715 | + htree)] |
| 716 | + (is (and (= 4 (count selection)) |
| 717 | + (every? true? (mapv #(or (= :html (-> % :tag)) |
| 718 | + (= :body (-> % :tag)) |
| 719 | + (= :div (-> % :tag)) |
| 720 | + (= :span (-> % :tag))) |
| 721 | + selection)))))) |
| 722 | + ;; Check examples from doc string. |
| 723 | + (let [htree (-> "<div><span class=\"foo\"><input disabled></input></span></div>" |
| 724 | + hickory/parse hickory/as-hickory)] |
| 725 | + (let [selection (select/select (select/ancestor (select/tag :div) |
| 726 | + (select/class :foo) |
| 727 | + (select/attr :disabled)) |
| 728 | + htree)] |
| 729 | + (is (and (= 1 (count selection)) |
| 730 | + (= :div (-> selection first :tag)))))) |
| 731 | + (let [htree (-> "<div><span class=\"foo\"><b><input disabled></input></b></span></div>" |
| 732 | + hickory/parse hickory/as-hickory)] |
| 733 | + (let [selection (select/select (select/ancestor (select/tag :div) |
| 734 | + (select/class :foo) |
| 735 | + (select/attr :disabled)) |
| 736 | + htree)] |
| 737 | + (is (and (= 1 (count selection)) |
| 738 | + (= :div (-> selection first :tag)))))))) |
| 739 | + |
594 | 740 | (deftest follow-test
|
595 | 741 | (testing "follow selector combinator"
|
596 | 742 | (let [htree (hickory/as-hickory (hickory/parse html1))]
|
|
636 | 782 | htree)]
|
637 | 783 | (is (= :div (-> selection first :tag)))))))
|
638 | 784 |
|
639 |
| -(deftest has-descendant-test |
640 |
| - (testing "has-descendant selector combinator" |
641 |
| - (let [docs ["<div id=\"outermost\"><div><span id=\"innermost\"></span></div></div>" |
642 |
| - "<div id=\"outermost\"><div><span id=\"innermost\"></span></div><span id=\"sib\"></span></div>" |
643 |
| - "<div id=\"outermost\"><span id=\"sib\"></span><div><span id=\"innermost\"></span></div></div>"]] |
644 |
| - (doseq [doc docs] |
645 |
| - (let [htree (-> doc |
646 |
| - hickory/parse hickory/as-hickory)] |
647 |
| - (let [selection (select/select (select/and (select/tag :div) |
648 |
| - (select/has-descendant |
649 |
| - (select/id :innermost))) |
650 |
| - htree)] |
651 |
| - (is (and (= 2 (count selection)) |
652 |
| - (every? true? (map #(= :div (-> % :tag)) selection))))) |
653 |
| - ;; Check that a descendant selector can peer up past the |
654 |
| - ;; node having its descendants examined. |
655 |
| - (let [selection (select/select (select/and (select/tag :div) |
656 |
| - (select/has-descendant |
657 |
| - (select/descendant (select/id :outermost) |
658 |
| - (select/tag :span)))) |
659 |
| - htree)] |
660 |
| - (is (and (= 2 (count selection)) |
661 |
| - (every? true? (map #(= :div (-> % :tag)) selection))))) |
662 |
| - (let [selection (select/select (select/has-descendant (select/tag :a)) |
663 |
| - htree)] |
664 |
| - (is (= [] selection)))))))) |
665 |
| - |
666 |
| -(deftest has-child-test |
667 |
| - (testing "has-child selector combinator" |
668 |
| - (let [docs ["<div id=\"outermost\"><div><span id=\"innermost\"></span></div></div>" |
669 |
| - "<div id=\"outermost\"><div><span id=\"innermost\"></span></div><span id=\"sib\"></span></div>" |
670 |
| - "<div id=\"outermost\"><span id=\"sib\"></span><div><span id=\"innermost\"></span></div></div>"]] |
671 |
| - (doseq [doc docs] |
672 |
| - (let [htree (-> doc |
673 |
| - hickory/parse hickory/as-hickory)] |
674 |
| - (let [selection (select/select (select/has-child |
675 |
| - (select/id :innermost)) |
676 |
| - htree)] |
677 |
| - (is (and (= 1 (count selection)) |
678 |
| - (every? true? (map #(= :div (-> % :tag)) selection))))) |
679 |
| - ;; Check that a descendant selector can peer up past the |
680 |
| - ;; node having its descendants examined. |
681 |
| - (let [selection (select/select (select/has-child |
682 |
| - (select/descendant (select/id :outermost) |
683 |
| - (select/id :innermost))) |
684 |
| - htree)] |
685 |
| - (is (and (= 1 (count selection)) |
686 |
| - (every? true? (map #(= :div (-> % :tag)) selection))))) |
687 |
| - (let [selection (select/select (select/has-child (select/tag :a)) |
688 |
| - htree)] |
689 |
| - (is (= [] selection)))))))) |
690 |
| - |
691 | 785 | (deftest graceful-boundaries-test
|
692 | 786 | ;; Testing some problematic expressions to make sure they gracefully
|
693 | 787 | ;; return empty results.
|
|
0 commit comments