move Ox.parseURL to String.js and Ox.highlightHTML to HTML.js
This commit is contained in:
parent
a4e0b60f87
commit
f94395d652
1 changed files with 38 additions and 82 deletions
|
@ -63,88 +63,6 @@ Ox.highlight = function(txt, str, classname) {
|
|||
) : txt;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.highlightHTML <f> Highlight matches in an HTML string
|
||||
> Ox.highlightHTML('<b>foo</b>bar', 'foobar', 'h')
|
||||
'<b><span class="match">foo</span></b><span class="h">bar</span>'
|
||||
> Ox.highlightHTML('<a href="/foo">foo</a>bar', 'foobar', 'h')
|
||||
'<a href="/foo"><span class="h">foo</span></a><span class="h">bar</span>'
|
||||
> Ox.highlightHTML('foo<br>bar', 'foobar', 'h')
|
||||
'foo<br>bar'
|
||||
> Ox.highlightHTML('AT&T', 'AT&T', 'h')
|
||||
'<span class="h">AT&T</span>'
|
||||
> Ox.highlightHTML('AT&T', 'amp', 'h')
|
||||
'AT&T'
|
||||
> Ox.highlightHTML('a <b> c', '<b>', 'h')
|
||||
'a <span class="h"><b></span> c'
|
||||
> Ox.highlightHTML('a <br> c', 'b', 'h')
|
||||
'a <br> 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 = '<span class="' + classname + '">'
|
||||
+ html.slice(position[0], position[1])
|
||||
.replace(/(<.*?>)/g, '</span>$1<span class="' + classname + '">')
|
||||
+ '</span>';
|
||||
html = html.slice(0, position[0]) + match + html.slice(position[1]);
|
||||
});
|
||||
return html;
|
||||
}
|
||||
|
||||
/*@
|
||||
Ox.isValidEmail <f> Tests if a string is a valid e-mail address
|
||||
(str) -> <b> True if the string is a valid e-mail address
|
||||
|
@ -245,6 +163,44 @@ Ox.parseSRT = function(string, fps) {
|
|||
});
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.parseURL <f> Takes a URL, returns its components
|
||||
(url) -> <o> URL components
|
||||
url <s> URL
|
||||
<script>
|
||||
Ox.test.object = Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c');
|
||||
</script>
|
||||
> 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: {
|
||||
|
|
Loading…
Reference in a new issue