react / wstein / node_modules / jest-cli / node_modules / jsdom / lib / jsdom / browser / htmltodom.js
80684 viewsvar HTMLDecode = require('./htmlencoding').HTMLDecode;12function HtmlToDom(parser) {34if(parser && parser.write) {5// sax parser6this.appendHtmlToElement = function(html, element){78var currentElement = element, currentLevel = 0;910parser.onerror = function (e) {};1112parser.ontext = function (t) {13var ownerDocument = currentElement.ownerDocument || currentElement;14var newText = ownerDocument.createTextNode(t);15currentElement.appendChild(newText);16};1718parser.onopentag = function (node) {19var nodeName = node.name.toLowerCase(),20document = currentElement.ownerDocument || currentElement,21newElement = document.createElement(nodeName),22i = 0,23length = (node.attributes && node.attributes.length) ?24node.attributes.length :250;2627for (i in node.attributes) {28if (node.attributes.hasOwnProperty(i)) {29newElement.setAttribute(i, node.attributes[i]);30}31}3233for (i=0; i<node.attributes.length; i++) {34newElement.setAttribute(i, node.attributes.item(i));35}36currentElement.appendChild(newElement);37currentElement = newElement;38};3940parser.onclosetag = function(node) {41currentElement = currentElement.parentNode;42};4344parser.write(html).close();4546return element;47};4849} else if (parser && (parser.ParseHtml || parser.DefaultHandler)) {5051// Forgiving HTML parser5253if (parser.ParseHtml) {54// davglass/node-htmlparser55} else if (parser.DefaultHandler){56// fb55/htmlparser25758parser.ParseHtml = function(rawHtml) {59var handler = new parser.DefaultHandler();60// Check if document is XML61var isXML = (/^<\?\s*xml.*version=["']1\.0["'].*\s*\?>/i).test(rawHtml);62var parserInstance = new parser.Parser(handler, {63xmlMode: isXML,64lowerCaseTags: !isXML,65lowerCaseAttributeNames: !isXML66});6768parserInstance.includeLocation = false;69parserInstance.parseComplete(rawHtml);70return handler.dom;71};72}7374this.appendHtmlToElement = function(html, element) {7576if (typeof html !== 'string') {77html +='';78}7980var parsed = parser.ParseHtml(html);8182for (var i = 0; i < parsed.length; i++) {83setChild(element, parsed[i]);84}8586return element;87};8889} else if (parser && parser.moduleName == 'HTML5') { /* HTML5 parser */90this.appendHtmlToElement = function(html, element) {9192if (typeof html !== 'string') {93html += '';94}95if (html.length > 0) {96if (element.nodeType == 9) {97new parser.Parser({document: element}).parse(html);98}99else {100var p = new parser.Parser({document: element.ownerDocument});101p.parse_fragment(html, element);102}103}104};105} else {106107this.appendHtmlToElement = function(){108console.log('');109console.log('###########################################################');110console.log('# WARNING: No HTML parser could be found.');111console.log('# Element.innerHTML setter support has been disabled');112console.log('# Element.innerHTML getter support will still function');113console.log('# Download: http://github.com/tautologistics/node-htmlparser');114console.log('###########################################################');115console.log('');116};117118}119};120121// utility function for forgiving parser122function setChild(parent, node) {123124var c, newNode, currentDocument = parent._ownerDocument || parent;125126switch (node.type)127{128case 'tag':129case 'script':130case 'style':131try {132newNode = currentDocument.createElement(node.name);133if (node.location) {134newNode.sourceLocation = node.location;135newNode.sourceLocation.file = parent.sourceLocation.file;136}137} catch (err) {138currentDocument.raise('error', 'invalid markup', {139exception: err,140node : node141});142143return null;144}145break;146147case 'text':148// Decode HTML entities if we're not inside a <script> or <style> tag:149newNode = currentDocument.createTextNode(/^(?:script|style)$/i.test(parent.nodeName) ?150node.data :151HTMLDecode(node.data));152break;153154case 'comment':155newNode = currentDocument.createComment(node.data);156break;157158default:159return null;160break;161}162163if (!newNode)164return null;165166if (node.attribs) {167for (c in node.attribs) {168// catchin errors here helps with improperly escaped attributes169// but properly fixing parent should (can only?) be done in the htmlparser itself170try {171newNode.setAttribute(c, HTMLDecode(node.attribs[c]));172} catch(e2) { /* noop */ }173}174}175176if (node.children) {177for (c = 0; c < node.children.length; c++) {178setChild(newNode, node.children[c]);179}180}181182try{183return parent.appendChild(newNode);184}catch(err){185currentDocument.raise('error', err.message, {186exception: err,187node : node188});189return null;190}191}192193exports.HtmlToDom = HtmlToDom;194195196