Skip to content

Keeping elements with empty content #212

Closed
@jakeg

Description

@jakeg

Using: [email protected] via nodejs

Say you have this in your input:

<script src=""//path/to.js"></script>
<iframe src="//path/to.html"></iframe>

You would expect that turndownService.keep(['iframe', 'script']) would keep these tags in the output, but it doesn't, because the nodes have no content in them. We could add some content, but I really don't want to.

Ok, so we can use blankReplacement to surely fix this:

const turndownService = new TurndownService({
  blankReplacement (content, node) {
    if (['SCRIPT', 'IFRAME'].indexOf(node.nodeName) !== -1) {
      return `\n\n${node.outerHTML}\n\n`
    } else {
      return node.isBlock ? '\n\n' : ''
    }
  }
})

Great, it works! Well, not quite...

<script src=""//path/to.js"></script>
<!-- for whatever reason, the iframe is now nested in another element, which is otherwise empty -->
<div><iframe src="//path/to.html"></iframe></div>

Here we have a <div> around the iframe, but nothing else in that div, so that iframe is left out of the output. I'm guessing when parsing the outer div, it sees it only has an iframe in it, which is empty, so it counts the div as empty, never giving the iframe a chance to appear.

I could continue by changing my blankReplacement function, but I feel this is a bug, or at least something which others will want to have as well, so could be added as an option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions