Skip to content

Commit

Permalink
kepub: Also skip svg and math tags when adding spans
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Oct 2, 2020
1 parent ea356a3 commit 874c34d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kepub/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func transform2koboSpans(doc *html.Node) {
cur.Parent.RemoveChild(cur)

fallthrough
case atom.Script, atom.Style, atom.Pre, atom.Audio, atom.Video:
case atom.Script, atom.Style, atom.Pre, atom.Audio, atom.Video, atom.Svg, atom.Math:
continue // don't add spans to elements which should keep text as-is
case atom.P, atom.Ol, atom.Ul, atom.Table, atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6:
incParaNext = true // increment it only if it will have spans in it
Expand Down
8 changes: 8 additions & 0 deletions kepub/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ func TestTransformContentParts(t *testing.T) {
Out: `<p><span class="koboSpan" id="kobo.1.1">One.</span></p><p><span class="koboSpan" id="kobo.2.1"> </span></p><p><!-- comment --></p><p><span class="koboSpan" id="kobo.3.1">Two.</span></p><p><b><span class="koboSpan" id="kobo.4.1">Three.</span></b></p>`,
}.Run(t)

transformContentCase{
Func: transform2koboSpans,
What: "don't add spans to svg and math elements",
Fragment: true,
In: `<svg xmlns="http://www.w3.org/2000/svg"><g><text font-size="24" y="20" x="0">kepubify</text></g></svg><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></math>`,
Out: `<svg xmlns="http://www.w3.org/2000/svg"><g><text font-size="24" y="20" x="0">kepubify</text></g></svg><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></math>`,
}.Run(t)

// The following cases were found after using kobotest on a bunch of files (the previous cases are also based on kepubs, but I did them manually and didn't keep track):

transformContentCase{
Expand Down

0 comments on commit 874c34d

Please sign in to comment.