Permalink
Browse files

Element#indexOf, Element.extendIframe, fixes to protoquery, experimen…

…tal #htmlToMarkdown
  • Loading branch information...
1 parent 361a7ba commit b48111b9f8312935f6ef420cc6144ef8bed3a802 kangax committed Jun 7, 2008
Showing with 79 additions and 3 deletions.
  1. +37 −0 element.methods.js
  2. +2 −1 form.element.methods.js
  3. +5 −1 protoquery/protoquery.js
  4. +35 −1 string.extensions.js
View
37 element.methods.js
@@ -183,4 +183,41 @@ Element.Methods.wrapContent = function(element, wrapper, attributes) {
return element;
};
+/**
+ * This snippet fully duplicates what prototype does when initializing Element.extend,
+ * but applied to a specified iframe document
+ * I think we should expose "copy" function as a public method, for cases like this one
+ * Tested in FF and Safari. Doesn't work in Opera.
+ *
+ **/
+Element.extendIframe = function(element) {
+ element = $(element);
+ var proto = $(element).contentWindow.HTMLElement.prototype;
+ function copy(methods, destination, onlyIfAbsent) {
+ onlyIfAbsent = onlyIfAbsent || false;
+ for (var property in methods) {
+ var value = methods[property];
+ if (!Object.isFunction(value)) continue;
+ if (!onlyIfAbsent || !(property in destination))
+ destination[property] = value.methodize();
+ }
+ }
+ copy(Element.Methods, proto);
+ copy(Element.Methods.Simulated, proto, true);
+ return element;
+}
+/**
+ * Element.indexOf(@element) -> Number|undefined
+ * returns index of element in its parent or undefined if parent does not exist
+ *
+ * $(document.body).indexOf(); // 1
+ * $$('ul#nav li a.active')[0].indexOf(); // 12
+ *
+ **/
+Element.Methods.indexOf = function(element) {
+ var parent = $(element.parentNode);
+ if (!parent) return;
+ return parent.childElements().indexOf(element);
+}
+
Element.addMethods();
View
3 form.element.methods.js
@@ -34,7 +34,8 @@ Field.Methods.present = function(element) {
**/
Field.Methods.selectOptionByValue = function(element, value) {
var index = 0;
- element = $(element);
+ element = $(element);
+ value = parseInt(value, 10);
Element.childElements(element).each(function(element, i) {
if (element.value == value) {
index = i;
View
6 protoquery/protoquery.js
@@ -49,7 +49,8 @@ $Q.addMethods({
var Wrapper = Class.create(Enumerable, {
initialize: function(selector) {
- this.elements = Object.isString(selector) ? $$(selector) : [$(selector)];
+ this.elements = Object.isString(selector) ? $$(selector) : [selector ? $(selector) : document];
+ this._toArray();
}
});
@@ -104,6 +105,9 @@ $Q.addMethods({
},
last: function() {
return this.elements.last();
+ },
+ _toArray: function() {
+ Array.prototype.push.apply(this, this.elements);
}
});
View
36 string.extensions.js
@@ -13,4 +13,38 @@ String.prototype.namespace = function(parent) {
return this.split('.').inject(parent || window, function(object, property) {
return object[property] = object[property] || { };
})
-};
+};
+
+// It seems impossible to translate html into a markdown without implementing a parser
+String.prototype.htmlToMarkdown = (function(){
+
+ var reTag = /<(\S*)\s*([^>]*)>(.*)<\/\1>/;
+ var map = {
+ h1: '\# #{content}',
+ h2: '\#\# #{content}',
+ h3: '\#\#\# #{content}',
+ h4: '\#\#\#\# #{content}',
+ h5: '\#\#\#\#\# #{content}',
+ h6: '\#\#\#\#\#\# #{content}'
+ }
+
+ for (var name in map) {
+ map[name] = new Template(map[name]);
+ }
+
+ return function() {
+
+ var content = '';
+ var match = this.match(reTag);
+
+ if (!match) return;
+
+ var tagName = match[1];
+ var attributes = match[2];
+ var content = match[3];
+
+ if (!map[tagName]) return '';
+
+ return map[tagName].evaluate({ content: this.stripTags() });
+ }
+})();

0 comments on commit b48111b

Please sign in to comment.