weixin_39969143
weixin_39969143
2020-12-01 22:48

Bleach linkify vs oembed content.

Hey,

I tried to use bleach.linkify with oembed text parsing tool and the problem is that it will make some mess and make it impossible to embed content :)


def get_description(self):
    providers = micawber.bootstrap_basic()
    description = bleach.clean(self.description)
    description = micawber.parse_text(description, providers)
    description = bleach.linkify(description)

Any ideas how to get around this problem?

该提问来源于开源项目:mozilla/bleach

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39575775 weixin_39575775 5月前

    Do you have any examples of the input/output and how it differs from what you expect?

    点赞 评论 复制链接分享
  • weixin_39969143 weixin_39969143 5月前

    my input:

    
      https://www.youtube.com/watch?v=6qNW7yRsqAM
    

    expected output:

    width="480" height="270" frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/6qNW7yRsqAM?feature=oembed"> /\ there are iframe tags but I removed them because github does not display them.

    example result result for:

    
    def get_description(self):
        providers = micawber.bootstrap_basic()
        description = bleach.clean(self.description)
        description = micawber.parse_text(description, providers)
        description = bleach.linkify(description)
    

    <iframe width="480" height="270" src="&lt;a href=" http:="" <a="" href="http://www.youtube.com" rel="nofollow">www.youtube.com="" embed="" 6qNW7yRsqAM?feature="oembed"" rel="nofollow">http://www.youtube.com/embed/6qNW7yRsqAM?feature=oembed" frameborder="0" allowfullscreen=""></iframe>

    点赞 评论 复制链接分享
  • weixin_39575775 weixin_39575775 5月前

    You can put in HTML by wrapping it in a literal block (e.g. start with one line that's <code>html</code> and end with <code>) Or put the raw, literal results in a gist. It's very hard to understand what's going on without actually seeing the values verbatim.

    点赞 评论 复制链接分享
  • weixin_39969143 weixin_39969143 5月前

    I hope that this will be ok :) https://gist.github.com/pypetey/089332e8bed73890e20f

    点赞 评论 复制链接分享
  • weixin_39808877 weixin_39808877 5月前

    Hi. Did this ever get resolved? Running into the same issue using bleach.linkify()

    This is the original HTML

    
    <div data-oembed-url="https://www.youtube.com/watch?v=ZZBFGz-wgrw">
    <div>
    <div style="left: 0px; width: 100%; height: 0px; position: relative; padding-bottom: 56.2493%;"><iframe allowfullscreen="" data-iframely-url="https://www.youtube.com/embed/ZZBFGz-wgrw?wmode=transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1" frameborder="0" style="top: 0px; left: 0px; width: 100%; height: 100%; position: absolute;"></iframe></div>
    </div>
    <script async="" charset="utf-8" src="//cdn.iframe.ly/embed.js"></script></div>
    

    which gets converted to

    
    <div>
    <div>
    
    <script async="" charset="utf-8" src="//&lt;a href=" http:="" <a="" href="http://cdn.iframe.ly" target="_blank" rel="nofollow">cdn.iframe.ly="" embed.js"="" target="_blank" rel="nofollow"><a href="http://cdn.iframe.ly/embed.js" rel="nofollow" target="_blank">cdn.iframe.ly/embed.js</a>"></script><div style="width: 100%; height: 0px;"><iframe allowfullscreen="" data-iframely-url="&lt;a href=" https:="" <a="" href="http://www.youtube.com" target="_blank" rel="nofollow">www.youtube.com="" embed="" ZZBFGz-wgrw?wmode="transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1"" target="_blank" rel="nofollow"><a href="https://www.youtube.com/embed/ZZBFGz-wgrw?wmode=transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1" rel="nofollow" target="_blank">https://www.youtube.com/embed/ZZBFGz-wgrw?wmode=transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1</a>" frameborder="0" style="top: 0px; left: 0px; width: 100%; height: 100%; position: absolute;"></iframe></div>
    </div></div>
    

    Would expect URLs inside attributes to not get linkified. Any thoughts on a workaround?

    Thanks!

    点赞 评论 复制链接分享
  • weixin_39971172 weixin_39971172 5月前

    I don't think I understand this issue very well.

    Can someone provide an input and the output they expected from linkify so that I can build a test case and look into it? Please use Markdown formatting so it's possible for me to parse your comment correctly.

    点赞 评论 复制链接分享
  • weixin_39971172 weixin_39971172 5月前

    No one has said anything in a little under a year, so I'm going to close this out.

    Going through the comments, it seems like the real problem here is that running clean() and then linkify() produces bad output somehow. With Bleach 2.0, you don't need to do those as separate passes, you can do them at the same time.

    More details here:

    https://bleach.readthedocs.io/en/latest/linkify.html#using-bleach-linkifier-linkifyfilter

    Hope that helps!

    点赞 评论 复制链接分享

相关推荐