26 require_once __DIR__.
'/../../common-string.php';
30 const START_OFFSET = 10;
31 const FRAGMENT_TEXT =
"foo bar baz quux";
34 private $textFragment;
36 private $textRenderer;
40 $this->textFragment =
new TextFragment(self::START_OFFSET, self::FRAGMENT_TEXT);
42 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
47 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
51 function testRenderHighlightedTextWithNoSplitPosition()
53 $renderedText = $this->textRenderer->renderText($this->textFragment);
54 assertThat($renderedText, is(self::FRAGMENT_TEXT));
57 function testRenderHighlightOutsideFragment()
59 $highlight1 =
new Highlight(0, 5, Highlight::MATCH, 0, 0,
'ref1');
61 $splitPositions = array(
62 0 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
63 5 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
64 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
66 assertThat($renderedText, is(
"foo bar baz quux"));
69 function testRenderHighlightAtStartOfFragment()
71 $highlight1 =
new Highlight(self::START_OFFSET, self::START_OFFSET + 5, Highlight::MATCH, 0, 0,
'ref1');
73 $splitPositions = array(
74 self::START_OFFSET => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
75 self::START_OFFSET + 5 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
76 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
78 assertThat($renderedText, is(
"<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">foo b</span>ar baz quux"));
81 function testRenderHighlightAtEndOfFragment()
83 $highlight_length = 5;
84 $end_offset = self::START_OFFSET + strlen(self::FRAGMENT_TEXT);
85 $start_offset = $end_offset - $highlight_length;
87 $highlight1 =
new Highlight($start_offset, $end_offset, Highlight::MATCH, 0, 0,
'ref1');
89 $splitPositions = array(
90 $start_offset => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
91 $end_offset => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
92 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
94 assertThat($renderedText, is(
"foo bar baz<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\"> quux</span>"));
97 function testRenderHighlightWithinFragment()
99 $highlight1 =
new Highlight(15, 18, Highlight::MATCH, 0, 0,
'ref1');
101 $splitPositions = array(
102 15 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
103 18 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
104 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
106 assertThat($renderedText, is(
"foo b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar </span>baz quux"));
109 function testRenderHexHighlightWithinFragment()
111 $highlight1 =
new Highlight(15, 18, Highlight::MATCH, 0, 0,
'ref1');
113 $splitPositions = array(
114 15 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
115 18 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
116 $renderedText = $this->textRenderer->renderHex($this->textFragment, $splitPositions);
118 assertThat($renderedText, is(
"0x0000000A |66 6f 6f 20 62 <a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">61 72 20 </span>62 61 7a 20 71 75 75 78| |foo b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar </span>baz quux|<br/>\n"));
121 function testRenderHighlightWithBacklinkWithinFragment()
123 $highlight1 =
new Highlight(15, 18, Highlight::MATCH, 0, 0,
'ref1');
125 $splitPositions = array(
126 15 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
127 18 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
128 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions,
true);
130 assertThat($renderedText, is(
"foo b<a id=\"highlight\" href=\"#top\"> ↑ </a><span class=\"hi-match\" title=\"ref1\">ar </span>baz quux"));
133 function testRenderHighlightedOverlapsStartOfFragment()
135 $highlight1 =
new Highlight(5, 18, Highlight::MATCH, 0, 0,
'ref1');
137 $splitPositions = array(
138 5 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
139 18 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
140 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
142 assertThat($renderedText, is(
"<span class=\"hi-match\" title=\"ref1\">foo bar </span>baz quux"));
145 function testRenderHighlightedOverlapsEndOfFragment()
147 $highlight1 =
new Highlight(15, 28, Highlight::MATCH, 0, 0,
'ref1');
149 $splitPositions = array(
150 15 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
151 28 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
152 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
154 assertThat($renderedText, is(
"foo b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar baz quux</span>"));
157 function testRenderFragmentFullInsideHighlight()
159 $highlight1 =
new Highlight(5, 50, Highlight::MATCH, 0, 0,
'ref1');
161 $splitPositions = array(
162 5 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
163 50 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
164 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
166 assertThat($renderedText, is(
"<span class=\"hi-match\" title=\"ref1\">foo bar baz quux</span>"));
169 function testRenderHighlightedTextWithFourSplitPositions()
171 $highlight1 =
new Highlight(12, 18, Highlight::URL,
'ref1', 0, 0);
172 $highlight2 =
new Highlight(14, 18, Highlight::MATCH,
'ref2', 0, 0);
174 $splitPositions = array(
175 12 => array(
new SplitPosition(1, SplitPosition::START, $highlight1)),
176 14 => array(
new SplitPosition(2, SplitPosition::START, $highlight2)),
177 18 => array(
new SplitPosition(2, SplitPosition::END, $highlight2)),
178 20 => array(
new SplitPosition(1, SplitPosition::END, $highlight1)));
179 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
181 assertThat($renderedText, is(
"fo<a id=\"highlight\"></a><span class=\"hi-url\" title=\"0\">o <span class=\"hi-match\" title=\"0\">bar </span>ba</span>z quux"));
184 function testRenderHighlightThatIsIgnorableByBulk()
186 $highlight1 =
new Highlight(14, 14, Highlight::DELETED, 0, 0,
'ref1');
188 $splitPositions = array(
189 14 => array(
new SplitPosition(1, SplitPosition::ATOM, $highlight1)));
190 $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
191 $pastedText = strip_tags($renderedText);
192 $bulkText = preg_replace(
'/[!#]/',
' ',$pastedText);
193 $cleanText = preg_replace(
'/\s\s+/',
' ',$bulkText);
195 assertThat($cleanText, is(
"foo bar baz quux"));