node-create.js revision 36ca1e4313c10e481525962934a360b773992432
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass/**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @module node
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @submodule node-base
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass */
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glassvar Y_Node = Y.Node,
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y_DOM = Y.DOM;
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass/**
daeb6d531149c45a2ceb543ae2cf1e56e5235bbeDav Glass * Returns a new dom node using the provided markup string.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @method create
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @static
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} html The markup used to create the element
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {HTMLDocument} doc An optional document context
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @return {Node} A Node instance bound to a DOM node or fragment
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @for Node
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav GlassY_Node.create = function(html, doc) {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass if (doc && doc._node) {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass doc = doc._node;
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass }
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass return Y.one(Y_DOM.create(html, doc));
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass};
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav GlassY.mix(Y_Node.prototype, {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Creates a new Node using the provided markup string.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @method create
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} html The markup used to create the element
f69da96c272d2efe7f43da5150c13c0fe6899646Dav Glass * @param {HTMLDocument} doc An optional document context
56fa0b5ce00608c58c7c82cc5341c5fbf8898719Dav Glass * @return {Node} A Node instance bound to a DOM node or fragment
c145dba89e51cac0a247d52f4f4c0c8d8245027dDav Glass */
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass create: Y_Node.create,
56fa0b5ce00608c58c7c82cc5341c5fbf8898719Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Inserts the content before the reference node.
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * @method insert
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String | Node | HTMLElement | NodeList | HTMLCollection} content The content to insert
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass * @param {Int | Node | HTMLElement | String} where The position to insert at.
9d0be4bedcaad945c505994577198223ffc4ed9eDav Glass * Possible "where" arguments
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dl>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dt>Y.Node</dt>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dd>The Node to insert before</dd>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dt>HTMLElement</dt>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dd>The element to insert before</dd>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dt>Int</dt>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dd>The index of the child element to insert before</dd>
a3d67a254e415c93408a9d4314e46e3cb7d87d99Dav Glass * <dt>"replace"</dt>
0db84e0da684308b0fd9ea9b5906c11bafa7a246Dav Glass * <dd>Replaces the existing HTML</dd>
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * <dt>"before"</dt>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * <dd>Inserts before the existing HTML</dd>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * <dt>"before"</dt>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * <dd>Inserts content before the node</dd>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * <dt>"after"</dt>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * <dd>Inserts content after the node</dd>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * </dl>
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * @chainable
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass */
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass insert: function(content, where) {
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass this._insert(content, where);
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass return this;
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass },
a2cf1018c8c3c924dd3210ac6d5d796939a87876Dav Glass
a2cf1018c8c3c924dd3210ac6d5d796939a87876Dav Glass _insert: function(content, where) {
a2cf1018c8c3c924dd3210ac6d5d796939a87876Dav Glass var node = this._node,
a2cf1018c8c3c924dd3210ac6d5d796939a87876Dav Glass ret = null;
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass if (typeof where == 'number') { // allow index
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass where = this._node.childNodes[where];
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass } else if (where && where._node) { // Node
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass where = where._node;
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass }
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass if (content && typeof content != 'string') { // allow Node or NodeList/Array instances
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass content = content._node || content._nodes || content;
9b5e5f2ec4de2f405fbad1fc7c71325c84feb772Dav Glass }
9b5e5f2ec4de2f405fbad1fc7c71325c84feb772Dav Glass ret = Y_DOM.addHTML(node, content, where);
9b5e5f2ec4de2f405fbad1fc7c71325c84feb772Dav Glass
9b5e5f2ec4de2f405fbad1fc7c71325c84feb772Dav Glass return ret;
9b5e5f2ec4de2f405fbad1fc7c71325c84feb772Dav Glass },
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass
f8b3e367f0b400ff8b4feeb35c56d9387a31f9e0Dav Glass /**
b7bd9aa409761c479bb0a2e5794295d35ebe24eaDav Glass * Inserts the content as the firstChild of the node.
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass * @method prepend
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass * @param {String | Node | HTMLElement} content The content to insert
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass * @chainable
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass */
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass prepend: function(content) {
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass return this.insert(content, 0);
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass },
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass /**
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * Inserts the content as the lastChild of the node.
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @method append
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @param {String | Node | HTMLElement} content The content to insert
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @chainable
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
883d785f73cff98fbe2fe00953621713c1489729Dav Glass append: function(content) {
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass return this.insert(content, null);
883d785f73cff98fbe2fe00953621713c1489729Dav Glass },
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass /**
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @method appendChild
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @param {String | HTMLElement | Node} node Node to be appended
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @return {Node} The appended node
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass appendChild: function(node) {
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass return Y_Node.scrubVal(this._insert(node));
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass },
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass /**
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @method insertBefore
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @param {String | HTMLElement | Node} newNode Node to be appended
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @param {HTMLElement | Node} refNode Node to be inserted before
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @return {Node} The inserted node
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass insertBefore: function(newNode, refNode) {
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass return Y.Node.scrubVal(this._insert(newNode, refNode));
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass },
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass /**
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * Appends the node to the given node.
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @method appendTo
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @param {Node | HTMLElement} node The node to append to
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @chainable
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass appendTo: function(node) {
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass Y.one(node).append(this);
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass return this;
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass },
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Replaces the node's current content with the content.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Note that this passes to innerHTML and is not escaped.
762b21413a7bbc38b5c7b2d94385fb44f26f9d39Dav Glass * Use `Y.Escape.html()` to escape HTML, or `set('text')` to add as text.
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method setContent
e08067d50c8d5e4692441db53bba084c7a667e0aDav Glass * @deprecated Use setHTML
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * @param {String | Node | HTMLElement | NodeList | HTMLCollection} content The content to insert
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * @chainable
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass */
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass setContent: function(content) {
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass this._insert(content, 'replace');
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass return this;
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass },
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass /**
3499692683cbc70ff10f1844350ff92b1d0eba3fDav Glass * Returns the node's current content (e.g. innerHTML)
fe442de8408a276919d26a1110e8121844b1ba11Dav Glass * @method getContent
edefd42ddde54932d14d9150369570db47822ab5Dav Glass * @deprecated Use getHTML
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass * @return {String} The current content
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass */
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass getContent: function(content) {
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass return this.get('innerHTML');
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass }
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass});
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass/**
323ca860aba4b2515b560796e2322d5eaae57e5aDav Glass * Replaces the node's current html content with the content provided.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Note that this passes to innerHTML and is not escaped.
edefd42ddde54932d14d9150369570db47822ab5Dav Glass * Use `Y.Escape.html()` to escape HTML, or `set('text')` to add as text.
1e2a941753ef897ca70f03b0c9803795357ad35dDav Glass * @method setHTML
577da64a9f0680112357f6595f47bfcab32d9adbDav Glass * @param {String | HTML | Node | HTMLElement | NodeList | HTMLCollection} content The content to insert
577da64a9f0680112357f6595f47bfcab32d9adbDav Glass * @chainable
577da64a9f0680112357f6595f47bfcab32d9adbDav Glass */
1e2a941753ef897ca70f03b0c9803795357ad35dDav GlassY.Node.prototype.setHTML = Y.Node.prototype.setContent;
edefd42ddde54932d14d9150369570db47822ab5Dav Glass
a2621d519886de7d60c30c5a0850f5c17fd2fb36Dav Glass/**
a2621d519886de7d60c30c5a0850f5c17fd2fb36Dav Glass * Returns the node's current html content (e.g. innerHTML)
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass * @method getHTML
762b21413a7bbc38b5c7b2d94385fb44f26f9d39Dav Glass * @return {String} The html content
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass */
43e2f05871a8e99383b8c1d893d290cd978be81aDav GlassY.Node.prototype.getHTML = Y.Node.prototype.getContent;
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass
b357b56da58949fa86ab8e56983972e0db5cbffbDav GlassY.NodeList.importMethod(Y.Node.prototype, [
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass /**
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass * Called on each Node instance
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass * @for NodeList
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass * @method append
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass * @see Node.append
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass */
aa2ac226ad6e45232f8416eecc99d2165ce74d03Dav Glass 'append',
43e2f05871a8e99383b8c1d893d290cd978be81aDav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method insert
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @see Node.insert
0db84e0da684308b0fd9ea9b5906c11bafa7a246Dav Glass */
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass 'insert',
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass /**
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass * Called on each Node instance
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass * @for NodeList
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass * @method appendChild
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass * @see Node.appendChild
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass */
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass 'appendChild',
6dbc2e0b2c23ae7763959af9762fc50c84dbd937Dav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
91085c33d52b89808c10ff61e5a94d7b35d57d65Dav Glass * @method insertBefore
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @see Node.insertBefore
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass */
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass 'insertBefore',
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method prepend
91085c33d52b89808c10ff61e5a94d7b35d57d65Dav Glass * @see Node.prepend
690dda02d518be26b6ee1c6530ee361081c9eb03Dav Glass */
690dda02d518be26b6ee1c6530ee361081c9eb03Dav Glass 'prepend',
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * Note that this passes to innerHTML and is not escaped.
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * Use `Y.Escape.html()` to escape HTML, or `set('text')` to add as text.
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method setContent
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @deprecated Use setHTML
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass */
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass 'setContent',
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method getContent
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @deprecated Use getHTML
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass */
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass 'getContent',
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass /** Called on each Node instance
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * @method setHTML
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * Note that this passes to innerHTML and is not escaped.
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass * Use `Y.Escape.html()` to escape HTML, or `set('text')` to add as text.
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass */
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass 'setHTML',
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass
690dda02d518be26b6ee1c6530ee361081c9eb03Dav Glass /** Called on each Node instance
690dda02d518be26b6ee1c6530ee361081c9eb03Dav Glass * @method getHTML
690dda02d518be26b6ee1c6530ee361081c9eb03Dav Glass */
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass 'getHTML'
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass]);
e20a95dc78bf51b6544e7bfb4e53ed190ecec3c9Dav Glass