Skip to content

Commit

Permalink
Traverse inline-block elements too
Browse files Browse the repository at this point in the history
Fixes #535.
  • Loading branch information
birtles committed Feb 26, 2021
1 parent 2811de8 commit 4b21b49
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_
_
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.3.4 (not released yet)

(Nothing yet)
- Made rikaichamp traverse text in `inline-block` elements so that it can
ready YouTube subtitles with ruby
([#535](https://github.com/birtles/rikaichamp/issues/535))

## 0.3.3 (2020-12-09)

Expand Down
5 changes: 4 additions & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,11 @@ export class RikaiContent {
// We always treat <rb> and <ruby> tags as inline regardless of the
// styling since sites like renshuu.org do faux-ruby styling where they
// give these elements styles like 'display: table-row-group'.
//
// Furthermore, we treat inline-block as inline because YouTube puts
// okurigana in a separate inline-block span when using ruby it seems.
(['RB', 'RUBY'].includes(element.tagName) ||
['inline', 'ruby', 'ruby-base', 'ruby-text'].includes(
['inline', 'inline-block', 'ruby', 'ruby-base', 'ruby-text'].includes(
getComputedStyle(element).display!
));

Expand Down
29 changes: 29 additions & 0 deletions tests/content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,35 @@ describe('rikaiContent:text search', () => {
assertTextResultEqual(result, 'せん', seNode, 0, seNode, 1, nNode, 1);
});

it('should traverse okurigana in inline-block elements too', () => {
// YouTube annotates okurigana inline-block spans.
//
// See https://github.com/birtles/rikaichamp/issues/535
testDiv.innerHTML =
'<p><ruby><span>疲</span><rt>つか</rt></ruby><span style="display: inline-block">れた</span></p>';

const kanjiNode = testDiv.firstChild!.firstChild!.firstChild!
.firstChild as Text;
const okuriganaNode = testDiv.firstChild!.childNodes[1].firstChild as Text;
const bbox = getBboxForOffset(kanjiNode, 0);

const result = subject.getTextAtPoint({
x: bbox.left,
y: bbox.top + bbox.height / 2,
});

assertTextResultEqual(
result,
'疲れた',
kanjiNode,
0,
kanjiNode,
1,
okuriganaNode,
2
);
});

it('should find text in SVG content', () => {
testDiv.innerHTML = '<svg><text y="1em">あいうえお</text></svg>';
const textNode = testDiv.firstChild!.firstChild!.firstChild as Text;
Expand Down

0 comments on commit 4b21b49

Please sign in to comment.