WordPress/wp-includes/js/tinymce/plugins/wplink/plugin.min.js

1 line
8.7 KiB
JavaScript
Raw Normal View History

!function(h){h.ui.Factory.add("WPLinkPreview",h.ui.Control.extend({url:"#",renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-preview"><a href="'+this.url+'" target="_blank" rel="noopener" tabindex="-1">'+this.url+"</a></div>"},setURL:function(e){var t,n;this.url!==e&&(this.url=e,-1!==(t=(e=(e=window.decodeURIComponent(e)).replace(/^(?:https?:)?\/\/(?:www\.)?/,"")).indexOf("?"))&&(e=e.slice(0,t)),-1!==(t=e.indexOf("#"))&&(e=e.slice(0,t)),"/"===(e=e.replace(/(?:index)?\.html$/,"")).charAt(e.length-1)&&(e=e.slice(0,-1)),""===e&&(e=this.url),40<e.length&&-1!==(t=e.indexOf("/"))&&-1!==(n=e.lastIndexOf("/"))&&n!==t&&(t+e.length-n<40&&(n=-(40-(t+1))),e=e.slice(0,t+1)+"\u2026"+e.slice(n)),h.$(this.getEl().firstChild).attr("href",this.url).text(e))}})),h.ui.Factory.add("WPLinkInput",h.ui.Control.extend({renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-input"><input type="text" value="" placeholder="'+h.translate("Paste URL or type to search")+'" /><input type="text" style="display:none" value="" /></div>'},setURL:function(e){this.getEl().firstChild.value=e},getURL:function(){return h.trim(this.getEl().firstChild.value)},getLinkText:function(){var e=this.getEl().firstChild.nextSibling.value;return h.trim(e)?e.replace(/[\r\n\t ]+/g," "):""},reset:function(){var e=this.getEl().firstChild;e.value="",e.nextSibling.value=""}})),h.PluginManager.add("wplink",function(l){var a,r,d,c,i,n,t,p=window.jQuery,o=/^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i,s=/^https?:\/\/([^\s/?.#-][^\s\/?.#]*\.?)+(\/[^\s"]*)?$/i,u=/^https?:\/\/[^\/]+\.[^\/]+($|\/)/i,w=void 0!==window.wp&&window.wp.a11y&&window.wp.a11y.speak?window.wp.a11y.speak:function(){},m=!1;function e(){l.$("a").each(function(e,t){var n=l.$(t);"_wp_link_placeholder"===n.attr("href")?l.dom.remove(t,!0):n.attr("data-wplink-edit")&&n.attr("data-wplink-edit",null)})}function k(e,i){return e.replace(/(<a [^>]+>)([\s\S]*?)<\/a>/g,function(e,t,n){return-1<t.indexOf(' href="_wp_link_placeholder"')?n:(i&&(t=t.replace(/ data-wplink-edit="true"/g,"")),(t=t.replace(/ data-wplink-url-error="true"/g,""))+n+"</a>")})}function f(e){var t=l.$(e),n=t.attr("href");n&&void 0!==p&&(m=!1,!/^http/i.test(n)||s.test(n)&&u.test(n)?t.removeAttr("data-wplink-url-error"):(m=!0,t.attr("data-wplink-url-error","true"),w(l.translate("Warning: the link has been inserted but may have errors. Please test it."),"assertive")))}return l.on("preinit",function(){if(l.wp&&l.wp._createToolbar){a=l.wp._createToolbar(["wp_link_preview","wp_link_edit","wp_link_remove"],!0);var e=["wp_link_input","wp_link_apply"];void 0!==window.wpLink&&e.push("wp_link_advanced"),(r=l.wp._createToolbar(e,!0)).on("show",function(){void 0!==window.wpLink&&window.wpLink.modalOpen||window.setTimeout(function(){var e=r.$el.find("input.ui-autocomplete-input")[0],t=i&&(i.textContent||i.innerText);e&&(!e.value&&t&&void 0!==window.wpLink&&(e.value=window.wpLink.getUrlFromSelection(t)),n||(e.focus(),e.select()))})}),r.on("hide",function(){r.scrolling||l.execCommand("wp_link_cancel")})}}),l.addCommand("WP_Link",function(){h.Env.ie&&h.Env.ie<10&&void 0!==window.wpLink?window.wpLink.open(l.id):(i=function(){var e,t,n=l.selection.getStart(),i=l.dom.getParent(n,"a[href]");return i||(t=l.selection.getContent({format:"raw"}))&&-1!==t.indexOf("</a>")&&((e=t.match(/href="([^">]+)"/))&&e[1]&&(i=l.$('a[href="'+e[1]+'"]',n)[0]),i&&l.selection.select(i)),i}(),r.tempHide=!1,i||(e(),l.execCommand("mceInsertLink",!1,{href:"_wp_link_placeholder"}),i=l.$('a[href="_wp_link_placeholder"]')[0],l.nodeChanged()),l.dom.setAttribs(i,{"data-wplink-edit":!0}))}),l.addCommand("wp_link_apply",function(){if(!r.scrolling){var e,t;if(i){e=c.getURL(),t=c.getLinkText(),l.focus();var n=document.createElement("a");if(n.href=e,"javascript:"!==n.protocol&&"data:"!==n.protocol||(e=""),!e)return void l.dom.remove(i,!0);/^(?:[a-z]+:|#|\?|\.|\/)/.test(e)||o.test(e)||(e="http://"+e),l.dom.setAttribs(i,{href:e,"data-wplink-edit":null}),h.trim(i.innerHTML)||l.$(i).text(t||e),f(i)}c.reset(),l.nodeChanged(),void 0===window.wpLinkL10n||m||w(window.wpLinkL