Skip to content
This repository has been archived by the owner on Sep 25, 2018. It is now read-only.

Commit

Permalink
Moved more dom stuff into platform.dom and fixed browser bugs. Now wo…
Browse files Browse the repository at this point in the history
…rks in IE7-9.
  • Loading branch information
jbeard4 committed Aug 11, 2012
1 parent 03b0d01 commit 008da7b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
12 changes: 12 additions & 0 deletions lib/browser/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ function getItem(nodeList,index){

var dom = Object.create(baseDom);

dom.hasAttribute = function(node,attribute){
return node.hasAttribute ? node.hasAttribute(attribute) : node.getAttribute(attribute);
};

dom.localName = function(node){
return node.localName || node.tagName;
};

dom.createElementNS = function(doc,ns,localName){
return doc.createElementNS ? doc.createElementNS(ns,localName) : doc.createElement(localName);
};

dom.getChildren = function(node){
var toReturn = [];
for(var i = 0; i < node.childNodes.length; i++){
Expand Down
28 changes: 14 additions & 14 deletions lib/core/util/annotate-scxml-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ var STATES_THAT_CAN_BE_CHILDREN = ["state", "parallel", "history", "final", "ini
STATE_TAGS = STATES_THAT_CAN_BE_CHILDREN.concat("scxml");


var states, basicStates, uniqueEvents, transitions, idToStateMap, onFoundStateIdCallbacks, datamodel, document;
var states, basicStates, uniqueEvents, transitions, idToStateMap, onFoundStateIdCallbacks, datamodel, doc;

var transformAndSerialize = exports.transformAndSerialize = transformAndSerialize = function(root) {
return JSON.stringify(transform(root));
};

var transform = exports.transform = function(doc) {
var transform = exports.transform = function(scxmlDoc) {

document = doc;
doc = scxmlDoc;

var root = document.documentElement;
var root = doc.documentElement;

states = [];
basicStates = [];
Expand Down Expand Up @@ -110,7 +110,7 @@ function transformTransitionNode (transitionNode, parentState) {

//wildcard "*" event will show up on transition.events, but will not show up in uniqueEvents
//default transitions (those without events) will have events set to undefined (rather than empty array)
if (transitionNode.hasAttribute('event')) {
if (pm.platform.dom.hasAttribute(transitionNode,'event')) {
var events;

var event = pm.platform.dom.getAttribute(transitionNode,'event');
Expand Down Expand Up @@ -145,7 +145,7 @@ function transformTransitionNode (transitionNode, parentState) {
source: parentState.id,
cond: pm.platform.dom.getAttribute(transitionNode,"cond"),
events: events,
targets: transitionNode.hasAttribute("target") ? pm.platform.dom.getAttribute(transitionNode,"target").trim().split(/\s+/) : null
targets: pm.platform.dom.hasAttribute(transitionNode,"target") ? pm.platform.dom.getAttribute(transitionNode,"target").trim().split(/\s+/) : null
};

if(pm.platform.dom.getElementChildren(transitionNode).length) transition.actions = codeGen.gen.parentToFnBody(transitionNode);
Expand All @@ -159,14 +159,14 @@ function transformTransitionNode (transitionNode, parentState) {

function transformDatamodel(node, ancestors) {
pm.platform.dom.getChildren(node).filter(function(child){return pm.platform.dom.localName(child) === 'data';}).forEach(function(child){
if (child.hasAttribute("id")) {
datamodel[pm.platform.dom.getAttribute(child,"id")] = child.hasAttribute("expr") ? pm.platform.dom.getAttribute(child,"expr") : null;
if (pm.platform.dom.hasAttribute(child,"id")) {
datamodel[pm.platform.dom.getAttribute(child,"id")] = pm.platform.dom.hasAttribute(child,"expr") ? pm.platform.dom.getAttribute(child,"expr") : null;
}
});
}

function transformStateNode(node, ancestors) {
var id = node.hasAttribute("id") ? pm.platform.dom.getAttribute(node,"id") : genId(pm.platform.dom.localName(node));
var id = pm.platform.dom.hasAttribute(node,"id") ? pm.platform.dom.getAttribute(node,"id") : genId(pm.platform.dom.localName(node));
var kind;

switch (pm.platform.dom.localName(node)) {
Expand Down Expand Up @@ -278,14 +278,14 @@ function transformStateNode(node, ancestors) {
});

if (!processedInitial && pm.platform.dom.localName(node) !== "parallel") {
var hasInitialAttribute = node.hasAttribute("initial");
var hasInitialAttribute = pm.platform.dom.hasAttribute(node,"initial");

//create a fake initial state and process him
var generateFakeInitialState = function(targetId) {
var initial = document.createElementNS(constants.SCXML_NS,"initial");
var transition = document.createElementNS(constants.SCXML_NS,"transition");
transition.setAttribute("target",targetId);
initial.appendChild(transition);
var initial = pm.platform.dom.createElementNS(doc,constants.SCXML_NS,"initial");
var transition = pm.platform.dom.createElementNS(doc,constants.SCXML_NS,"transition");
pm.platform.dom.setAttribute(transition,"target",targetId);
pm.platform.dom.appendChild(initial,transition);

return processInitialState(initial);
};
Expand Down
20 changes: 10 additions & 10 deletions lib/core/util/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ var actionTags = {
"log" : function(action){
var params = [];

if(action.hasAttribute("label")) params.push( JSON.stringify(pm.platform.dom.getAttribute(action,"label")));
if(action.hasAttribute("expr")) params.push( pm.platform.dom.getAttribute(action,"expr"));
if(pm.platform.dom.hasAttribute(action,"label")) params.push( JSON.stringify(pm.platform.dom.getAttribute(action,"label")));
if(pm.platform.dom.hasAttribute(action,"expr")) params.push( pm.platform.dom.getAttribute(action,"expr"));

return "$log(" + params.join(",") + ");";
},
Expand All @@ -118,19 +118,19 @@ var actionTags = {

"send" : function(action){
return "$send({\n" +
"target: " + (action.hasAttribute("targetexpr") ? pm.platform.dom.getAttribute(action,"targetexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"target"))) + ",\n" +
"name: " + (action.hasAttribute("eventexpr") ? pm.platform.dom.getAttribute(action,"eventexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"event"))) + ",\n" +
"type: " + (action.hasAttribute("typeexpr") ? pm.platform.dom.getAttribute(action,"typeexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"type"))) + ",\n" +
"target: " + (pm.platform.dom.hasAttribute(action,"targetexpr") ? pm.platform.dom.getAttribute(action,"targetexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"target"))) + ",\n" +
"name: " + (pm.platform.dom.hasAttribute(action,"eventexpr") ? pm.platform.dom.getAttribute(action,"eventexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"event"))) + ",\n" +
"type: " + (pm.platform.dom.hasAttribute(action,"typeexpr") ? pm.platform.dom.getAttribute(action,"typeexpr") : JSON.stringify(pm.platform.dom.getAttribute(action,"type"))) + ",\n" +
"data: " + constructSendEventData(action) + ",\n" +
"origin: $origin\n" +
"}, {\n" +
"delay: " + (action.hasAttribute("delayexpr") ? pm.platform.dom.getAttribute(action,"delayexpr") : getDelayInMs(pm.platform.dom.getAttribute(action,"delay"))) + ",\n" +
"sendId: " + (action.hasAttribute("idlocation") ? pm.platform.dom.getAttribute(action,"idlocation") : JSON.stringify(pm.platform.dom.getAttribute(action,"id"))) + "\n" +
"delay: " + (pm.platform.dom.hasAttribute(action,"delayexpr") ? pm.platform.dom.getAttribute(action,"delayexpr") : getDelayInMs(pm.platform.dom.getAttribute(action,"delay"))) + ",\n" +
"sendId: " + (pm.platform.dom.hasAttribute(action,"idlocation") ? pm.platform.dom.getAttribute(action,"idlocation") : JSON.stringify(pm.platform.dom.getAttribute(action,"id"))) + "\n" +
"});";
},

"foreach" : function(action){
var isIndexDefined = action.hasAttribute("index"),
var isIndexDefined = pm.platform.dom.hasAttribute(action,"index"),
index = pm.platform.dom.getAttribute(action,"index") || "$i", //FIXME: the index variable could shadow the datamodel. We should pick a unique temperorary variable name
item = pm.platform.dom.getAttribute(action,"item"),
arr = pm.platform.dom.getAttribute(action,"array");
Expand Down Expand Up @@ -221,14 +221,14 @@ function makeActionFactory(topLevelScripts,actionStrings,datamodel){

function constructSendEventData(action){

var namelist = action.hasAttribute("namelist") ? pm.platform.dom.getAttribute(action,"namelist").trim().split(/ +/) : null,
var namelist = pm.platform.dom.hasAttribute(action,"namelist") ? pm.platform.dom.getAttribute(action,"namelist").trim().split(/ +/) : null,
params = pm.platform.dom.getChildren(action).filter(function(child){return pm.platform.dom.localName(child) === 'param';}),
content = pm.platform.dom.getChildren(action).filter(function(child){return pm.platform.dom.localName(child) === 'content';});

if(content.length){
//TODO: instead of using textContent, serialize the XML
return JSON.stringify(content.map(function(child){return pm.platform.dom.textContent(child);})[0]);
}else if(action.hasAttribute("contentexpr")){
}else if(pm.platform.dom.hasAttribute(action,"contentexpr")){
return pm.platform.dom.getAttribute(action,"contentexpr");
}else{
var s = "{";
Expand Down
2 changes: 1 addition & 1 deletion lib/core/util/docToModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function inlineSrcs(url,doc,cb){
}

function traverse(node,nodeList){
if((pm.platform.dom.localName(node) === 'script' || pm.platform.dom.localName(node) === 'data') && node.hasAttribute("src")){
if((pm.platform.dom.localName(node) === 'script' || pm.platform.dom.localName(node) === 'data') && pm.platform.dom.hasAttribute(node,"src")){
nodeList.push(node);
}

Expand Down
16 changes: 16 additions & 0 deletions lib/embedded/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ module.exports = {
return node.getAttribute(attribute);
},

hasAttribute : function(node,attribute){
return node.hasAttribute(attribute);
},

namespaceURI : function(node){
return node.namespaceURI;
},

createElementNS : function(doc,ns,localName){
return doc.createElementNS(ns,localName);
},

setAttribute : function(node,name,value){
return node.setAttribute(name,value);
},

appendChild : function(parent,child){
return parent.appendChild(child);
},

textContent : function(node,txt){
if(txt === undefined){
if(node.nodeType === 1){
Expand Down

0 comments on commit 008da7b

Please sign in to comment.