1
1
package org.asciidoctor.extension
2
2
3
3
import org.asciidoctor.Asciidoctor
4
+ import org.asciidoctor.Options
4
5
import org.asciidoctor.OptionsBuilder
5
6
import org.asciidoctor.SafeMode
6
7
import org.asciidoctor.util.ClasspathResources
@@ -9,36 +10,59 @@ import org.jboss.arquillian.spock.ArquillianSputnik
9
10
import org.jboss.arquillian.test.api.ArquillianResource
10
11
import org.jsoup.Jsoup
11
12
import org.jsoup.nodes.Document
13
+ import org.jsoup.select.Elements
12
14
import org.junit.rules.TemporaryFolder
13
15
import org.junit.runner.RunWith
14
16
import spock.lang.Specification
15
17
18
+ import static java.nio.charset.StandardCharsets.UTF_8
19
+
16
20
@RunWith (ArquillianSputnik )
17
21
class WhenABlockMacroProcessorCreatesATable extends Specification {
18
22
19
23
public static final String FIRST_TD = ' td:first-child'
20
24
public static final String SECOND_TD = ' td:nth-child(2)'
21
25
public static final String THIRD_TD = ' td:nth-child(3)'
22
26
public static final String IMG_ELEMENT = ' img'
27
+ public static final String COL = ' col'
28
+ public static final String STYLE = ' style'
29
+ public static final String WIDTH = ' width'
30
+ public static final String WIDTH_2 = ' 2%'
31
+ public static final String WIDTH_3 = ' 3%'
32
+ public static final String WIDTH_20 = ' 20%'
33
+ public static final String WIDTH_40 = ' 40%'
23
34
public static final String CONTRIBUTOR = ' bob'
24
35
public static final String BLOCKMACRO_NAME = ' githubcontributors'
25
36
26
37
public static final String AVATAR_URL_REGEXP = / https:\/\/ avatars.githubusercontent.com\/ u\/ .*/
38
+ public static final String CSS_QUERY_TABLE = ' table'
39
+ public static final String CLASS_GRID_ROWS = ' grid-rows'
40
+ public static final String CLASS_HALIGN_LEFT = ' halign-left'
41
+ public static final String CLASS_HALIGN_CENTER = ' halign-center'
42
+ public static final String ATTR_SRC = ' src'
43
+ public static final int THREE = 3
27
44
28
45
@ArquillianResource
29
46
private Asciidoctor asciidoctor
30
47
31
48
@ArquillianResource
32
49
private TemporaryFolder tmp
33
50
51
+ @ArquillianResource
52
+ private ClasspathResources classpathResources
53
+
34
54
private static final String DOCUMENT = '''
35
55
= AsciidoctorJRuby contributors
36
56
37
57
githubcontributors::asciidoctor/asciidoctorj[]
38
58
'''
39
59
40
- @ArquillianResource
41
- private ClasspathResources classpathResources
60
+ private static final String DOCUMENT_WITH_NEGATIVE_WIDTHS = '''
61
+ = AsciidoctorJRuby contributors
62
+
63
+ githubcontributors::asciidoctor/asciidoctorj[widths="2,3,-1"]
64
+ '''
65
+
42
66
43
67
def setup () {
44
68
TestHttpServer . start([' http://api.github.com/repos/asciidoctor/asciidoctorj/contributors' : classpathResources. getResource(' githubcontributors.json' )])
@@ -60,18 +84,61 @@ githubcontributors::asciidoctor/asciidoctorj[]
60
84
asciidoctor. convert(DOCUMENT , OptionsBuilder . options(). safe(SafeMode . SAFE ). inPlace(false ). baseDir(tmp. getRoot()). toDir(tmp. getRoot()). toFile(resultFile))
61
85
62
86
then :
63
- Document htmlDocument = Jsoup . parse(resultFile, ' UTF-8' )
87
+ Document htmlDocument = Jsoup . parse(resultFile, UTF_8 . name())
88
+
89
+ Elements cols = htmlDocument. select(COL )
90
+ cols. size() == THREE
91
+ cols. get(0 ). attr(STYLE ). contains(WIDTH_20 ) || cols. get(0 ). attr(WIDTH ). equals(WIDTH_20 )
92
+ cols. get(1 ). attr(STYLE ). contains(WIDTH_40 ) || cols. get(1 ). attr(WIDTH ). equals(WIDTH_40 )
93
+ cols. get(2 ). attr(STYLE ). contains(WIDTH_40 ) || cols. get(2 ). attr(WIDTH ). equals(WIDTH_40 )
94
+
95
+ htmlDocument. select(CSS_QUERY_TABLE ). hasClass(CLASS_GRID_ROWS )
96
+
97
+ htmlDocument. select(FIRST_TD ). every { tdElement -> tdElement. select(IMG_ELEMENT ). size() == 1 }
98
+ htmlDocument. select(FIRST_TD ). every { tdElement -> tdElement. select(IMG_ELEMENT )[0 ]. attr(ATTR_SRC ) =~ AVATAR_URL_REGEXP }
99
+
100
+ htmlDocument. select(SECOND_TD ). size() == htmlDocument. select(SECOND_TD ) != 0
101
+ htmlDocument. select(SECOND_TD ). size() != 0
102
+
103
+ htmlDocument. select(SECOND_TD ). every { tdElement -> tdElement. hasClass(CLASS_HALIGN_LEFT ) }
104
+ htmlDocument. select(THIRD_TD ). every { tdElement -> tdElement. hasClass(CLASS_HALIGN_CENTER ) }
105
+
106
+ htmlDocument. select(SECOND_TD ). find { tdElement -> tdElement. text() == CONTRIBUTOR } != null
107
+ }
108
+
109
+ def " the table should be rendered to html with negative widths" () {
110
+ given :
111
+ asciidoctor. javaExtensionRegistry(). blockMacro(BLOCKMACRO_NAME , GithubContributorsBlockMacro )
112
+ File resultFile = tmp. newFile(' resultWithNegativeWidth.html' )
113
+
114
+ when :
115
+ def options = Options . builder()
116
+ .safe(SafeMode . SAFE )
117
+ .inPlace(false )
118
+ .baseDir(tmp. getRoot())
119
+ .toFile(resultFile)
120
+ .build()
121
+ asciidoctor. convert(DOCUMENT_WITH_NEGATIVE_WIDTHS , options)
122
+
123
+ then :
124
+ Document htmlDocument = Jsoup . parse(resultFile, UTF_8 . name())
125
+
126
+ Elements cols = htmlDocument. select(COL )
127
+ cols. size() == THREE
128
+ cols. get(0 ). attr(STYLE ). contains(WIDTH_2 ) || cols. get(0 ). attr(WIDTH ). equals(WIDTH_2 )
129
+ cols. get(1 ). attr(STYLE ). contains(WIDTH_3 ) || cols. get(1 ). attr(WIDTH ). equals(WIDTH_3 )
130
+ cols. get(2 ). attr(STYLE ). length() == 0 && cols. get(2 ). attr(WIDTH ). length() == 0
64
131
65
- htmlDocument. select(' table ' ). hasClass(' grid-rows ' )
132
+ htmlDocument. select(CSS_QUERY_TABLE ). hasClass(CLASS_GRID_ROWS )
66
133
67
134
htmlDocument. select(FIRST_TD ). every { tdElement -> tdElement. select(IMG_ELEMENT ). size() == 1 }
68
- htmlDocument. select(FIRST_TD ). every { tdElement -> tdElement. select(IMG_ELEMENT )[0 ]. attr(' src ' ) =~ AVATAR_URL_REGEXP }
135
+ htmlDocument. select(FIRST_TD ). every { tdElement -> tdElement. select(IMG_ELEMENT )[0 ]. attr(ATTR_SRC ) =~ AVATAR_URL_REGEXP }
69
136
70
137
htmlDocument. select(SECOND_TD ). size() == htmlDocument. select(SECOND_TD ) != 0
71
138
htmlDocument. select(SECOND_TD ). size() != 0
72
139
73
- htmlDocument. select(SECOND_TD ). every { tdElement -> tdElement. hasClass(' halign-left ' ) }
74
- htmlDocument. select(THIRD_TD ). every { tdElement -> tdElement. hasClass(' halign-center ' ) }
140
+ htmlDocument. select(SECOND_TD ). every { tdElement -> tdElement. hasClass(CLASS_HALIGN_LEFT ) }
141
+ htmlDocument. select(THIRD_TD ). every { tdElement -> tdElement. hasClass(CLASS_HALIGN_CENTER ) }
75
142
76
143
htmlDocument. select(SECOND_TD ). find { tdElement -> tdElement. text() == CONTRIBUTOR } != null
77
144
}
0 commit comments