Permalink
Browse files

Added URL detection

  • Loading branch information...
1 parent e29c8d1 commit 7b69c71769d030ceab66367ff87dc4b5063a1fd4 @sergeche committed Sep 10, 2012
Showing with 30 additions and 5 deletions.
  1. +4 −0 css/_colorizer.css
  2. +1 −1 extensions/chrome/manifest.json
  3. +1 −2 index.html
  4. +3 −1 src/renderer.js
  5. +20 −0 src/utils.js
  6. +1 −1 xml/example.xml
View
@@ -195,4 +195,8 @@
font-weight: bold;
padding-left:17px;
color: #2F6F9F;
+}
+
+.xv-url {
+ color: #1732b4;
}
@@ -1,6 +1,6 @@
{
"name" : "XV — XML Viewer",
- "version" : "1.0.8",
+ "version" : "1.0.9",
"description" : "Powerful XML viewer for Google Chrome",
"manifest_version": 2,
"background": {
View
@@ -29,10 +29,9 @@
<div class="xv-source-pane">
<div class="xv-source-pane-inner"></div>
</div>
-
<script type="text/javascript">
var xhr = new XMLHttpRequest();
- xhr.open('GET', './xml/ns.xml', true);
+ xhr.open('GET', './xml/example.xml', true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
xv_controller.process(xhr.responseXML);
View
@@ -9,6 +9,7 @@
var _id = 0,
orig_elems = {},
+ oneline_text_len = 100;
/**
@@ -148,7 +149,8 @@
return result.join('');
}
- function processText(text) {
+ function processText(text) {
+ text = xv_utils.escapeHTML(xv_utils.trim(text));
return xv_utils.findURLs(text);
}
View
@@ -5,6 +5,9 @@
var xv_utils = (function(){
var re_not_xpath = /^[\w\-\:]+$/;
+ // regexp from http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/
+ var reURL = /\b(?:(?:https?|ftp|file|feed):\/\/|www\.|ftp\.)[\-A-Z0-9\+&@#\/%=~_\|\$\?\!:,\.]*[A-Z0-9+&@#\/%=~_|$]/ig;
+
return {
/**
* Trim whitespace from the beginning and the end of string
@@ -114,6 +117,23 @@ var xv_utils = (function(){
return _.filter(elem.attributes, function(n) {
return n.name.indexOf('data-xv-') != 0;
});
+ },
+
+ /**
+ * Finds URLs inside text and wraps it with &lt;a&gt; tag
+ * @param {String} text
+ * @returns {String}
+ */
+ findURLs: function(text) {
+ return text.replace(reURL, function(url) {
+ // has protocol?
+ var href = url;
+ if (!/^[a-z]+:\/\//i.test(href)) {
+ href = 'http://' + href;
+ }
+
+ return '<a href="' + href + '" class="xv-url">' + url + '</a>';
+ });
}
};
})();
View
@@ -8,7 +8,7 @@
<test xmlns='http://example.com/def'>asdf</test>
-<sub><xyz:bar>test</xyz:bar></sub>
+<sub><xyz:bar href="http://www.example.com">link to www.example.com</xyz:bar></sub>
<selfclosed/>
<selfclosed with='attributes'/>

0 comments on commit 7b69c71

Please sign in to comment.