44package pvl
55
66import (
7+ "bytes"
78 b64 "encoding/base64"
89 "net"
910 "net/url"
@@ -66,8 +67,10 @@ const (
6667 cmdAssertCompare commandName = "assert_compare"
6768 cmdWhitespaceNormalize commandName = "whitespace_normalize"
6869 cmdRegexCapture commandName = "regex_capture"
70+ cmdReplaceAll commandName = "replace_all"
6971 cmdParseURL commandName = "parse_url"
7072 cmdFetch commandName = "fetch"
73+ cmdParseHTML commandName = "parse_html"
7174 cmdSelectorJSON commandName = "selector_json"
7275 cmdSelectorCSS commandName = "selector_css"
7376 cmdFill commandName = "fill"
@@ -357,6 +360,7 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
357360 case ins .AssertCompare != nil :
358361 case ins .WhitespaceNormalize != nil :
359362 case ins .RegexCapture != nil :
363+ case ins .ReplaceAll != nil :
360364 case ins .ParseURL != nil :
361365 case ins .Fill != nil :
362366
@@ -388,6 +392,7 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
388392 return logerr (g , service , whichscript , i ,
389393 "Unsupported fetch type: %v" , fetchType )
390394 }
395+ case ins .ParseHTML != nil :
391396 case ins .SelectorJSON != nil :
392397 // Can only select after fetching.
393398 switch {
@@ -414,6 +419,12 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
414419 return logerr (g , service , whichscript , i ,
415420 "Script contains css selector in non-html mode" )
416421 }
422+
423+ // Can only select one of text, attr, or data.
424+ if ins .SelectorCSS .Attr != "" && ins .SelectorCSS .Data {
425+ return logerr (g , service , whichscript , i ,
426+ "Script contains css selector with both 'attr' and 'data' set" )
427+ }
417428 default :
418429 return logerr (g , service , whichscript , i ,
419430 "Unsupported PVL instruction: %v" , ins )
@@ -548,12 +559,18 @@ func stepInstruction(g proofContextExt, ins instructionT, state scriptState) (sc
548559 case ins .RegexCapture != nil :
549560 newState , stepErr = stepRegexCapture (g , * ins .RegexCapture , state )
550561 customErrSpec = ins .RegexCapture .Error
562+ case ins .ReplaceAll != nil :
563+ newState , stepErr = stepReplaceAll (g , * ins .ReplaceAll , state )
564+ customErrSpec = ins .ReplaceAll .Error
551565 case ins .ParseURL != nil :
552566 newState , stepErr = stepParseURL (g , * ins .ParseURL , state )
553567 customErrSpec = ins .ParseURL .Error
554568 case ins .Fetch != nil :
555569 newState , stepErr = stepFetch (g , * ins .Fetch , state )
556570 customErrSpec = ins .Fetch .Error
571+ case ins .ParseHTML != nil :
572+ newState , stepErr = stepParseHTML (g , * ins .ParseHTML , state )
573+ customErrSpec = ins .ParseHTML .Error
557574 case ins .SelectorJSON != nil :
558575 newState , stepErr = stepSelectorJSON (g , * ins .SelectorJSON , state )
559576 customErrSpec = ins .SelectorJSON .Error
@@ -704,6 +721,21 @@ func stepRegexCapture(g proofContextExt, ins regexCaptureT, state scriptState) (
704721 return state , nil
705722}
706723
724+ func stepReplaceAll (g proofContextExt , ins replaceAllT , state scriptState ) (scriptState , libkb.ProofError ) {
725+ from , err := state .Regs .Get (ins .From )
726+ if err != nil {
727+ return state , err
728+ }
729+
730+ replaced := strings .Replace (from , ins .Old , ins .New , - 1 )
731+ err = state .Regs .Set (ins .Into , replaced )
732+ if err != nil {
733+ return state , err
734+ }
735+
736+ return state , nil
737+ }
738+
707739func stepParseURL (g proofContextExt , ins parseURLT , state scriptState ) (scriptState , libkb.ProofError ) {
708740 s , err := state .Regs .Get (ins .From )
709741 if err != nil {
@@ -805,6 +837,24 @@ func stepFetch(g proofContextExt, ins fetchT, state scriptState) (scriptState, l
805837 }
806838}
807839
840+ func stepParseHTML (g proofContextExt , ins parseHTMLT , state scriptState ) (scriptState , libkb.ProofError ) {
841+ from , err := state .Regs .Get (ins .From )
842+ if err != nil {
843+ return state , err
844+ }
845+
846+ gq , err2 := goquery .NewDocumentFromReader (bytes .NewBuffer ([]byte (from )))
847+ if err2 != nil {
848+ return state , libkb .NewProofError (keybase1 .ProofStatus_FAILED_PARSE , "Failed to parse html from '%v': %v" , ins .From , err2 )
849+ }
850+
851+ state .FetchResult = & fetchResult {
852+ fetchMode : fetchModeHTML ,
853+ HTML : gq ,
854+ }
855+ return state , nil
856+ }
857+
808858func stepSelectorJSON (g proofContextExt , ins selectorJSONT , state scriptState ) (scriptState , libkb.ProofError ) {
809859 if state .FetchResult == nil || state .FetchResult .fetchMode != fetchModeJSON {
810860 return state , libkb .NewProofError (keybase1 .ProofStatus_INVALID_PVL ,
@@ -852,8 +902,7 @@ func stepSelectorCSS(g proofContextExt, ins selectorCSST, state scriptState) (sc
852902 }
853903
854904 // Whether to get an attribute or the text contents.
855- useAttr := ins .Attr != ""
856- res := selectionContents (selection , useAttr , ins .Attr )
905+ res := selectionContents (selection , ins .Attr , ins .Data )
857906
858907 err := state .Regs .Set (ins .Into , res )
859908 return state , err
@@ -890,9 +939,11 @@ func runCSSSelectorInner(g proofContextExt, html *goquery.Selection, selectors [
890939 selection = selection .Eq (selector .Index )
891940 case selector .IsKey :
892941 selection = selection .Find (selector .Key )
942+ case selector .IsContents :
943+ selection = selection .Contents ()
893944 default :
894945 return nil , libkb .NewProofError (keybase1 .ProofStatus_INVALID_PVL ,
895- "Selector entry must be a string or int %v" , selector )
946+ "CSS selector entry must be a string, int, or 'contents' %v" , selector )
896947 }
897948 }
898949
@@ -957,9 +1008,10 @@ func runSelectorJSONInner(g proofContextExt, state scriptState, selectedObject *
9571008 results = append (results , innerresults ... )
9581009 }
9591010 return results , nil
1011+ default :
1012+ return nil , libkb .NewProofError (keybase1 .ProofStatus_INVALID_PVL ,
1013+ "JSON selector entry must be a string, int, or 'all' %v" , selector )
9601014 }
961- return []string {}, libkb .NewProofError (keybase1 .ProofStatus_INVALID_PVL ,
962- "Invalid selector entry: %v" , selector )
9631015}
9641016
9651017// Take a regex descriptor, do variable substitution, and build a regex.
0 commit comments