File: test_custom_functions.rb

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (128 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

require "helper"

module Nokogiri
  module XSLT
    class TestCustomFunctions < Nokogiri::TestCase
      def setup
        super
        @xml = Nokogiri.XML(<<~EOXML)
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
           "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
            <head>
              <meta http-equiv="Content-type" content="application/xhtml+xml"/>
              <title>Foo</title>
            </head>
            <body>
              <h1>Foo</h1>
              <p>Lorem ipsum.</p>
            </body>
          </html>
        EOXML
      end

      def test_function
        skip_unless_libxml2("java version doesn't support this feature")
        foo = Class.new do
          def capitalize(nodes)
            nodes.first.content.upcase
          end
        end

        XSLT.register("http://e.org/functions", foo)

        xsl = Nokogiri.XSLT(<<~EOXSL)
          <?xml version="1.0"?>
          <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:f="http://e.org/functions"
            extension-element-prefixes="f">
            <xsl:template match="@*|node()">
              <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
                <xsl:apply-imports/>
              </xsl:copy>
            </xsl:template>
            <xsl:template match="text()">
              <xsl:copy-of select="f:capitalize(.)"/>
            </xsl:template>
          </xsl:stylesheet>
        EOXSL
        result = xsl.transform(@xml)
        assert_match(/FOO/, result.css("title").first.text)
      end

      def test_function_arguments
        skip_unless_libxml2("java version doesn't support this feature")
        foo = Class.new do
          include MiniTest::Assertions
          # Minitest 5 uses `self.assertions` in `assert()` which is not
          # defined in the Minitest::Assertions module :-(
          attr_writer :assertions

          def assertions
            @assertions ||= 0
          end

          def multiarg(*args)
            assert_equal(["abc", "xyz"], args)
            args.first
          end

          def numericarg(arg)
            assert_equal(42, arg)
            arg
          end
        end

        xsl = Nokogiri.XSLT(<<~EOXSL, "http://e.org/functions" => foo)
          <?xml version="1.0"?>
          <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:f="http://e.org/functions"
            extension-element-prefixes="f">
            <xsl:template match="text()">
              <xsl:copy-of select="f:multiarg('abc', 'xyz')"/>
              <xsl:copy-of select="f:numericarg(42)"/>
            </xsl:template>
          </xsl:stylesheet>
        EOXSL

        xsl.transform(@xml)
      end

      def test_function_XSLT
        skip_unless_libxml2("java version doesn't support this feature")
        foo = Class.new do
          def america(nodes)
            nodes.first.content.upcase
          end
        end

        xsl = Nokogiri.XSLT(<<~EOXSL, "http://e.org/functions" => foo)
          <?xml version="1.0"?>
          <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:f="http://e.org/functions"
            extension-element-prefixes="f">
            <xsl:template match="@*|node()">
              <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
                <xsl:apply-imports/>
              </xsl:copy>
            </xsl:template>
            <xsl:template match="text()">
              <xsl:copy-of select="f:america(.)"/>
            </xsl:template>
          </xsl:stylesheet>
        EOXSL
        result = xsl.transform(@xml)
        assert_match(/FOO/, result.css("title").first.text)
      end
    end
  end
end