Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

JavaScript Rules

Options
LegacyForum
LegacyForum Posts: 1,664 ✭✭
Is there a good document on how JavaScript rules work? What I am trying to do to understand how it works is to write a rule that takes an node, checks several attributes, and sometimes need to walk up the DOM tree to check for another element. I think its easier to do this in JavaScript than doing the rule building, but there is no documentation on how it works.

regards,
Doron

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    All CodeWizard rules, including Javascript rules, are created using the RuleWizard. The RuleWizard documentation can be found through WebKing under Help: RuleWizard Documentation. That will give you a general idea on how RuleWizard works and thus how JavaScript rules work.

    A couple sections might be of interest to you specifically. Page 15, "Writing Rules About Javascript, VBScript, and CSS Embedded in HTML Pages," explains how to apply a Javascript rule to Javascript in an HTML page. Also, page 26, "Example 1: Creating a Simple Javascript Rule," goes through a tutorial process for making your own Javascript rules.

    That being said, it's likely you have a question that is more specific than what the documentation provides. If that's the case, please feel free to go into more detail by posting follow-up questions or contacting us at webkingsupport@parasoft.com.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    I want to basically look for occurances of ".getElementById" in JavaScript code, finding it hard to figure exactly how to do that.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited April 2004
    Options
    You can quickly create a rule like this by using RuleWizard's Auto-Creation ability.

    + First, choose to create a new JavaScript rule. (Go to File:New and choose "Rule" on the left hand side of the dialog. When the dialog changes, under "Dictionary" choose "JavaScript.")
    + Choose to auto-create your rule. (Select the "Auto-Create" radio button)
    + In the text area, type something like:
    CODE
    foo.getElementById()
    Then just click "OK" and the next thing you will see is the auto-created rule.

    Note that the rule it creates is too vague for your purposes. Specifically, it will be looking for CODE^[a-z]+[A-Z][a-z]+[A-Z][a-z][A-Z][a-z]$ on the right hand side. What you really want is to look for the function "getElementById()." To change the value, just double click the node with the long pattern and replace it with "getElementById()." Then the rule is complete.

    Now, you may notice that the rule is doing a lot more checking than you probably care to do. For example, it's checking for particular left hand side values when all you're interested in is the right hand side. It may be sufficient to create a much simpler rule. You can do this by manually creating a rule as follows:

    + First, choose to create a new JavaScript rule. It will automatically assume you wish to create this rule "By Node"
    + In the node tree, under java script:Expressions:Miscellaneous choose "a.b" and click "OK"
    + You will see a rule looking for just "a.b." Right-click that node and select "Right Hand Side."
    + From the dialog that pops up, under java script:Declarations choose "Reference."
    + Right-click the new "Right Hand Side" node and select "Name." Input "getElementById.
    + Finally, right click the first node in the rule ( a.b ) and choose Create Output:Display and choose the message you would like to have show when the rule fires.

    You may also have to set the header to make the rule complete, but that isn't core to the functionality of the rule. Hopefully that wasn't too much detail. Good luck with your project.

    -employee.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    That cleared it up, thanks.

    What if I want to search if a JavaScript file contains at least one "document.all", but no "document.getElementById" - I would need 2 checks, what should their parent be?
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited April 2004
    Options
    The way you've formulated the check, it sounds like you could use two rules for this problem thereby removing any concerns about a common parent. Rule-A would look to see that the document contained at least one "document.all" and if not it would fire a message. Rule-B would fire if there was any instance of "document.getElementById."

    Rule-B is trivial (we already answered that one earlier). Rule-A is a little trickier in that it requires a collector. Basically, you will look for a.b whose left is "document" and whose right is "all." Attach a collector to the first node ( a.b ). The collector will increment each time its parent is true. (To attach a collector, right-click the node and choose "Create Collector.") On the collector you will want to check the count (right-click the collector and choose "Count(#)") for $$<1 or $$==0. This way, the rule will only fire if there are no "document.all" instances in the entire document. Put the output on the counter node (the one that is checking for $$<1 or $$==0) and enter the text you want to see when this rule has been broken.<br />
    You could combine these two rules (if you so desired) with a.b as the common parent and moving the Rule-B collector to the last logical node (the right hand side's name node). I have attached an example for reference.

    (See the RuleWizard Guide for more information on collectors, page 16, "Adding a Collector").
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited May 2004
    Options
    QUOTE(employee @ Apr 19 2004, 04:09 PM)
    The way you've formulated the check, it sounds like you could use two rules for this problem thereby removing any concerns about a common parent. Rule-A would look to see that the document contained at least one "document.all" and if not it would fire a message. Rule-B would fire if there was any instance of "document.getElementById."

    Rule-B is trivial (we already answered that one earlier). Rule-A is a little trickier in that it requires a collector. Basically, you will look for a.b whose left is "document" and whose right is "all." Attach a collector to the first node ( a.b ). The collector will increment each time its parent is true. (To attach a collector, right-click the node and choose "Create Collector.") On the collector you will want to check the count (right-click the collector and choose "Count(#)") for $$<1 or $$==0. This way, the rule will only fire if there are no "document.all" instances in the entire document. Put the output on the counter node (the one that is checking for $$<1 or $$==0) and enter the text you want to see when this rule has been broken.<br />
    You could combine these two rules (if you so desired) with a.b as the common parent and moving the Rule-B collector to the last logical node (the right hand side's name node). I have attached an example for reference.

    (See the RuleWizard Guide for more information on collectors, page 16, "Adding a Collector").

    What I need is for a .js file, if there is at least one document.all and no document.getElementById in it, create a violation.

    I trued modifying your example rule to have the output on the (a.B) node and have a counter check for $$ > 0 on the document.all check and a $$==0 counter on the document.getElementById one, but that doesn't seem to work.

    Do I have to label the 2 counters and then check them in my output?
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    Hi Doron,

    Just want to let you know that I am looking into your question and hope to have an answer for you soon. Thank you for your patience.

    - employee.