diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index a57b5d8b..f071c72a 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -63,88 +63,6 @@ Ox.highlight = function(txt, str, classname) { ) : txt; }; -/*@ -Ox.highlightHTML Highlight matches in an HTML string - > Ox.highlightHTML('foobar', 'foobar', 'h') - 'foobar' - > Ox.highlightHTML('foobar', 'foobar', 'h') - 'foobar' - > Ox.highlightHTML('foo
bar', 'foobar', 'h') - 'foo
bar' - > Ox.highlightHTML('AT&T', 'AT&T', 'h') - 'AT&T' - > Ox.highlightHTML('AT&T', 'amp', 'h') - 'AT&T' - > Ox.highlightHTML('a <b> c', '', 'h') - 'a <b> c' - > Ox.highlightHTML('a
c', 'b', 'h') - 'a
c' -@*/ -Ox.highlightHTML = function(html, string, classname, tags) { - var count = 0, - isEntity = false, - isTag = false, - position, - positions = []; - //fixme: default tags should be same as in parseHTML - tags = (tags || []).concat([ - // inline formatting - 'b', 'code', 'i', 's', 'sub', 'sup', 'u', - // block formatting - 'blockquote', 'h1', 'h2', 'h3', 'p', 'pre', - // lists - 'li', 'ol', 'ul', - // tables - 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', - // other - 'a', 'br', 'img', - ]); - string = Ox.encodeHTML(string).toLowerCase(); - Ox.forEach(html.toLowerCase(), function(char, i) { - // check for entity or tag start - if (!isEntity && char == '&') { - isEntity = true; - } else if (!isTag && char == '<') { - Ox.forEach(tags, function(tag) { - if (html.slice(i + 1).match(new RegExp('^/?' + tag + '\\W'))) { - isTag = true; - Ox.Break(); - } - }); - } - // if outside entity or tag - if (!isEntity && !isTag) { - // if character matches - if (char == string[count]) { - if (count == 0) { - position = i; - } - count++; - if (count == string.length) { - // make sure matches are last to first - positions.unshift([position, i + 1]); - } - } else { - count = 0; - } - } - // check for entity or tag end - if (isEntity && char == ';') { - isEntity = false; - } else if (isTag && char == '>') { - isTag = false; - } - }); - positions.forEach(function(position) { - var match = '' - + html.slice(position[0], position[1]) - .replace(/(<.*?>)/g, '$1') - + ''; - html = html.slice(0, position[0]) + match + html.slice(position[1]); - }); - return html; -} - /*@ Ox.isValidEmail Tests if a string is a valid e-mail address (str) -> True if the string is a valid e-mail address @@ -245,6 +163,44 @@ Ox.parseSRT = function(string, fps) { }); }; +/*@ +Ox.parseURL Takes a URL, returns its components + (url) -> URL components + url URL + + > Ox.test.object.hash + '#c' + > Ox.test.object.host + 'www.foo.com:8080' + > Ox.test.object.hostname + 'www.foo.com' + > Ox.test.object.origin + 'http://www.foo.com:8080' + > Ox.test.object.pathname + '/bar/index.html' + > Ox.test.object.port + '8080' + > Ox.test.object.protocol + 'http:' + > Ox.test.object.search + '?a=0&b=1' +@*/ +Ox.parseURL = (function() { + var a = document.createElement('a'), + keys = ['hash', 'host', 'hostname', 'origin', + 'pathname', 'port', 'protocol', 'search']; + return function(string) { + var ret = {}; + a.href = string; + keys.forEach(function(key) { + ret[key] = a[key]; + }); + return ret; + }; +}()); + Ox.parseUserAgent = function(userAgent) { var aliases = { browser: {