WordPress/wp-includes/js/tinymce/plugins/wplink/plugin.min.js
Andrew Ozz 04319a7898 TinyMCE, link check:
- Use `wp.a11y.speak()` to announce bad URLs.
- Do not add a title to the link toolbar.
- Better error message.

Props afercia, azaozz.
See #36638.
Built from https://develop.svn.wordpress.org/trunk@38126


git-svn-id: http://core.svn.wordpress.org/trunk@38067 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-07-21 03:42:34 +00:00

1 line
8.9 KiB
JavaScript

!function(a){a.ui.WPLinkPreview=a.ui.Control.extend({url:"#",renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-preview"><a href="'+this.url+'" target="_blank" tabindex="-1">'+this.url+"</a></div>"},setURL:function(b){var c,d;this.url!==b&&(this.url=b,b=window.decodeURIComponent(b),b=b.replace(/^(?:https?:)?\/\/(?:www\.)?/,""),-1!==(c=b.indexOf("?"))&&(b=b.slice(0,c)),-1!==(c=b.indexOf("#"))&&(b=b.slice(0,c)),b=b.replace(/(?:index)?\.html$/,""),"/"===b.charAt(b.length-1)&&(b=b.slice(0,-1)),""===b&&(b=this.url),b.length>40&&-1!==(c=b.indexOf("/"))&&-1!==(d=b.lastIndexOf("/"))&&d!==c&&(c+b.length-d<40&&(d=-(40-(c+1))),b=b.slice(0,c+1)+"\u2026"+b.slice(d)),a.$(this.getEl().firstChild).attr("href",this.url).text(b))}}),a.ui.WPLinkInput=a.ui.Control.extend({renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-input"><input type="text" value="" placeholder="'+a.translate("Paste URL or type to search")+'" /><input type="text" style="display:none" value="" /></div>'},setURL:function(a){this.getEl().firstChild.value=a},getURL:function(){return a.trim(this.getEl().firstChild.value)},getLinkText:function(){var b=this.getEl().firstChild.nextSibling.value;return a.trim(b)?b.replace(/[\r\n\t ]+/g," "):""},reset:function(){var a=this.getEl().firstChild;a.value="",a.nextSibling.value=""}}),a.PluginManager.add("wplink",function(b){function c(){var a,c,d=b.selection.getNode(),e=b.dom.getParent(d,"a[href]");return e||(c=b.selection.getContent({format:"raw"}),c&&-1!==c.indexOf("</a>")&&(a=c.match(/href="([^">]+)"/),a&&a[1]&&(e=b.$('a[href="'+a[1]+'"]',d)[0]),e&&b.selection.select(e))),e}function d(){b.$("a").each(function(a,c){var d=b.$(c);"_wp_link_placeholder"===d.attr("href")?b.dom.remove(c,!0):d.attr("data-wplink-edit")&&d.attr("data-wplink-edit",null)})}function e(a,b){return a.replace(/(<a [^>]+>)([\s\S]*?)<\/a>/g,function(a,c,d){return c.indexOf(' href="_wp_link_placeholder"')>-1?d:(b&&(c=c.replace(/ data-wplink-edit="true"/g,"")),c=c.replace(/ data-wplink-url-error="true"/g,""),c+d+"</a>")})}function f(a){s=!0,a.attr("data-wplink-url-error","true"),r(b.translate("Warning: the link has been inserted but the destination cannot be reached."),"assertive"),h&&h.visible()&&h.$el.find(".wp-link-preview a").addClass("wplink-url-error")}function g(a){var c=b.$(a),d=c.attr("href");if(d&&"undefined"!=typeof o){if(s=!1,/^http/i.test(d)&&!/^https?:\/\/[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}(\/|$)/i.test(d)&&(p[d]=!0),p.hasOwnProperty(d))return void f(c);c.removeAttr("data-wplink-url-error"),o.post(window.ajaxurl,{action:"test_url",nonce:o("#_wplink_urltest_nonce").val(),href:d},"json").done(function(a){a.success||a.data&&a.data.httpError&&(p[d]=!0,f(c))})}}var h,i,j,k,l,m,n,o=window.jQuery,p={},q=/^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i,r="undefined"!=typeof window.wp&&window.wp.a11y&&window.wp.a11y.speak?window.wp.a11y.speak:function(){},s=!1;return b.on("preinit",function(){if(b.wp&&b.wp._createToolbar){h=b.wp._createToolbar(["wp_link_preview","wp_link_edit","wp_link_remove"],!0);var a=["wp_link_input","wp_link_apply"];"undefined"!=typeof window.wpLink&&a.push("wp_link_advanced"),i=b.wp._createToolbar(a,!0),i.on("show",function(){"undefined"!=typeof window.wpLink&&window.wpLink.modalOpen||window.setTimeout(function(){var a=i.$el.find("input.ui-autocomplete-input")[0],b=l&&(l.textContent||l.innerText);a&&(!a.value&&b&&"undefined"!=typeof window.wpLink&&(a.value=window.wpLink.getUrlFromSelection(b)),m||(a.focus(),a.select()))})}),i.on("hide",function(){i.scrolling||b.execCommand("wp_link_cancel")})}}),b.addCommand("WP_Link",function(){return a.Env.ie&&a.Env.ie<10&&"undefined"!=typeof window.wpLink?void window.wpLink.open(b.id):(l=c(),i.tempHide=!1,void(l?b.dom.setAttribs(l,{"data-wplink-edit":!0}):(d(),b.execCommand("mceInsertLink",!1,{href:"_wp_link_placeholder"}),l=b.$('a[href="_wp_link_placeholder"]')[0],b.nodeChanged())))}),b.addCommand("wp_link_apply",function(){if(!i.scrolling){var c,d;if(l){if(c=k.getURL(),d=k.getLinkText(),b.focus(),!c)return void b.dom.remove(l,!0);/^(?:[a-z]+:|#|\?|\.|\/)/.test(c)||q.test(c)||(c="http://"+c),b.dom.setAttribs(l,{href:c,"data-wplink-edit":null}),a.trim(l.innerHTML)||b.$(l).text(d||c),g(l)}k.reset(),b.nodeChanged(),"undefined"==typeof window.wpLinkL10n||s||r(window.wpLinkL10n.linkInserted)}}),b.addCommand("wp_link_cancel",function(){i.tempHide||(k.reset(),d())}),b.addCommand("wp_unlink",function(){b.execCommand("unlink"),i.tempHide=!1,b.execCommand("wp_link_cancel")}),b.addShortcut("access+a","","WP_Link"),b.addShortcut("access+s","","wp_unlink"),b.addShortcut("meta+k","","WP_Link"),b.addButton("link",{icon:"link",tooltip:"Insert/edit link",cmd:"WP_Link",stateSelector:"a[href]"}),b.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink"}),b.addMenuItem("link",{icon:"link",text:"Insert/edit link",cmd:"WP_Link",stateSelector:"a[href]",context:"insert",prependToContext:!0}),b.on("pastepreprocess",function(c){var d=c.content,e=/^(?:https?:)?\/\/\S+$/i;b.selection.isCollapsed()||e.test(b.selection.getContent())||(d=d.replace(/<[^>]+>/g,""),d=a.trim(d),e.test(d)&&(b.execCommand("mceInsertLink",!1,{href:b.dom.decode(d)}),c.preventDefault()))}),b.on("savecontent",function(a){a.content=e(a.content,!0)}),b.on("BeforeAddUndo",function(a){a.lastLevel&&a.lastLevel.content&&a.level.content&&a.lastLevel.content===e(a.level.content)&&a.preventDefault()}),b.on("keydown",function(c){27===c.keyCode&&b.execCommand("wp_link_cancel"),c.altKey||a.Env.mac&&(!c.metaKey||c.ctrlKey)||!a.Env.mac&&!c.ctrlKey||89!==c.keyCode&&90!==c.keyCode||(m=!0,window.clearTimeout(n),n=window.setTimeout(function(){m=!1},500))}),b.addButton("wp_link_preview",{type:"WPLinkPreview",onPostRender:function(){j=this}}),b.addButton("wp_link_input",{type:"WPLinkInput",onPostRender:function(){var c,d,e,f=this.getEl(),g=f.firstChild;k=this,o&&o.ui&&o.ui.autocomplete&&(c=o(g),c.on("keydown",function(){c.removeAttr("aria-activedescendant")}).autocomplete({source:function(a,b){return e===a.term?void b(d):/^https?:/.test(a.term)||-1!==a.term.indexOf(".")?b():(o.post(window.ajaxurl,{action:"wp-link-ajax",page:1,search:a.term,_ajax_linking_nonce:o("#_ajax_linking_nonce").val()},function(a){d=a,b(a)},"json"),void(e=a.term))},focus:function(a,b){c.attr("aria-activedescendant","mce-wp-autocomplete-"+b.item.ID),a.preventDefault()},select:function(a,b){return c.val(b.item.permalink),o(f.firstChild.nextSibling).val(b.item.title),9===a.keyCode&&"undefined"!=typeof window.wpLinkL10n&&r(window.wpLinkL10n.linkSelected),!1},open:function(){c.attr("aria-expanded","true"),i.blockHide=!0},close:function(){c.attr("aria-expanded","false"),i.blockHide=!1},minLength:2,position:{my:"left top+2"},messages:{noResults:"undefined"!=typeof window.uiAutocompleteL10n?window.uiAutocompleteL10n.noResults:"",results:function(a){return"undefined"!=typeof window.uiAutocompleteL10n?a>1?window.uiAutocompleteL10n.manyResults.replace("%d",a):window.uiAutocompleteL10n.oneResult:void 0}}}).autocomplete("instance")._renderItem=function(a,b){return o('<li role="option" id="mce-wp-autocomplete-'+b.ID+'">').append("<span>"+b.title+'</span>&nbsp;<span class="wp-editor-float-right">'+b.info+"</span>").appendTo(a)},c.attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":c.autocomplete("widget").attr("id")}).on("focus",function(){var a=c.val();a&&!/^https?:/.test(a)&&c.autocomplete("search")}).autocomplete("widget").addClass("wplink-autocomplete").attr("role","listbox").removeAttr("tabindex").on("menufocus",function(a,b){b.item.attr("aria-selected","true")}).on("menublur",function(){o(this).find('[aria-selected="true"]').removeAttr("aria-selected")})),a.$(g).on("keydown",function(a){13===a.keyCode&&(b.execCommand("wp_link_apply"),a.preventDefault())})}}),b.on("wptoolbar",function(a){var c,d,e,f=b.dom.getParent(a.element,"a");return"undefined"!=typeof window.wpLink&&window.wpLink.modalOpen?void(i.tempHide=!0):(i.tempHide=!1,void(f?(c=b.$(f),d=c.attr("href"),e=c.attr("data-wplink-edit"),"_wp_link_placeholder"===d||e?("_wp_link_placeholder"===d||k.getURL()||k.setURL(d),a.element=f,a.toolbar=i):d&&!c.find("img").length&&(j.setURL(d),a.element=f,a.toolbar=h,"true"===c.attr("data-wplink-url-error")?h.$el.find(".wp-link-preview a").addClass("wplink-url-error"):(h.$el.find(".wp-link-preview a").removeClass("wplink-url-error"),s=!1))):i.visible()&&b.execCommand("wp_link_cancel")))}),b.addButton("wp_link_edit",{tooltip:"Edit ",icon:"dashicon dashicons-edit",cmd:"WP_Link"}),b.addButton("wp_link_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",cmd:"wp_unlink"}),b.addButton("wp_link_advanced",{tooltip:"Link options",icon:"dashicon dashicons-admin-generic",onclick:function(){if("undefined"!=typeof window.wpLink){var c=k.getURL()||null,d=k.getLinkText()||null;a.Env.ie&&b.focus(),window.wpLink.open(b.id,c,d,l),i.tempHide=!0,k.reset()}}}),b.addButton("wp_link_apply",{tooltip:"Apply",icon:"dashicon dashicons-editor-break",cmd:"wp_link_apply",classes:"widget btn primary"}),{close:function(){i.tempHide=!1,b.execCommand("wp_link_cancel")},checkLink:g}})}(window.tinymce);