2011-05-10 94 views
6

我試圖修補Drupal Panels模塊來修復this nasty issue,這使得AJAX錯誤處理程序完全無法讀取。錯誤處理程序有這樣的:Javascript RegEx替換"

// Replace all &lt; and &gt; by <and> 
    error_text = error_text.replace("/&(lt|gt);/g", function (m, p) { 
    return (p == "lt")? "<" : ">"; 
    }); 

我已經試過兩種方法:

添加

// Now replace all &quot with ' for readability, for goodness sakes, per 
    // http://drupal.org/node/1124042 
    error_text = error_text.replace(/&quot;/g, "'"); 

修改

// Replace all &lt; and &gt; by <and> 
    error_text = error_text.replace("/&(lt|gt|quot);/g", function (m, p) { 
    return (p == "lt")? "<" : (p == "gt") ? ">" : "'"; 
    }); 

然而,無論工作過。幫助不大?

編輯:當打印到控制檯(在FF4或Chrome中)HTML實體不顯示。產生的警報( 「」),然而,看起來像這樣:

An error occurred at /home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center. 

Error Description: [ { &quot;command&quot;: &quot;settings&quot;, &quot;argument&quot;: { &quot;basePath&quot;: &quot;/home/&quot;, &quot;admin_menu&quot;: { &quot;margin_top&quot;: 1 }, &quot;asu_webauth&quot;: { &quot;callapp&quot;: 
>&quot;home&quot;, quot;onLogoutURL&quot;: &quot;http://physics-dev.asu.edu&quot; } } }, { &quot;command&quot;: &quot;css_files&quot;, &quot;argument&quot;: [ { &quot;file&quot;: &quot;/home/modules/node/node.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/defaults.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/system.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/system/system-menus.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/modules/user/user.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/admin_menu/admin_menu.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/cck/theme/content-module.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/ctools/css/ctools.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/date/date.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/filefield/filefield.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/office_hours/office_hours.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/og/theme/og.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/panels/css/panels.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/misc/farbtastic/farbtastic.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/calendar/calendar.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/cck/modules/fieldgroup/fieldgroup.css?E&quot;, &quot;media&quot;: &quot;all&quot; }, { &quot;file&quot;: &quot;/home/sites/all/modules/views/css/views.css?E&quot;, &quot;media&quot;: &quot;all&quot; } ] }, { &quot;command&quot;: &quot;scripts&quot;, &quot;argument&quot;: [ &quot;/home/misc/jquery.js&quot;, &quot;/home/misc/drupal.js&quot;, &quot;/home/sites/all/modules/admin_menu/admin_menu.js&quot;, &quot;/home/sites/all/modules/og/og.js&quot;, &quot;/home/sites/all/modules/panels/js/panels.js&quot;, &quot;/home/sites/all/modules/views_slideshow/js/jquery.cycle.all.min.js&quot;, &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.js&quot;, &quot;/home/sites/all/modules/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.js&quot;, &quot;/home/sites/all/modules/asu_webauth/asu_webauth.js&quot; ] }, { &quot;command&quot;: &quot;modal_display&quot;, &quot;title&quot;: &quot;Add content to Center&quot;, &quot;output&quot;: &quot;\x3cdiv class=\&quot;panels-add-content-modal\&quot;\x3e\x3cdiv class=\&quot;panels-section-column panels-section-column-categories\&quot;\x3e\x3cdiv class=\&quot;inside\&quot;\x3e\x3cdiv class=\&quot;panels-categories-box\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/activity\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eActivity\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/menus\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eMenus\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/miscellaneous\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eMiscellaneous\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/page-elements\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3ePage elements\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/view-panes\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eView panes\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/views\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eViews\x3c/a\x3e\x3ca href=\&quot;/home/panels/ajax/editor/select-content/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/widgets\&quot; class=\&quot;ctools-use-ajax panels-modal-add-category\&quot; title=\&quot;\&quot;\x3eWidgets\x3c/a\x3e\x3c/div\x3e\x3cdiv class=\&quot;content-type-button clear-block\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Add a node from your site as content.\&quot;\x3e\x3cimg src=\&quot;/home/sites/all/modules/ctools/plugins/content_types/node/icon_node.png\&quot; alt=\&quot;\&quot; title=\&quot;\&quot; width=\&quot;16\&quot; height=\&quot;16\&quot; /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/node/node\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Add a node from your site as content.\&quot;\x3eExisting node\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\&quot;content-type-button clear-block\&quot;\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Create a completely custom piece of HTML content.\&quot;\x3e\x3cimg src=\&quot;/home/sites/all/modules/ctools/images/no-icon.png\&quot; alt=\&quot;\&quot; title=\&quot;\&quot; width=\&quot;16\&quot; height=\&quot;16\&quot; /\x3e\x3c/a\x3e\x3cdiv\x3e\x3ca href=\&quot;/home/panels/ajax/editor/add-pane/panel_context%3Apage-faculty_page%3Apage_faculty_page_panel_context/center/custom/custom\&quot; class=\&quot;ctools-use-ajax panels-modal-add-config\&quot; title=\&quot;Create a completely custom piece of HTML content.\&quot;\x3eNew custom content\x3c/a\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e\x3cdiv class=\&quot;panels-categories-description\&quot;\x3eContent options are divided by category. Please select a category from the left to proceed.\x3c/div\x3e\x3c/div\x3e&quot; } ] 
+2

正確,''"應該翻譯爲'「'。 ('''會是''') – 2011-05-10 23:26:42

+0

你使用的是什麼drupal版本? – 2011-05-11 00:08:19

+0

6.20與面板6.x-3.9和CTools 6.x-1.8。我認爲更深層次的問題可能是其他問題,因爲我可以在之前添加面板,然後「發生了什麼」,現在每當我在頁面管理器中單擊「添加內容」時,都會出現此類錯誤。也就是說,我仍然不喜歡CTools錯誤處理程序不會「漂亮地打印」用戶的錯誤信息(甚至不會將它寫出來給控制檯,直到我剛剛添加它),所以我試圖將其作爲服務來執行此操作。 – 2011-05-11 00:18:28

回答

8

刪除正則表達式的引號

error_text = error_text.replace(/&(lt|gt|quot);/g, function (m, p) { 
    return (p == "lt")? "<" : (p == "gt") ? ">" : "'"; 
    }); 

演示http://jsfiddle.net/gaby/mfmkc/

+0

用JSFiddle幹得好。這應該工作,但出於某種原因不適用。輸出保持如附圖所示的截圖所示。 – 2011-05-10 23:43:43

+0

@Visionary您是否驗證過錯誤是由您正在修補的功能提醒的? (*添加一個額外的警報將證明*)。也可以在提醒它之前嘗試添加'console.log(error_text)'並查看在螢火蟲中打印的內容。 – 2011-05-10 23:54:11

+0

事實上,線條上方的警報顯示已達到。我假設控制檯會自動處理html實體,因爲它可以在那裏打印出來,但不在提醒中。 – 2011-05-11 00:15:24

1

這應該工作:

var str = '&lt;span&gt;&quot;error&quot;&lt;/span&gt;'; 
str.replace(/&(lt|gt|quot);/g, function(m, t) { 
    switch (t) { 
    case 'lt': 
    return '<'; 
    case 'gt': 
    return '>'; 
    case 'quot': 
    return '\''; 
    } 
}); 

如果不工作,那麼也許你原來的字符串包含它返回"<span>'error'</span>"

&amp;lt;而不是&lt;等...

+0

如果聲明是問題,我懷疑三元論,特別是考慮下面的證明正確的解決方案。 – 2011-05-10 23:41:33

+0

它解釋了爲什麼第二種解決方案不起作用(將正則表達式文字用引號括起來),但仍然不確定爲什麼你採取的第一種方法不起作用,因爲它爲我工作。 – 2011-05-10 23:59:44

+0

看來你的代碼有錯字。函數參數列表中的順序參數應顛倒過來:函數(t,m)。 – 2013-12-01 06:42:04

10
str.replace(/&(l|g|quo)t;/g, function(a,b){ 
    return { 
     l : '<', 
     g : '>', 
     quo : '"' 
    }[b]; 
}) 
+1

這就像代碼混淆的奇蹟。爲了節省1個字符的比較(因爲t在GT中是常見的),我們設法使正則表達式更容易混淆500%。不錯的演出。 – 2011-05-10 23:53:48

+0

謝謝你,那是我的目標! :)如果你拿出'?:',如果你把't'從比賽中拿出來,然後讓'{':'',...',[編輯],好吧,只是做了這些變化 – 2011-05-11 00:02:53

+0

+1超級清除屬性查找,而不是條件(或三元,或大小寫),但我同意,較長的正則表達式更具可讀性。如果你願意在正則表達式中修改角色,你是否應該更關心匿名對象構造上的閉包? – maerics 2011-05-11 00:49:12