summaryrefslogtreecommitdiff
path: root/test/attributes_test.rb
blob: 3fd2ba3bcd86319f06c4911979a280207420e738 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'test_helper'

context "Attributes" do
  test "creates an attribute" do
    doc = document_from_string(":frog: Tanglefoot")
    assert_equal doc.attributes['frog'], 'Tanglefoot'
  end

  test "creates an attribute by fusing a multi-line value" do
    str = <<-EOS
:description: This is the first +
              Ruby implementation of +
              AsciiDoc.
    EOS
    doc = document_from_string(str)
    assert_equal doc.attributes['description'], 'This is the first Ruby implementation of AsciiDoc.'
  end

  test "deletes an attribute" do
    doc = document_from_string(":frog: Tanglefoot\n:frog!:")
    assert_equal nil, doc.attributes['frog']
  end

  test "doesn't choke when deleting a non-existing attribute" do
    doc = document_from_string(":frog!:")
    assert_equal nil, doc.attributes['frog']
  end

  test "render properly with simple names" do
    html = render_string(":frog: Tanglefoot\nYo, {frog}!")
    result = Nokogiri::HTML(html)
    assert_equal 'Yo, Tanglefoot!', result.css("p").first.content.strip
  end

  test "convert multi-word names and render" do
    html = render_string("Main Header\n===========\n:My frog: Tanglefoot\nYo, {myfrog}!")
    result = Nokogiri::HTML(html)
    assert_equal 'Yo, Tanglefoot!', result.css("p").first.content.strip
  end

  test "ignores lines with bad attributes" do
    html = render_string("This is\nblah blah {foobarbaz}\nall there is.")
    result = Nokogiri::HTML(html)
    assert_no_match /blah blah/m, result.css("p").first.content.strip
  end

  # See above - AsciiDoc says we're supposed to delete lines with bad
  # attribute refs in them. AsciiDoc is strange.
  #
  # test "Unknowns" do
  #   html = render_string("Look, a {gobbledygook}")
  #   result = Nokogiri::HTML(html)
  #   assert_equal("Look, a {gobbledygook}", result.css("p").first.content.strip)
  # end

  test "substitutes inside unordered list items" do
    html = render_string(":foo: bar\n* snort at the {foo}\n* yawn")
    result = Nokogiri::HTML(html)
    assert_match /snort at the bar/, result.css("li").first.content.strip
  end

  test "renders attribute until it's deleted" do
    pending "Not working yet (will require adding element-specific attributes or early attr substitution during parsing)"
    # html = render_string(":foo: bar\nCrossing the {foo}\n\n:foo!:\nBelly up to the {foo}")
    # result = Nokogiri::HTML(html)
    # assert_match /Crossing the bar/, result.css("p").first.content.strip
    # assert_no_match /Belly up to the bar/, result.css("p").last.content.strip
  end

  test "doesn't disturb attribute-looking things escaped with backslash" do
    html = render_string(":foo: bar\nThis is a \\{foo} day.")
    result = Nokogiri::HTML(html)
    assert_equal 'This is a {foo} day.', result.css('p').first.content.strip
  end

  test "doesn't disturb attribute-looking things escaped with literals" do
    html = render_string(":foo: bar\nThis is a +++{foo}+++ day.")
    result = Nokogiri::HTML(html)
    #assert_equal 'This is a {foo} day.', result.css('p').first.content.strip
    pending "Don't yet have inline passthrough working"
  end

  test "doesn't substitute attributes inside code blocks" do
    pending "whut?"
  end

  test "doesn't substitute attributes inside literal blocks" do
    pending "whut?"
  end

  context "Block attributes" do
    test "Position attributes assigned to block" do
      input = <<-EOS
[quote, Name, Source]
____
A famous quote.
____
      EOS
      doc = document_from_string(input)
      qb = doc.elements.first
      assert_equal 'quote', qb.attributes['style']
      assert_equal 'quote', qb.attr(:style)
      assert_equal 'Name', qb.attributes['attribution']
      assert_equal 'Source', qb.attributes['citetitle']
    end

    test "Block attributes are additive" do
      input = <<-EOS
[id='foo']
[role='lead']
A paragraph.
      EOS
      doc = document_from_string(input)
      para = doc.elements.first
      assert_equal 'foo', para.id
      assert_equal 'lead', para.attributes['role']
    end
  end

  context "intrinsics" do

    test "substitute intrinsics" do
      Asciidoctor::INTRINSICS.each_pair do |key, value|
        html = render_string("Look, a {#{key}} is here")
        # can't use Nokogiri because it interprets the HTML entities and we can't match them
        assert_match /Look, a #{Regexp.escape(value)} is here/, html
      end
    end

    test "don't escape intrinsic substitutions" do
      html = render_string('happy{nbsp}together')
      assert_match /happy&#160;together/, html
    end

    test "escape special characters" do
      html = render_string('<node>&</node>')
      assert_match /&lt;node&gt;&amp;&lt;\/node&gt;/, html
    end
    
  end

end