用朋友姓名中的单引号解决错误 [英] Solving bug with single quotes in names of friends

查看:137
本文介绍了用朋友姓名中的单引号解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JavaScript应用程序使用Facebook Connect显示用户的朋友的个人资料照片。



这样工作正常,直到Facebook最近的更新在Safari中创建了一个错误。



基本上,一切都正常,除非用户的朋友在其名称中有撇号(单引号)。以爱尔兰人的名字为例,像O'Connor。撇号完全打破了所有个人资料图片的渲染,只显示剪影。



通常在渲染fb:profile-pics时,朋友的名字将成为图像的alt和标题属性。但是,使用带撇号的名称,则会发生改变(使用名称Gregory O'Connor为此示例):

  ; fb:profile-pic class =FB_profile_pic_rendered FB_ElementReady> 
<! - 注意奇怪的新connor属性 - >
< img class =FB_profile_pic fb_profile_pic_renderedstyle =connor =title =Gregory Oalt =Gregory Osrc =http://profile.ak.fbcdn.net/path/到/朋友/简档/ image.jpg的>
< fb:profile-pic>幸运的是,我的代码与Facebook给出的代码一样,是FB的一个例子。 ApiClient.friends_get函数,可以在这里找到(如果一个朋友在他们的名字中有一个撇号,将会重现Safari的错误)。



From: http://developers.facebook.com/docs/?u=facebook.jslib .FB.ApiClient.friends_get

 < div id =profile_pics>< / div> 
< script type =text / javascript>
var widget_div = document.getElementById(profile_pics);
FB.ensureInit(function(){
FB.Facebook.apiClient.friends_get(null,function(result){
var markup =;
var num_friends = result? Math.min(10,result.length):0;
if(num_friends> 0){
for(var i = 0; i< num_friends; i ++){
markup +
'< fb:profile-pic size =squareuid ='
+ result [i]
+'facebook-logo =true>'
+'< / fb:profile-pic>';
}
}
widget_div.innerHTML = markup;
FB.XFBML.Host.parseDomElement(widget_div);
});
});
< / script>

由于Facebook对此错误一无所知,我需要提出一个解决方案。我开始试图剥离撇号(由于Facebook和我们的应用程序之间的沟通和抽象层,丑陋很快),并决定退出并就如何解决这个问题寻求建议。



非常感谢提前。



修改


$ b $我想通过给Facebook的呼叫生成的HTML的一个现实世界的例子来澄清。基本上,上述JavaScript将从上面的结果中获取用户ID,并从该结果生成图像的HTML。



我相信从img alt中转义结果和img标题将修复这个错误,但是如何访问它的问题仍然存在于我。



示例:

 < fb:profile-pic class =FB_profile_pic fb_profile_pic_rendered FB_ElementReadyfacebook-logo =falselinked =falseuid =6841956size =normal > 
< img class =FB_profile_pic fb_profile_pic_renderedstyle =opacity:1; connor =title =Colin Oalt =Colin Osrc =http://profile.ak.fbcdn.net/v227/676/36/n6841956_271.jpg>
< / fb:profile-pic>

再次感谢

解决方案

与F3的答案相关,而不是尝试对整个结果进行编码(有关差异,请参阅以下链接: http://xkr.us/articles/javascript/encode-compare/ )你不能只编码(或删除)单引号吗?

 result [i] .replace(/'/ g,%27)

正则表达式模式应该匹配'character

/'[^'] * +'/

$ b $的所有实例b

像罗伯特一样,如果你能提供一个返回的例子,那么它将帮助一大堆。


My JavaScript application uses Facebook Connect to display the profile pictures of a user's friends.

This worked fine until a recent update on Facebook's end created a bug in Safari.

Essentially, everything works fine unless a user's friend has an apostrophe (single quote) in their name. Think Irish last names like O'Connor, for example. An apostrophe completely breaks the rendering of all profile pictures, and only silhouettes are displayed instead.

Usually when rendering the fb:profile-pics, the friend's name becomes the value of the image's alt and title attributes. However, with a name with an apostrophe, this happens instead (using the name Gregory O'Connor for this example):

<fb:profile-pic class=" FB_profile_pic_rendered FB_ElementReady">
  <!-- Notice the strange new "connor" attribute -->
  <img class=" FB_profile_pic fb_profile_pic_rendered " style="" connor="" title="Gregory O" alt="Gregory O" src="http://profile.ak.fbcdn.net/path/to/friends/profile/image.jpg">
<fb:profile-pic>

Luckily for me, my code is almost identical to the code Facebook gives for an example of the FB.ApiClient.friends_get function, which can be found here (and will reproduce the Safari bug if a friend has an apostrophe in their name).

From: http://developers.facebook.com/docs/?u=facebook.jslib.FB.ApiClient.friends_get

 <div id="profile_pics"></div>
 <script type="text/javascript">
   var widget_div = document.getElementById("profile_pics");
   FB.ensureInit(function () {
     FB.Facebook.apiClient.friends_get(null, function(result) {
       var markup = "";
       var num_friends = result ? Math.min(10, result.length) : 0;
       if (num_friends > 0) {
         for (var i=0; i<num_friends; i++) {
           markup +=
             '<fb:profile-pic size="square" uid="'
             + result[i]
             + '" facebook-logo="true">'
             + ' </fb:profile-pic>';
          }
        }
        widget_div.innerHTML = markup;
        FB.XFBML.Host.parseDomElement(widget_div);
      });
    });
  </script>

Since Facebook has been silent on this bug, I need to come up with a fix on our end. I started trying to strip out apostrophes (which got ugly pretty fast due to the layers of communication and abstraction between Facebook and our application), and decided I should step back and ask for advice on how to solve this problem here.

Many thanks in advance.

Edit

I'd like to clarify by giving a real-world example of HTML generated by the call to Facebook. Essentially, the above JavaScript will grab the user ID from the call to result above, and from that result generate HTML for the image.

I believe escaping the result from img alt and img title will fix this bug, but the question of how to get access to it still exists for me.

Example:

<fb:profile-pic class=" FB_profile_pic fb_profile_pic_rendered FB_ElementReady" facebook-logo="false" linked="false" uid="6841956" size="normal">
  <img class=" FB_profile_pic fb_profile_pic_rendered" style="opacity: 1;" connor="" title="Colin O" alt="Colin O" src="http://profile.ak.fbcdn.net/v227/676/36/n6841956_271.jpg">
 </fb:profile-pic>

Thanks again!

解决方案

Related to the answer by F3, but rather than attempting to encode the entire result (see the following link for differences: http://xkr.us/articles/javascript/encode-compare/) can you not just encode (or remove) the single quotes?

result[i].replace(/'/g,"%27")

Alternately, this regex pattern should match all instances of the ' character

"/'[^']*+'/"

Like Robert, if you could provide an example of what is returned it would help a bunch.

这篇关于用朋友姓名中的单引号解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆