/** * * File: jurlp.js * * Title: JQuery URL parser. * * JQuery URL parser plugin for parsing, manipulating, filtering and monitoring URLs in href and src attributes within arbitrary elements (including document.location.href), as well as creating anchor elements from URLs found in HTML/text. * * About: Authors * * Thomas James Bonner (tom.bonner@gmail.com). * * Yonas Sandbæk (seltar@gmail.com). * * About: Version * * 1.0.4 * * About: License * * Copyright (C) 2012, Thomas James Bonner (tom.bonner@gmail.com). * * MIT License: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **/ /** * * Section: URL overview. * * URL naming scheme: * * A quick quide to URL nomenclature in this plugin. * * Throughout this plugin, URLs are segmented and refered to in the following manner; * * > http://username:password@www.example.com:443/path/file.name?query=string#anchor * > |_____||______| |______| |_____________| |_||_____________||___________||_____| * > | | | | | | | | * > scheme user password host port path query fragment * > |______________________________________________________________________________| * > | * > url * * Scheme: * * Contains the protocol identifier (i.e. "https://", "ftp://"). * * User: * * Conains the username to use when connecting to the host server. This segment may be empty. * * Password: * * Contains the password to use in conjunction with the username when connecting to the remote server. This segment may be empty (and cannot be set without a user name). * * Host: * * Contains the name or IP address of the host server (i.e. "www.example.com", or "127.0.0.1"). * * Port: * * Contains the listening port number for the host server (i.e. "80", or "8080"). Note that an empty port value implies the default port (80). * * Path: * * Contains the file path (i.e. "/index.html", or "/"). * * Query: * * Contains any parameters passed in the query (i.e. "?param1=value1¶m2=value2"). This segment may be empty. * * Fragment: * * Contains any anchors/hash tags (i.e. "#elementname"). This segment may be empty. * * Section: URL Objects * * URL object definition. * * For the purposes of this plugin, URLs can be represented either as a string, for example "http://www.example.com:8080/path/file.name?query=string#anchor", or as an object; * * (start code) * * { * scheme: "http://" * user: "username", * password: "password", * host: "www.example.com", * port: "8080", * path: "/path/file.name", * query: "?query=string", * fragment: "#anchor" * } * * (end code) * * Therefore, wherever URLs are supplied as a parameter to the plugin via the or methods, either a string or object representation or the URL may be supplied. * * URL objects that have been returned via the parser interface can easily be converted to a string by calling the objects toString() method. * * Example: * * (start code) * * // Parse the document.location.href URL, and convert it back to a string again. * $(document).jurlp("url").toString(); * * (end code) * */ /** * * Section: Quick overview * * Useful example code. * * (start code) * * // Parse and set the element(s) URL * $("a").jurlp("url"); * $("a").jurlp("url", "http://www.example.com/"); * * // Get or set individual URL segments for the element(s) * $("a").jurlp("scheme"); * $("a").jurlp("scheme", "https://"); * * $("a").jurlp("user"); * $("a").jurlp("user", "username"); * * $("a").jurlp("password"); * $("a").jurlp("password", "password"); * * $("a").jurlp("host"); * $("a").jurlp("host", "www.example.com"); * * $("a").jurlp("port"); * $("a").jurlp("port", "8080"); * * $("a").jurlp("path"); * $("a").jurlp("path", "../file.name"); * * $("a").jurlp("query"); * $("a").jurlp("query", {"param":"value"}); * * $("a").jurlp("fragment"); * $("a").jurlp("fragment", "elementid"); * * // Filter on URL segments * $("a").jurlp("filter", "scheme", "^=", "http") * .jurlp("filter", "user", "=", "user") * .jurlp("filter", "password", "=", "password") * .jurlp("filter", "host", "=", "www.example.com") * .jurlp("filter", "port", "!=", "8080") * .jurlp("filter", "path", "$=", ".html") * .jurlp("filter", "query", "*=", "param=value") * .jurlp("filter", "fragment", "regex", /(\#)/); * * // Watch a selector for new nodes * $("a:eq(0)").jurlp("watch", function(element, selector){}) * .jurlp("filter", "host", "=", "www.example.com") * .jurlp("query",{"found":"example"}); * * $("body").prepend(""); * * $("a:eq(0)").jurlp("unwatch"); * * // Parse an element's text for URLs and create/return anchor elements * $("
www.example.com
").jurlp(); * * // Get an interface for parsing/manipulating the supplied URL * url = $.jurlp("http://www.example.com:80/path/file.name?param1=value1#fragment"); * * // Parse the URL to an object. * url.url(); * * // Get the URL scheme. * url.scheme(); * * // Get the URL user name. * url.user(); * * // Get the URL password. * url.password(); * * // Get the URL host. * url.host(); * * // Get the URL port. * url.port(); * * // Get the URL path. * url.path(); * * // Get the URL query. * url.query(); * * // Get a specific parameter value from the URL query. * url.query().param1; * * // Get the URL fragment. * url.fragment(); * * // Set the full URL. * url.url("http://www.example.com:80/path/file.name?param1=value1#fragment"); * * // Set the URL scheme. * url.scheme("https://"); * * // Set the URL user name. * url.user("user"); * * // Set the URL password. * url.password("password"); * * // Set the URL host. * url.host("www.newexample.com"); * * // Set the URL port. * url.port("80"); * * // Set the URL path. * url.path("/newpath/newfile.file"); * * // Append to the URL path. * url.path("./newfile.file"); * * // Remove two path elements and append to the URL path. * url.path("../../newfile.file"); * * // Set the URL query. * url.query("?param=value"); * * // Append/modify the URL query (string or object) * url.query("param=value"); * url.query({"param":"value"}); * * // Remove the URL query * url.query(""); * url.query({}); * * // Set the URL fragment. * url.fragment("#newfragment"); * * (end code) * **/ /** * Section: Parsing document.location.href * * Parsing the document URL. * * The document URL (document.location.href) can be parsed by specifying the HTML document element to the parser in the following manner; * * (start code) * * // Parse the document.location.href URL string into a URL object * $(document).jurlp("url"); * * (end code) * * Similarly, the document URL can be modified by the plugin, but it is worth noting that changes will not be directly applied to document.location.href until is explicitly called on the element, and instead, a working copy of the URL is stored under the documents "data-href" attribute. * * (start code) * * // Does not modify document.location.href (updates $(document).data("href")) * $(document).jurlp("url", "www.example.com"); * * // Does modify document.location.href (from $(document).data("href")) * $(document).jurlp("goto"); * * (end code) * */ /** * Section: Parsing elements with an "href" or "src" attribute. * * Parsing "href" or "src" attributes. * * Elements with an "href" or "src" attribute (i.e. , , , ,