forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulled out markup rendering logic for better reuse.
- Loading branch information
Showing
5 changed files
with
254 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright 2013 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @providesModule createArrayFrom | ||
* @typechecks | ||
*/ | ||
|
||
var hasArrayNature = require('hasArrayNature'); | ||
|
||
/** | ||
* Ensure that the argument is an array by wrapping it in an array if it is not. | ||
* Creates a copy of the argument if it is already an array. | ||
* | ||
* This is mostly useful idiomatically: | ||
* | ||
* var createArrayFrom = require('createArrayFrom'); | ||
* | ||
* function takesOneOrMoreThings(things) { | ||
* things = createArrayFrom(things); | ||
* ... | ||
* } | ||
* | ||
* This allows you to treat `things' as an array, but accept scalars in the API. | ||
* | ||
* This is also good for converting certain pseudo-arrays, like `arguments` or | ||
* HTMLCollections, into arrays. | ||
* | ||
* @param {*} obj | ||
* @return {array} | ||
*/ | ||
function createArrayFrom(obj) { | ||
if (!hasArrayNature(obj)) { | ||
return [obj]; | ||
} | ||
if (obj.item) { | ||
// IE does not support Array#slice on HTMLCollections | ||
var l = obj.length, ret = new Array(l); | ||
while (l--) { ret[l] = obj[l]; } | ||
return ret; | ||
} | ||
return Array.prototype.slice.call(obj); | ||
} | ||
|
||
module.exports = createArrayFrom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright 2013 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @providesModule createNodesFromMarkup | ||
* @typechecks | ||
*/ | ||
|
||
/*jslint evil: true, sub: true */ | ||
|
||
var createArrayFrom = require('createArrayFrom'); | ||
var getMarkupWrap = require('getMarkupWrap'); | ||
var invariant = require('invariant'); | ||
|
||
/** | ||
* Dummy container used to render all markup. | ||
*/ | ||
var dummyNode = document.createElement('div'); | ||
|
||
/** | ||
* Pattern used by `getNodeName`. | ||
*/ | ||
var nodeNamePattern = /^\s*<(\w+)/; | ||
|
||
/** | ||
* Extracts the `nodeName` of the first element in a string of markup. | ||
* | ||
* @param {string} markup String of markup. | ||
* @return {?string} Node name of the supplied markup. | ||
*/ | ||
function getNodeName(markup) { | ||
var nodeNameMatch = markup.match(nodeNamePattern); | ||
return nodeNameMatch && nodeNameMatch[1].toLowerCase(); | ||
} | ||
|
||
/** | ||
* Creates an array containing the nodes rendered from the supplied markup. The | ||
* optionally supplied `handleScript` function will be invoked once for each | ||
* <script> element that is rendered. If no `handleScript` function is supplied, | ||
* an exception is thrown if any <script> elements are rendered. | ||
* | ||
* @param {string} markup A string of valid HTML markup. | ||
* @param {?function} handleScript Invoked once for each rendered <script>. | ||
* @return {array<DOMElement|DOMTextNode>} An array of rendered nodes. | ||
*/ | ||
function createNodesFromMarkup(markup, handleScript) { | ||
var node = dummyNode; | ||
var nodeName = getNodeName(markup); | ||
|
||
var wrap = nodeName && getMarkupWrap(nodeName); | ||
if (wrap) { | ||
node.innerHTML = wrap[1] + markup + wrap[2]; | ||
|
||
var wrapDepth = wrap[0]; | ||
while (wrapDepth--) { | ||
node = node.lastChild; | ||
} | ||
} else { | ||
node.innerHTML = markup; | ||
} | ||
|
||
var scripts = node.getElementsByTagName('script'); | ||
if (scripts.length) { | ||
invariant( | ||
handleScript, | ||
'createNodesFromMarkup(...): Unexpected <script> element rendered.' | ||
); | ||
createArrayFrom(scripts).forEach(handleScript); | ||
} | ||
|
||
var nodes = createArrayFrom(node.childNodes); | ||
while (node.lastChild) { | ||
node.removeChild(node.lastChild); | ||
} | ||
return nodes; | ||
} | ||
|
||
module.exports = createNodesFromMarkup; |
Oops, something went wrong.